Search code examples
javaencryptionbouncycastlegnupgpgp

gpg --list-keys is empty but file decrypts bouncycastle encrypted file


I am currently using BouncyCastle PGP in a Java application to read a public key from a string, and encrypt a file using that key. Loading the key...

Security.addProvider(new BouncyCastleProvider());
InputStream keyIn = new ByteArrayInputStream(publicKey.getBytes());
PGPPublicKeyRingCollection pgpKeyRing = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn), new JcaKeyFingerpringCalculator());
PGPPublicKey = pgpKeyRing.getKeyRings().next().getPublicKey();

The output file is able to be decrypted with

gpg --decrypt file.gpg

even though gpg --list-keys and gpg --list-secret-keys is empty

How is this possible? Is it reading a keyring from somewhere else possibly? I am on CentOS 7, and I have deleted ~/.gnupg and re-created it.

I have also tried changing the public key I am using to encrypt to, and it still decrypts.


Solution

  • As it turns out, my code had an error in it, and was writing out an Ascii armored version of the PGP Compressed file, but not Encrypted. As it would happen, gpg --decrypt will decompress this file, even if there are no keys in the keyring, because it never does a check. gpg --debug-level 9 was useful to figure this out.