I have a string message called customerPublicKey which I encrypted with OpenPGP.js:
const { customerPublicKey} = await openpgp.generateKey({
type: 'rsa',
passphrase: contract.passphrase,
userIDs: [{ name: 'My Name', email: '[email protected]' }]
});
const message = await openpgp.createMessage({text: 'stringKey'});
const encrypted = await openpgp.encrypt({
message, // input as Message object
encryptionKeys: customerPublicKey
});
I'm trying to decrypt it with OpenPGP.js, like this:
const buyerEncryptedKey = await openpgp.readMessage({
armoredMessage: encrypted
})
const { privateKey } = await openpgp.generateKey({
type: 'rsa',
passphrase: passphrase.value,
userIDs: [{ name: 'My Name', email: '[email protected]' }]
});
const decryptionKeys = await openpgp.decryptKey({
privateKey: await openpgp.readPrivateKey({armoredKey:privateKey}),
passphrase: passphrase.value
})
This is throwing the following error:
error: Error: Error decrypting message: Session key decryption failed.
What do I need to do to debug this? Does anyone have insight here?
That error is because your code is wrong. Your
privateKey-- returned fromopenpgp.generateKey-- is the externalized (encrypted, serialized and armored) form of the key, butopenpgp.decryptwants the internal (JS-object) form; to get that you need to read back and decrypt the external form like this:However, that gives a different error:
because your design is wrong. You can only decrypt a publickey-encrypted message using the privatekey that MATCHES the publickey -- i.e. the other half of the same keypair, created by the same key-generation operation. Generating a new keypair as you do here creates a keypair that is different from all keypairs in existence anywhere in the world, and that includes the keypair whose publickey you (or someone) used to encrypt your message. Since the generated key is different from the correct key, it cannot decrypt the message.