OpenPGPJS: Invalid session key for decryption

1.7k Views Asked by At

OpenPGP JS throws the following error when I attempt to decrypt some armored data with my private key: Uncaught (in promise) Error: Error decrypting message: Invalid session key for decryption. at onError (openpgp.min.js:16057) onError @ openpgp.min.js:16057

From what I can tell from google, this means something is going wrong with the encryption, but I can't tell what it is. What makes it worse is that it seems to be inconsistent with only certain files (seemingly encrypted around the same time?) failing in this way. The encrypted messages don't seem to be malformed in any way.

If anyone has any tips for debugging this it would be appreciated. What could throw this error? Excerpts of my code are below, based primarily on the openPGPJS example code.

For extra information about what my code is doing, image files are being encrypted on the client side, uploaded to a server, downloaded elsewhere, and then being decrypted.

function encryptData(data) {
  var openpgp = window.openpgp;
  var options, encrypted;
  var pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----...-----END PGP PUBLIC KEY BLOCK-----`;

  options = {
    data: data,
    publicKeys: openpgp.key.readArmored(pubkey).keys
  };

  return openpgp.encrypt(options);
}

function decryptPGP(encData, doneFunc) {
    var privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----...-----END PGP PRIVATE KEY BLOCK-----`;
    var pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----...-----END PGP PUBLIC KEY BLOCK-----`;
    var passphrase = '...';
    var privKeyObj = openpgp.key.readArmored(privkey).keys[0];
    privKeyObj.decrypt(passphrase);

    options = {
        message: openpgp.message.readArmored(encData),
        publicKeys: openpgp.key.readArmored(pubkey).keys,
        privateKey: privKeyObj
    };

    openpgp.decrypt(options).then(function(plaintext) {
        doneFunc(plaintext.data);
    });
}
1

There are 1 best solutions below

0
On

I have had the same issue. To resolve it, encode your result from encryption with base64. That base64 string can then be sent over the internet as you desire. When you want to decrypt, just decrypt base64 first and then

await openpgp.message.readArmored(Base64.decode(encData))

will work!