Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error encrypting message: Private key is not decrypted

I was trying to encrypt and sign a message using OpenPgpjs.

But I keep getting this error "Error encrypting message: Private key is not decrypted"

This is what I tried:

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;
});
like image 696
Uma Kanth Avatar asked Jun 07 '16 11:06

Uma Kanth


1 Answers

You need to decrypt your private key if you want to sign:

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);
});
like image 199
Mark Avatar answered Oct 01 '22 12:10

Mark