如何在openpgp.js中使用公钥加密?

时间:2018-11-29 20:07:22

标签: javascript public-key-encryption pgp openpgp openpgp.js

我尝试用openpgp.js加密数据 不幸的是,该文档似乎在多个站点之间缺乏一致性。无法从其github复制并粘贴提供的用于使用PGP公钥加密的代码。

<script>
const openpgp = window.openpgp // use as CommonJS, AMD, ES6 module or via      window.openpgp

openpgp.initWorker({     path:'openpgp.worker.js' }) // set the relative web worker path

var options = {
userIds: [{ name:'Jon Smith', email:'jon@example.com' }], // multiple user IDs
curve: "ed25519",                                         // ECC curve name
passphrase: 'super long and hard to guess secret'         // protects the private key
 };  
 openpgp.generateKey(options).
  then(function(key) {
var privkey = key.privateKeyArmored; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
var pubkey = key.publicKeyArmored;   // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
console.log(pubkey);
console.log(privkey);
var revocationSignature = key.revocationSignature; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
});

// put keys in backtick (``) to avoid errors caused by spaces or tabs
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK----- Version: OpenPGP.js v4.2.1 Comment: https://openpgpjs.org xjMEXAArxRYJKwYBBAHaRw8BAQdAIgQN1lj9GOGVFoPzPzYjkz1iO3jHajwA HKauyPaf3RbNG0pvbiBTbWl0aCA8am9uQGV4YW1wbGUuY29tPsJ3BBAWCgAf BQJcACvFBgsJBwgDAgQVCAoCAxYCAQIZAQIbAwIeAQAKCRBrvmKohFbjYggf AP403aIEJQRe//fsqsUDu2Ur34AL/c1QBfsIU/Zrkrxw/gEAvV6/gpI1Y1aL dBgGnDgqhPUbRlWNan9k8498M0VAuwbOOARcACvFEgorBgEEAZdVAQUBAQdA gvaGU/GRRU8bp3fc+GO8JcRhMBzD4T8dO9t+hHZc81YDAQgHwmEEGBYIAAkF AlwAK8UCGwwACgkQa75iqIRW42LnUAEA2qoQAqSOkHoNmi1E+78VaTxe0xO4 YymcqfBXVcqTGPoA/RgLuEe7mF35dYRWFOqznNfudL5lhsElmsePQa84q70D =crPX -----END PGP PUBLIC KEY BLOCK----- `;

const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK----- Version: OpenPGP.js v4.2.1 Comment: https://openpgpjs.org xYYEXAArxRYJKwYBBAHaRw8BAQdAIgQN1lj9GOGVFoPzPzYjkz1iO3jHajwA HKauyPaf3Rb+CQMImFDdXFcbpXDgsWHIZjzfQe+AMwiANg70YZQgfXgR6VAz oMcVTMI9px+lJAGFmIi6upQDIRUqt4GOffqrtDhpLyCTG9snNWaWVly3hQ/N rM0bSm9uIFNtaXRoIDxqb25AZXhhbXBsZS5jb20+wncEEBYKAB8FAlwAK8UG CwkHCAMCBBUICgIDFgIBAhkBAhsDAh4BAAoJEGu+YqiEVuNiCB8A/jTdogQl BF7/9+yqxQO7ZSvfgAv9zVAF+whT9muSvHD+AQC9Xr+CkjVjVot0GAacOCqE 9RtGVY1qf2Tzj3wzRUC7BseLBFwAK8USCisGAQQBl1UBBQEBB0CC9oZT8ZFF Txund9z4Y7wlxGEwHMPhPx07236EdlzzVgMBCAf+CQMIpu98k4tMseDgwbok uW59BZCFpdycpzvSYohdtAQb9bYAdnjV4GNENWlHj4rMEhZVDZ6s2sQbc1gW w66RkRGJeku5e8KQv8TzNQoIoTmlKcJhBBgWCAAJBQJcACvFAhsMAAoJEGu+ YqiEVuNi51ABANqqEAKkjpB6DZotRPu/FWk8XtMTuGMpnKnwV1XKkxj6AP0Y C7hHu5hd+XWEVhTqs5zX7nS+ZYbBJZrHj0GvOKu9Aw== =Ub8c -----END PGP PRIVATE KEY BLOCK-----`;  

//encrypted private key
const passphrase = 'yourPassphrase';   

//what the privKey is encrypted with
const message = 'Hello, World!';    //    input as Message object
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] openpgptest2 copy.html:103:27 note: { opened at line 102, column 24

如何使用openpgp.js进行简单加密?

0 个答案:

没有答案