【问题标题】:Opengpg.js Encrypt/Decrypyt exampleOpenpgp.js 加密/解密示例
【发布时间】:2016-02-28 16:26:02
【问题描述】:

我正在尝试对 opengpg.js 进行简单测试。

我得到以下输出:

Encrypted Message[object Object]

这段代码中的什么导致了这个错误?

var openpgp = require('openpgp');

var options = {
    numBits: 1024,
    userId: 'user@localhost',
    passphrase: 'password'
};

openpgp.generateKeyPair(options).then(function(keypair) {

    var privkey = keypair.privateKeyArmored;
    var pubkey = keypair.publicKeyArmored;

    console.log("Pub key" + pubkey + "\n\n");

    var publicKey = openpgp.key.readArmored(pubkey);
    var pgpMessage = openpgp.encryptMessage(publicKey.keys,"Hello");

    console.log("Encrypted Message" + pgpMessage + "\n\n");

    }).catch(function(error) {
    // failure
    console.log("Error: " + error);
});

【问题讨论】:

  • "这段代码中的什么导致了这个错误?" 什么错误

标签: javascript openpgp.js


【解决方案1】:

我的这个答案是基于你所说的“错误”的假设 “意外的输出”。

openpgp.encryptMessage 返回promise,而不是加密文本。请参阅opengpg.js documentation

从promise中获取值,和其他promise一样:

openpgp.encryptMessage(publicKey.keys,"Hello").then(function(pgpMessage) {
    // success
    console.log("Encrypted Message" + pgpMessage + "\n\n");
}).catch(function(error) {
    // failure
});

【讨论】:

  • 这不再起作用了。该方法已重命名。
  • @codebird456 this 是您指的更新方法吗?我会更新我的答案。看起来他们也改变了参数和返回类型,所以我想确认这是正确的方法。
  • 是的。我很难正确理解它,因为很多事情都发生了变化。似乎应该创建一个选项对象: var options2 = { message: openpgp.message.fromText('hallooo'), // 输入为消息对象 publicKeys: publicKey, // publicKeys: openpgp.key.readArmored(publicKeyAliceArmored ).keys, // 密码: ['secret stuff'], // 多个密码可能 armour: true // 不使用 ASCII armour (用于 Uint8Array 输出) };
猜你喜欢
  • 2019-03-04
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 2019-07-14
  • 2017-09-18
  • 2021-02-20
  • 1970-01-01
  • 2021-01-22
相关资源
最近更新 更多