Openpgpjs: Not able decrypt the encrypted content

Created on 13 Jun 2016  路  8Comments  路  Source: openpgpjs/openpgpjs

Hi

I could able to encrypt the data using pgp public key, but could't decrypt it uisng private key
My sample code is

var openpgp = require('openpgp');

var options = {
userIds: [{ name:'Jon Smith', email:'[email protected]' }], // multiple user IDs
numBits: 2048, // RSA key size
passphrase: 'hello' // protects the private key
};

openpgp.generateKey(options).then(function(key) {
var privkey = key.privateKeyArmored; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
var pubkey = key.publicKeyArmored; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '

plaintext = "hi";
  var pubKeyDE = openpgp.key.readArmored(privkey).keys;
  var privKeyDE = openpgp.key.readArmored(privkey).keys[0];
 // privKeyDE.decrypt(passphrase);
  openpgp.encrypt({
    publicKeys: pubKeyDE,
    data: plaintext
  }).then(function(encrypted) {

    console.info("encrypted", encrypted);
    console.info( openpgp.decrypt({
      privateKey: privKeyDE,
      message: openpgp.message.readArmored(encrypted.data)
    }));
  }).then(function(plaintext) {
    //expect(encrypted.data).to.exist;
    //expect(encrypted.data).to.equal(plaintext);
    //done();
  });

});

privKeyDE.decrypt(passphrase); is showing error.

Could you please help me to decrypt the encrypted content?

Most helpful comment

@jyothisjose Did you read my second comment about the promise? I happen to have the example I tried around, so I'll post it, but it really seems like you didn't read the my comment.

`var openpgp = require('openpgp');

var options = {
userIds: [{ name:'Jon Smith', email:'[email protected]' }], // multiple user IDs
numBits: 2048//, // RSA key size
//passphrase: 'hello' // protects the private key
};

openpgp.generateKey(options).then(function(key) {
var privkey = key.privateKeyArmored; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
var pubkey = key.publicKeyArmored; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '

plaintext = "hi";
var pubKeyDE = openpgp.key.readArmored(pubkey).keys[0];
var privKeyDE = openpgp.key.readArmored(privkey);
privKeyDE.keys[0].decrypt(options.passphrase);

privKeyDE = privKeyDE.keys[0];

return openpgp.encrypt({
publicKeys: pubKeyDE,
data: plaintext
}).then(function(encrypted) {

var msg = openpgp.message.readArmored(encrypted.data);

return msg.decrypt(privKeyDE);

})
})
.then(function (data) { console.log(data.getText()); })
.catch(function (err) { console.error(err); });`

All 8 comments

Can somebody please help to solve the above issue.
Thanks in advance.

It's best to check out the unit tests for working examples.

We tried the test in https://github.com/openpgpjs/openpgpjs/blob/master/test/general/openpgp.js. It returns the encrypted value, but while decrypting we are getting
{ _id: 16,
_state: undefined,
_result: undefined,
_subscribers: [] }

Could you please help us?
Thanks in advance.

Sorry. Don't have the time to debug every user's issue. Currently busy doing customer work.

@jyothisjose @anjaly So, to echo @tanx, please read the docs.

That said, the private key decrypt thing might be a legit bug, as I couldn't get Key.decrypt to work for me either for some reason. I decrypted the private key using:

privKeyDE.keys[0].decrypt(options.passphrase);
privKeyDE = privKeyDE.keys[0];

Secondly, openpgp.decrypt returns a promise, and you are outputting the contents of the promise object rather than the actual result of the decryption.

Once I fixed those things your example worked for me.

@bartbutler
We have given privKeyDE.keys[0].decrypt(options.passphrase);in our example. But still we are facing the same problem (not able to decrypt the encrypted content).

Could you please provide the working copy of our example it will be great help for us.
Thank you.

@jyothisjose Did you read my second comment about the promise? I happen to have the example I tried around, so I'll post it, but it really seems like you didn't read the my comment.

`var openpgp = require('openpgp');

var options = {
userIds: [{ name:'Jon Smith', email:'[email protected]' }], // multiple user IDs
numBits: 2048//, // RSA key size
//passphrase: 'hello' // protects the private key
};

openpgp.generateKey(options).then(function(key) {
var privkey = key.privateKeyArmored; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
var pubkey = key.publicKeyArmored; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '

plaintext = "hi";
var pubKeyDE = openpgp.key.readArmored(pubkey).keys[0];
var privKeyDE = openpgp.key.readArmored(privkey);
privKeyDE.keys[0].decrypt(options.passphrase);

privKeyDE = privKeyDE.keys[0];

return openpgp.encrypt({
publicKeys: pubKeyDE,
data: plaintext
}).then(function(encrypted) {

var msg = openpgp.message.readArmored(encrypted.data);

return msg.decrypt(privKeyDE);

})
})
.then(function (data) { console.log(data.getText()); })
.catch(function (err) { console.error(err); });`

@bartbutler
Its working for us.
Thanks for your valuable information and help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomholub picture tomholub  路  9Comments

CoNETProject picture CoNETProject  路  4Comments

batuhansozer picture batuhansozer  路  7Comments

gquittet picture gquittet  路  5Comments

kingleecha picture kingleecha  路  10Comments