I'm attempting to decrypt an S/MIME email (sent originally via Outlook), and to do that, I'm using the bouncycastle API. I'm running into a snag, though.
I have, in the Windows certificate store, the certificate for the recipient. I had previously used it to send a signed and encrypted email to the other party, and they in turn used it to send me an encrypted reply. I then exported the certificate (with private key) as a .pfx file, and I loaded this pfx file into a Java KeyStore. It doesn't work, however, and I suspect that's because the subject key identifiers don't match.
Here's the code I'm using to get the subject key id from the KeyStore:
KeyStore ks = KeyStore.getInstance("PKCS12");
char[] pw = "password".toCharArray();
ks.load(new FileInputStream("d:\\cert_priv_key.pfx"), pw);
Enumeration en = ks.aliases();
while( en.hasMoreElements() )
{
String alias = (String)en.nextElement();
System.out.println(alias);
if( ks.isKeyEntry(alias) )
{
Certificate[] chain = ks.getCertificateChain(alias);
X509Certificate cert = (X509Certificate)chain[0];
byte[] id = cert.getExtensionValue("2.5.29.14");
System.out.println(" " + toHex(id));
}
}
This prints out the following key identifier:
04 16 04 14 88 ed bb 7c 64 7b 41 63 48 0a 24 40 2b 3c d0 78 72 3c 30 b3
When I check the Windows certificate store, however, the key identifier is different:
88 ed bb 7c 64 7b 41 63 48 0a 24 40 2b 3c d0 78 72 3c 30 b3
The KeyStore returns an extra 4 bytes in the front (the subject key identifier should be the 160-bit SHA1 hash of the key, and therefore 20 bytes long, correct?).
Even more confusing is the fact that when I parse the S/MIME email using the bouncycastle API, and go through the recipients (SMIMEEnveloped.getRecipientInfos().getRecipients()
), the only recipient returned (there should be only one) has this subject key identifier:
04 14 88 ed bb 7c 64 7b 41 63 48 0a 24 40 2b 3c d0 78 72 3c 30 b3
... it has only two extra bytes, not four, and I assume this is why I'm unable to decrypt the email with the certificate.
Why do none of these subject key identifiers match up? What am I doing wrong?