Bouncycastle (Spongycastle) key import and use on android

590 views Asked by At

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.

1

There are 1 answers

0
Tom Macdonald On BEST ANSWER

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