【发布时间】:2019-02-24 16:53:10
【问题描述】:
我正在使用 node 进行 OpenPGP 加密。 This 是我的参考库。当我运行演示时,出现以下错误。
openpgpTest.js:32 message: await openpgp.message.readArmored(encrypted), // 解析装甲消息 ^^^^^^^
SyntaxError:意外的标识符 在 createScript (vm.js:80:10) 在 Object.runInThisContext (vm.js:139:10) 在 Module._compile (module.js:599:28) 在 Object.Module._extensions..js (module.js:646:10) 在 Module.load (module.js:554:32) 在 tryModuleLoad (module.js:497:12) 在 Function.Module._load (module.js:489:3) 在 Function.Module.runMain (module.js:676:10) 启动时(bootstrap_node.js:187:16) 在 bootstrap_node.js:608:3
下面是我的代码
var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp
openpgp.initWorker({ path:'openpgp.worker.js' })
// put keys in backtick (``) to avoid errors caused by spaces or tabs
const pubkey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
...
-----END PGP PUBLIC KEY BLOCK-----`
const privkey = `-----BEGIN PGP PRIVATE KEY BLOCK-----
...
-----END PGP PRIVATE KEY BLOCK-----` //encrypted private key
const passphrase = `yourPassphrase` //what the privKey is encrypted with
const encryptDecryptFunction = async() => {
console.log("init",openpgp)
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)
}
console.log("init",options)
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()
有人知道我为什么会收到这个错误吗?我在 Windows 系统上的 cmd 中运行此代码。
【问题讨论】:
-
不,当我尝试在浏览器中运行它时(没有 node.js),我也不断收到错误消息。来自 openpgp.js/protonmail 的人能否提供更详细的规范?
标签: javascript node.js pgp