1

I'm have encrypted a text file on Linux using:

 gpg --cipher-algo AES256 -c file.txt 

That command asks for a passphrase, let's say we enter "123" here.

This file can be trivially decrypted:

gpg -d file.txt.gpg

Now I like to decrypt this file in Java, but can't quite find out how to do this using the passphrase "123". Specifically, it's not entirely clear what the salt and initial vector is, and what else is needed.

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
dexter meyers
  • 2,798
  • 2
  • 18
  • 22

1 Answers1

4

GnuPG implements the OpenPGP protocol, which is not directly support by Java's native classes. OpenPGP has its own file format, but also uses a slightly different variant of the CFB mode.

Instead of implementing all that on your own, better go for the Bouncy Castle library. It also provides an example how to decrypt a symmetrically encrypted message, which boils down to those relevant calls to decrypt an OutputStream out (some more code to determine the used algorithm parameter and compression is also provided in the linked example):

PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
        new JcePGPDataEncryptorBuilder(algorithm).setSecureRandom(
          new SecureRandom()).setProvider("BC"));
encGen.addMethod(
        new JcePBEKeyEncryptionMethodGenerator(passPhrase).setProvider("BC"));
OutputStream encOut = encGen.open(out, compressedData.length);
Community
  • 1
  • 1
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • 1
    Remember if using _Oracle_ Java, AES-256 requires the Unlimited Strength Jurisidiction Policy to be installed. – dave_thompson_085 Mar 23 '17 at 00:18
  • 1
    That looked like it should work, but in practice I still got exceptions about invalid padding. After working on this for nearly 72 hours straight I've decided to give up on it. I upvoted your post as it may help someone, but can't accept it since it didn't work for me personally. – dexter meyers Mar 27 '17 at 09:55