I am trying to use the Encrypt and decrypt String data with PGP keys example from openpgp.js but I struggle to make it work inside Firefox. openpgp.js doc
I create a keypair.
const openpgp = window.openpgp; // use as CommonJS, AMD, ES6 module or via window.openpgp
openpgp.config.compression = openpgp.enums.compression.zlib
var options = {
userIds: [{ name: 'Alicee', email: '[email protected]' }],
numBits: 2048,
passphrase: 'secretttoo'
};
var publicKeyAlice;
var privateKeyAlice;
openpgp.generateKey(options).then(key => {
privateKeyAlice = key.privateKeyArmored;
publicKeyAlice = key.publicKeyArmored;
console.log('Key generated');
console.log(privateKeyAlice);
console.log(publicKeyAlice);
});
The keys I get consoled out are used for the example of string encryption by openpgp.js
const pubkey = '-----BEGIN PGP PUBLIC KEY BLOCK----- Version: OpenPGP.js v4.1.1'
const privkey = '-----BEGIN PGP PRIVATE KEY BLOCK----- Version: OpenPGP.js v4.1.1'
const passphrase = `secretttoo` //what the privKey is encrypted with
const encryptDecryptFunction = async() => {
const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0]
await privKeyObj.decrypt(passphrase)
const options = {
message: openpgp.message.fromText('Hello, World!'), // input as Message object
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for encryption
privateKeys: [privKeyObj] // for signing (optional)
}
openpgp.encrypt(options).then(ciphertext => {
encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
return encrypted
})
.then(encrypted => {
const options = {
message: await openpgp.message.readArmored(encrypted), // parse armored message
publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for verification (optional)
privateKeys: [privKeyObj] // for decryption
}
openpgp.decrypt(options).then(plaintext => {
console.log(plaintext.data)
return plaintext.data // 'Hello, World!'
})
})
}
encryptDecryptFunction();
I get the following error in browser console:
SyntaxError: missing } after property list[Learn More] openpgp testing.html:153:27 note: { opened at line 152, column 24
How does a simple pgp encryption of string work using openpgp.js?
To actually answer your question over suggesting another library, the fix is in changeing the syntax from
to
Then it should works since that method is not async (anymore?)
Here your example modified for symetric encryption (that's why I couldn't use jsencrypt as Nikola suggested: