Question

Having imported a PGP key using SpongyCastle and checked that I can decrypt using it, I want to add a password to secure the keyring. So I make an encrypted copy:

secretKeyRing = PGPSecretKeyRing.copyWithNewPassword(secretKeyRing, new char[] {},
                newPassPhrase.toCharArray(), PGPEncryptedData.CAST5,
                new SecureRandom(), new BouncyCastleProvider());

I test the decryption with:

testKey = secretKeyRing.getSecretKey().extractPrivateKey(newPassPhrase.toCharArray(), new BouncyCastleProvider());

This works fine, apparently.

When I try to decrypt a file with this encrypted key, using the same password:

PGPPrivateKey testKey = secretKey.extractPrivateKey(passPhrase.toCharArray(),
                                                   new BouncyCastleProvider());

where secretKey is the master key of the keyring, I get a PGPException saying

Exception constructing key

I feel like I am missing something obvious. Please feel free to ask for more details.

This is on a custom pgp app I am building, which only needs to import a single private key, so the master key is the only key, AFAI understand.

Was it helpful?

Solution

I found a solution to the problem: I simply needed to use the same algorithm to encode the new keyring.

secretKeyRing = PGPSecretKeyRing.copyWithNewPassword(secretKeyRing, new char[] {},
                newPassPhrase.toCharArray(), secretKeyRing.getSecretKey().getKeyEncryptionAlgorithm(),
                new SecureRandom(), new BouncyCastleProvider());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top