Please help with VB(encrypt) -> Java (decrypt)
843811Jan 9 2008 — edited Jan 9 2008The digital signing, enveloping, de-enveloping and signature-verifying processes in my VB6 programs
are implemented by using the features which are exposed by Microsoft's CAPICOM ActiveX Control.
The primary CAPICOM objects and methods of which I made use are summarized as follows,
for digital signing
Object: CAPICOM.SignedData
Method: Sign()
for signature verification
Object: CAPICOM.SignedData
Method: Verify()
for digital enveloping
Object: CAPICOM.EnvelopedData
Method: Encrypt()
The invocation of Triple DES algorithm is accomplished simply by assigning
the CAPICOM_ENCRYPTION_ALGORITHM_3DES value to the
CAPICOM.EnvelopedData.Algorithm.Name property before invoking the Encrypt() method.
for de-enveloping
Object: CAPICOM.EnvelopedData
Method: Decrypt()
I can successfully get the privateKey from the .pfx file in java then i run this:
public boolean decryptByRSA(String targetFilePath, Key key, String decryptedFilePath){
try {
System.out.println("targetFilePath: " + targetFilePath);
// get file which need decrypt
FileInputStream in = new FileInputStream(targetFilePath);
byte[] cipherText = new byte[in.available()];
in.read(cipherText, 0, in.available());
in.close();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println("Start decryption..........");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(cipherText);
System.out.println("Finish decryption");
CryptUtil.writeObjToFile(decryptedFilePath, result);
System.out.println("new file write at: " + decryptedFilePath);
}catch (java.lang.Exception e){
System.out.println("error at decryptByRSA(String targetFilePath, PrivateKey privateKey, String decryptedFilePath): " + e.getMessage());
e.printStackTrace();
return false;
}
return true;
}
public boolean decryptByRSA(String targetFilePath, String privateKeyPath, String decryptedFilePath){
try {
System.out.println("privateKeyPath: " + privateKeyPath);
// get private key from file
RawRSAKey rawKey = RawRSAKey.getInstance(privateKeyPath);
RSAPrivateKeySpec privateSpec = new RSAPrivateKeySpec(rawKey.getModulus(), rawKey.getExponent());
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(privateSpec);
System.out.println("Private key format: " + privateKey.getFormat());
return decryptByRSA(targetFilePath, privateKey, decryptedFilePath);
}catch (java.lang.Exception e){
System.out.println("error at decryptByRSA(String targetFilePath, String privateKeyPath, String decryptedFilePath): " + e.getMessage());
e.printStackTrace();
return false;
}
}
and got this error:
javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA12275)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at TestRSA2.decryptByRSA(TestRSA2.java:121)
at TestRSA2.run(TestRSA2.java:80)
at TestRSA2.main(TestRSA2.java:64)
I searched google for a long time and still no hope.
Anyone can help to explain a detail solution how to handle a file which "developed" by VB in java? THANKS