【问题标题】:Error from openpgpjs example: 'openpgp.encrypt is not a function'来自 openpgpjs 示例的错误:“openpgp.encrypt 不是函数”
【发布时间】:2016-08-16 18:51:23
【问题描述】:

我在尝试 openpgp.js github 页面上给出的示例时遇到“openpgp.encrypt 不是函数”错误:https://github.com/openpgpjs/openpgpjs/blob/master/README.md#getting-started

按照提供的示例并在使用 npm install --save openpgp 安装后

然后我尝试了标记为“设置”和“使用密码加密和解密 Uint8Array 数据”的 sn-ps

// Set up

var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp

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

openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard)

//  Encrypt and decrypt Uint8Array data with a password

var options, encrypted;

options = {
    data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array (or String)
    passwords: ['secret stuff'],              // multiple passwords possible
    armor: false                              // don't ASCII armor (for Uint8Array output)
};

openpgp.encrypt(options).then(function(ciphertext) {
    encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array
});

options = {
    message: openpgp.message.read(encrypted), // parse encrypted bytes
    password: 'secret stuff',                 // decrypt with password
    format: 'binary'                          // output as Uint8Array
};

openpgp.decrypt(options).then(function(plaintext) {
    return plaintext.data // Uint8Array([0x01, 0x01, 0x01])
});

这是错误:

TypeError: openpgp.encrypt is not a function
    at Object.<anonymous> (/home/tgrego/1/Src/Example/Javascript/Node.js/OpenPgp/openpgpExamp.js:20:9)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

【问题讨论】:

  • 在调试器中检查 openpgp 显示 .encryptMessage 是 openpgp 的方法,但不是 .encrypt。但是尝试使用 encryptMessage 替换 encrypt 的代码仍然失败。
  • 也许这更适合作为 GitHub 上的问题。这在我看来就像有人在修改代码后忘记更新文档。
  • 感谢 Artjom。我刚刚在 GitHub 存储库中提交了问题。

标签: javascript encryption npm openpgp openpgp.js


【解决方案1】:

使用npm install --save openpgp@2.2.1 安装解决了版本问题。

另外,为了使示例工作,解密部分需要嵌入到加密部分的回调函数中,如下所示:

var openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp

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

openpgp.config.aead_protect = true // activate fast AES-GCM mode (not yet OpenPGP standard)


var options, encrypted;

options = {
    data: new Uint8Array([0x01, 0x01, 0x01]), // input as Uint8Array (or String)
    passwords: ['secret stuff'],              // multiple passwords possible
    armor: false                              // don't ASCII armor (for Uint8Array output)
};

openpgp.encrypt(options).then(function(ciphertext) {
    encrypted = ciphertext.message.packets.write(); // get raw encrypted packets as Uint8Array

    options = {
        message: openpgp.message.read(encrypted), // parse encrypted bytes
        password: 'secret stuff',                 // decrypt with password
        format: 'binary'                          // output as Uint8Array
    };

    openpgp.decrypt(options).then(function(plaintext) {
        console.log (plaintext.data);
        return plaintext.data // Uint8Array([0x01, 0x01, 0x01])
    });

});

【讨论】:

    猜你喜欢
    • 2018-04-04
    • 2014-07-23
    • 1970-01-01
    • 2021-06-06
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多