Dec 8, 2013

PGP PGP/Mime EMail processing in Java

I have to problem to read emails from an IMAP-Server, where the emails are encrypted with PGP/GPG.

This toolset uses a public/private Keyset for encryption. Thunderbird with the Plugin Enigmail supports
the encryption/decryption of emails with S/Mime OR PGP/Mime encrypted email messages.

On my Fedora 19, the gpg2 command can be used to manage the GPG-Keys on the command line.

But my problem is using this feature in a service developed in Java.

There is an "old" project named javamail-crypto on sourceforge.net. After compiling the source with ant, the
cryptix is not necessary -> only the bouncycastle-pgp provider.

After using the source from here, I could decrypt messages, but the day after, new encrypted messages
couldn't be encrypted.

Here are the exceptions I got:

org.bouncycastle.openpgp.PGPException: Exception creating cipher
at org.bouncycastle.openpgp.operator.jcajce.OperatorHelper.createDataDecryptor(Unknown Source) at org.bouncycastle.openpgp.operator.jcajce.OperatorHelper.createDataDecryptor(Unknown Source)
at org.bouncycastle.openpgp.operator.jcajce.OperatorHelper.createDataDecryptor(Unknown Source)
org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder$2.createDataDecryptor(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
at net.suberic.crypto.bouncycastlepgp.BCPGPProviderImpl.decrypt(BCPGPProviderImpl.java:106)
at net.suberic.crypto.PGPEncryptionUtils.decrypt(PGPEncryptionUtils.java:52)
at net.suberic.crypto.PGPEncryptionUtils.decryptMultipart(PGPEncryptionUtils.java:225)
at javamailtest.JavaMailTest.main(JavaMailTest.java:116)
Caused by: java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.a(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
at javax.crypto.Cipher.init(DashoA13*..)
... 10 more
Exception in thread "main" java.security.GeneralSecurityException: Incorrectly formed message:  org.bouncycastle.openpgp.PGPException: Exception creating cipher
at net.suberic.crypto.bouncycastlepgp.BCPGPProviderImpl.decrypt(BCPGPProviderImpl.java:140)
at net.suberic.crypto.PGPEncryptionUtils.decrypt(PGPEncryptionUtils.java:52)
at net.suberic.crypto.PGPEncryptionUtils.decryptMultipart(PGPEncryptionUtils.java:225)
at javamailtest.JavaMailTest.main(JavaMailTest.java:116)
Java Result: 1

After long time of searching, I found a hint on the bouncycastle homepage, about a missing JCE files from the JDK to get the decryption to work again.

After downloading the JCE-Package from Oracle's JDK and installing, the decryption worked again.

Now I found, that the openJDK-Installation on Fedora 19 had no problems, but the JDK from Oracle with
the missing JCE.