neuhalje / bouncy-gpg

Make using Bouncy Castle with OpenPGP fun again!
https://neuhalje.github.io/bouncy-gpg/
Other
205 stars 58 forks source link

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

Closed chanurahemal closed 3 years ago

chanurahemal commented 3 years ago

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 commented 3 years ago

Hi @chanurahemal,

Was there a solution for this bug ??

chanurahemal commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Awesome, thanks!

On Mon, 6 Sep 2021 at 7:59 pm, SPE-ALath @.***> wrote:

Hi @chanurahemal https://github.com/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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/neuhalje/bouncy-gpg/issues/64#issuecomment-913516574, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACV6EBV2BLGEEDZQQ7QUXQLUASGIVANCNFSM47C7PEJQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

constantin-baciu commented 1 year ago

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)