neuhalje/bouncy-gpg

unknown packet type encountered: 20 error is coming when decrypt

chanurahemal opened this issue · 5 comments

Describe the bug
I created gpg keys(public/private) and managed to successfully encrypt/decrypt files using keys in command line.
Encrypt : gpg --batch --armor --passphrase "PASSWORD" -se --recipient "RECIPIENT" FILE.txt

it generated .asc file and i managed to decrypt it using bellow command
Decrypt: gpg --yes --batch --passphrase "PASSWORD" FILE.txt.asc

But when i try to decrypt the same file in code, I am getting "unknown packet type encountered: 20" error

Sample Code:
{
System.out.println("Initializing Bouncy Castle");

    BouncyGPG.registerProvider();

    System.out.println("Creating keyring");

//I am providing pub/keys as strings
final InMemoryKeyring keyring = KeyringConfigs
.forGpgExportedKeys(KeyringConfigCallbacks.withPassword("passwod"));
keyring.addSecretKey(BPAY_PRIVATE.getBytes());
keyring.addPublicKey(BPAY_PUBLIC.getBytes());

    final InputStream cipherTextStream = Files.newInputStream(Path.of("FILE.txt.asc"));

    final InputStream decryptedInputStream = BouncyGPG
            .decryptAndVerifyStream()
            .withConfig(keyring)
            .andRequireSignatureFromAllKeys("USERID")
            .fromEncryptedInputStream(cipherTextStream);

    byte[] plain = new byte[2048];
    int len = decryptedInputStream.read(plain);

    String plainText = new String(plain, 0, len);
    System.out.println(plainText);

}

Hi @chanurahemal,

Was there a solution for this bug ??

Hi @chanurahemal,

Was there a solution for this bug ??

Actually I regenerated keys and tested, then it worked. That's why i closed the ticket. If you are having the same issue, try with generating keys on a linux machine and try. I used to generate keys on a mac and i got above error. But when i tried with keys generated on linux, it worked. Hope this helps. Good luck

Hi @chanurahemal,

I was able to resolve it as well. I was generating key using Mac too. I was getting error because the file had to be encrypted using option —openpgp

Once I used above options while encrypting, the error was gone

Hi All,

I'm currently running into the same issue.
I've tried encrypting the file with the "--openpgp" flag, but I'm still getting the "unknown packet type encountered: 20" when trying to decrypt.

Was there anything else that needed to be done?

BTW: I'm on version 2.3.0 from Maven (https://mvnrepository.com/artifact/name.neuhalfen.projects.crypto.bouncycastle.openpgp/bouncy-gpg/2.3.0)