我正在尝试使用来自openpgp.js的使用PGP密钥示例对字符串数据进行加密和解密,但是我很难使它在Firefox中运行。 openpgp.js doc

我创建一个密钥对。

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

});

我得到控制的密钥用于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();

我在浏览器控制台中收到以下错误:
SyntaxError: missing } after property list[Learn More] openpgp testing.html:153:27 note: { opened at line 152, column 24
使用openpgp.js如何对字符串进行简单的pgp加密?

最佳答案

为了通过建议另一个库来实际回答您的问题,解决方法是将语法从

message: await openpgp.message.readArmored(encrypted),


message: openpgp.message.readArmored(encrypted),

然后应该可以正常工作,因为该方法不是异步的(不再吗?)

在这里,您的示例针对对称加密进行了修改(这就是为什么我不能像Nikola所建议的那样使用jsencrypt:
 <script lang="JavaScript" src="openpgp.js"></script>
 <script lang="JavaScript">
    const options = {
        message : window.openpgp.message.fromText('Hello, World!'),
        passwords : ['pw'],
        armor : false
    }

    window.openpgp.encrypt(options).then(ciphertext => {
        encrypted = ciphertext.message
        return encrypted
    }).then(encrypted => {
        const options = {
            message : encrypted,
            passwords : ['pw']
        }
        window.openpgp.decrypt(options).then(plaintext => {
            console.log(plaintext.data)
            alert(plaintext.data)
            return plaintext.data
        })
    })
</script>

09-17 13:10