Openpgpjs: using openpgp to decrypt the csv file encrypted with gpg4win-3.1.2

Created on 17 Aug 2018  路  3Comments  路  Source: openpgpjs/openpgpjs

I have generated key pair using gpg4win-3.1.2.
I have both public and private key with pass-phrase.

I created a csv file and encrypted it using kleopatra tool.

I am able to decrypt the file using node gpg library but it uses binary to decrypt.

can we use openpgp to decrypt the same file?

Most helpful comment

openpgp.message.readArmored parses armored message data, which is a string beginning with "-----BEGIN PGP MESSAGE-----".

To parse binary data into a message object, you will need to use openpgp.message.read instead.

All 3 comments

You should be able to, yes. Does it not work?

Yes. Its not working.

Following code ->

const path = require('path');
const fs = require('fs');
const openpgp = require('openpgp');

const privkey = fs.readFileSync(path.join(__dirname, 'private.asc'), 'utf8');
const pubkey = fs.readFileSync(path.join(__dirname, 'public.asc'), 'utf8');
const encrypted = fs.readFileSync(path.join(__dirname, 'test.csv.gpg'),'utf8');

const passphrase = test;

const encryptDecryptFunction = async() => {

const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0]
await privKeyObj.decrypt(passphrase)

      const options = {
        message: await openpgp.message.readArmored(encrypted),    // parse armored message
        publicKeys: (await openpgp.key.readArmored(pubkey)).keys, // for verification (optional)
        privateKeys: [privKeyObj]                                 // for decryption
    }

    openpgp.decrypt(options).then(plaintext => {
        console.log(plaintext.data)
        return plaintext.data
    }).catch(err => {
        console.log(err);
    })

}

encryptDecryptFunction();


Has error -> UnhandledPromiseRejectionWarning: Error: Misformed armored text

openpgp.message.readArmored parses armored message data, which is a string beginning with "-----BEGIN PGP MESSAGE-----".

To parse binary data into a message object, you will need to use openpgp.message.read instead.

Was this page helpful?
0 / 5 - 0 ratings