2016-06-07 55 views
3

我試圖使用OpenPgpjs對消息進行加密和簽名。加密消息時出錯:私鑰未解密

,但我不斷收到此錯誤「錯誤加密消息:私鑰是不能解密」

這是我的嘗試:

var openpgp = require('openpgp'); 

var publicKey = [].join("\n"); //This has the complete key. Removed for representation 
var privateKey = [].join("\n"); //This has the complete key. Removed for representation 
var publicKeys = openpgp.key.readArmored(publicKey).keys; 
var privateKeys = openpgp.key.readArmored(privateKey).keys; 

encryptionOptions = { 
    data : 'Example Test', 
    publicKeys : publicKeys, 
    privateKeys : privateKeys 
}; 

return openpgp.encrypt(encryptionOptions).then(function(ciphertext) { 
    encryptedData = ciphertext.data; 
    console.log(ciphertext); 
    return encryptedData; 
}); 

回答

3

你需要的,如果要簽名解密私鑰:

var pub = openpgp.key.readArmored(publicKey); 
var priv = openpgp.key.readArmored(privateKey); 

// decrypt the private key with password 
var success = priv.keys[0].decrypt('my-secret-password'); 

var options = { 
    data: 'Hello, World!', 
    publicKeys: pub.keys, 
    privateKeys: priv.keys // for signing (optional) 
}; 

openpgp.encrypt(options).then(function(ciphertext) { 
    console.log (ciphertext.data); 
});