views:

1135

answers:

1

I would like to be able to encrypt files on disk and/or data in memory using GnuPG from a Java application. If possible I'd like to avoid having to make system calls out to the GPG command line tools.

Is there a recommended library, or can you recommend the best approach to GPG encrypting from Java (or Scala)?

I'm developing and intend to run the application in a Linux environment, although a cross-platform solution would be preferred.

+3  A: 

You can try to call the JAVA API of BountyCastle.org.

Its documentation mentions:

The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms.

You have here an example of openpgp ByteArrayHandler.

There might be some incompatibility between BountyCastle encryption and GnuGP encryption though, since BountyCastle does not use GnuPG, but rather implements OpenPGP (RFC2440) in Java.

VonC
Thanks for the answer - looks promising. Will give it a go and report back on whether I can make it work with GnuPG. Looks like it might just be about careful selection of mutually compatible options.
James Shade
Have got this to work now. I recommend looking at the example code in org.bouncycastle.openpgp.examples.KeyBasedFileProcessor in particular. Tricky bits included finding the desired public key within the key ring collection, and working out that the JCE implementation supplied in the standard JDK is crippled, and you need to explicitly download and install the "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 6" from the Sun website.
James Shade
@James Shade: thank you for the feedback. Very instructive.
VonC