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.
GnuPG also supports symmetric encryption algorithms. By default, GnuPG uses the AES symmetrical algorithm since version 2.1, CAST5 was used in earlier versions. GnuPG does not use patented or otherwise restricted software or algorithms.
The GNU Privacy GuardGnuPG, also known as GPG, is a command line tool with features for easy integration with other applications. A wealth of frontend applications and libraries are available. GnuPG also provides support for S/MIME and Secure Shell (ssh).
GPG is very secure, as long as your passphrase is long and strong enough. In practice, your passphrase will almost always be the weakest link. This instructs GPG to use a password hashing method that is as slow as possible, to try to provide a bit of extra resistance against password guessing attacks.
To encrypt a document the option --encrypt is used. You must have the public keys of the intended recipients. The software expects the name of the document to encrypt as input or, if omitted, on standard input. The encrypted result is placed on standard output or as specified using the option --output.
Note, that using asymmetric encryption such as RSA public key encryption in these cases would not be efficient, because it would add more complexity to the implementation, where this is not necessary, since we don’t have a key exchange problem. We can use the Java API directly from Scala.
You have here an example of openpgp ByteArrayHandler. There might be some incompatibility between BouncyCastle encryption and GnuGP encryption though, since BouncyCastle does not use GnuPG, but rather implements OpenPGP (RFC2440) in Java.
We can use the Java API directly from Scala. We don’t need a class because we don’t need to store any state. That is why we are using an object. This is what the code would look like. The first thing we need to do is creating a cipher.
The Windows version is part of Gpg4win . gpg_encrypt () is a PHP function that will allow you to easily use GnuPG to encrypt data to your public PGP key and mail that encrypted data to yourself, where it can be securely decrypted with your private key. This is designed primarily for use with web-based forms but can be used to encrypt any data.
You can try to call the JAVA API of BouncyCastle.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 BouncyCastle encryption and GnuGP encryption though, since BouncyCastle does not use GnuPG, but rather implements OpenPGP (RFC2440) in Java.
I recently had to work on GPG encryption-decryption and did find BountyCastle's PGP library does the trick. The steps were
1) Add the version in pom.xml properties
<org.bouncycastle.version>1.46</org.bouncycastle.version>
2) Add the following dependencies
<!-- Dependency for PGP and GPG Encryption-Decryption --> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcmail-jdk15</artifactId> <version>${org.bouncycastle.version}</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcpg-jdk15</artifactId> <version>${org.bouncycastle.version}</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15</artifactId> <version>${org.bouncycastle.version}</version> </dependency>
3) In the implementation class added the provider with Java Security
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
4) The rest of the code was just simple Java implementation
File encryptedFile = new File(encryptedFileName); byte[] encryptedByteArray = FileUtils.readFileToByteArray(inputFile); byte[] decryptedByteArray = ByteArrayHandler.decrypt(encryptedByteArray, passPhrase.toCharArray()); String decryptedString = new String(decryptedByteArray);
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With