Can't encrypt the string in OpenPGP.js

150 views Asked by At

I am trying encrypt text with OpenPGP.js.

Here is an example of error: https://jsfiddle.net/7e2s516d/

let publicKeyArmored = `-----BEGIN PGP PUBLIC KEY BLOCK-----

xjMEYLTYzBYJKwYBBAHaRw8BAQdAoatyco6C3CO87ZxgjIfWsFv3RMxs5W1F
EinwU2UiQ+bNF3Rlc3QgPHRlc3RAZXhhbXBsZS5vcmc+wowEEBYKAB0FAmC0
2MwECwkHCAMVCAoEFgACAQIZAQIbAwIeAQAhCRC+iM+wNlG7chYhBFZgyLBQ
Cavx4mET576Iz7A2UbtyVa8BAP1oFrSwoGdd7+dgBUu7KwU02wvrtuqyURe5
GFSTyMn2AQDWTsJjgYq7hPqSQoeZ30xN0eUoiUiyIA/ffqhM2qHPD844BGC0
2MwSCisGAQQBl1UBBQEBB0CxgKqNlcBTkuaFRvYm/UbR8ZKFFda0tnUCTzl/
tgCsJgMBCAfCeAQYFggACQUCYLTYzAIbDAAhCRC+iM+wNlG7chYhBFZgyLBQ
Cavx4mET576Iz7A2UbtysPgA/0pn4n3QqBBc56/okcGFdC78LEwDLN/ZLkY6
azfQ1aqwAP0d9CWw9ywheGtB1SBCzHZbToKaltVFCNT1ArSUFK45Dg==
=pnHg
-----END PGP PUBLIC KEY BLOCK-----
`;

let messageEncrypt = openpgp.encrypt({
    message: openpgp.createMessage({ text: 'test' }),
  publicKeys: openpgp.readKey({ armoredKey: publicKeyArmored })
});

console.log(messageEncrypt);

I am using the latest version (5.0.0-3) of OpenPGP.js

1

There are 1 answers

0
Mirek Pluta On BEST ANSWER

It looks like you didn't check documentation of those functions.

  1. encrypt, createMessage and readKey are returning a Promise, not a simple value (i.e. Message or PublicKey). Therefore you need to use those promises correctly.
  2. EncryptOptions object that you pass to encrypt method should not have publicKeys property, but encryptionKeys

Once you do those changes it'll work fine.