Decrypt .p7m file
843811Apr 23 2009 — edited Apr 23 2009I have trouble decrypting a .p7m file.
Can someone help me?
What I try to do is getting the key from the certificate file (xxx.pks)
Use the key to decrypted the .p7m file and write to another file.
I can retrieve the key from the certificate.
However, I cannot decrypt the .p7m file.
Thanks.
Here is my code:
//get receiver cert
X509Certificate receiverCert = (X509Certificate)keystore.getCertificate(alias);
//get key
Key key = keystore.getKey(alias, password.toCharArray());
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
File p7mFile = new File("C:\\temp\\test.p7m");
DataInputStream input = new DataInputStream(new FileInputStream(p7mFile ));
File out = new File("C:\\temp\\out");
FileOutputStream fotest= new FileOutputStream(out);
try {
byte[] data = new byte[1024];
int read;
try {
while((read = input.read(data)) >= 0) {
fotest.write(cipher.update(data,0, read));
}
} finally {
fotest.write(cipher.doFinal());
fotest.close();
}
Edited by: Jenny_Run on Apr 23, 2009 12:25 PM