Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unknown packet type encountered: 20 error is coming when decrypt #64

Closed
chanurahemal opened this issue Jun 22, 2021 · 5 comments
Closed

Comments

@chanurahemal
Copy link

chanurahemal commented Jun 22, 2021

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);

}

@SPE-ALath
Copy link

Hi @chanurahemal,

Was there a solution for this bug ??

@chanurahemal
Copy link
Author

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

@SPE-ALath
Copy link

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

@chanurahemal
Copy link
Author

chanurahemal commented Sep 6, 2021 via email

@constantin-baciu
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants