错误加密消息:privateKey.isPublic不是函数

时间:2018-09-21 09:19:00

标签: javascript node.js openpgp

我正在使用此https://github.com/openpgpjs/openpgpjs库来创建加密的消息。下面是我的代码

 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 ... '
    var revocationSignature = key.revocationSignature; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '

   // console.log(privkey)

const options = {
    message: openpgp.message.fromText('Hello, World!'),       // input as Message object
    publicKeys: pubkey, // for encryption
    privateKeys: privkey                                 // for signing (optional)
}


openpgp.encrypt(options).then(ciphertext => {
    encrypted = ciphertext.data // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
    return encrypted
})
.then(encrypted => {
    console.log(encrypted)
//   decryptmessage(encrypted,privKeyObj).then(_=>{

//   })
}).catch(error=>{
    console.log("encryption error",error)

})

运行此代码时,出现以下错误

  

错误加密消息:privateKey.isPublic不是函数       在C:\ Users \ vinayak.b \ Desktop \ hello \ node_modules \ openpgp \ dist \ openpgp.js:33104:20       在Array.map()       在Message.sign(C:\ Users \ vinayak.b \ Desktop \ hello \ node_modules \ openpgp \ dist \ openpgp.js:33103:55)       在C:\ Users \ vinayak.b \ Desktop \ hello \ node_modules \ openpgp \ dist \ openpgp.js:33811:33       在       在process._tickCallback(内部/进程/next_tick.js:188:7)       在Function.Module.runMain(module.js:678:11)       在启动时(bootstrap_node.js:187:16)       在bootstrap_node.js:608:3

我做错了什么吗?请帮助我。

0 个答案:

没有答案