Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misformed armored text error when loading key using OpenPGP

I'm trying to decrypt a file encrypted with GnuPG 2.2.28 using the Node.js package openPGP, but I can't even seem to load the private key to start with and I get the following error:

{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "Error: Misformed armored text",
  "trace": [
    "Runtime.UnhandledPromiseRejection: Error: Misformed armored text",
    "    at process.<anonymous> (/var/runtime/index.js:35:15)",
    "    at process.emit (events.js:314:20)",
    "    at process.EventEmitter.emit (domain.js:483:12)",
    "    at processPromiseRejections (internal/process/promises.js:209:33)",
    "    at processTicksAndRejections (internal/process/task_queues.js:98:32)"
  ]
}

In my code it looks like I've provided the armored key correctly, using backticks to enclose the key:

const openpgp = require('openpgp');

async() => {

    try {

        const passphrase = `changeit`;

        const priv_key = `-----BEGIN PGP PRIVATE KEY BLOCK-----
    
    lQPGBGEcehABCAC2/ws+pKo/9DB2JgQI3IXUXtj666KfHiFF2GjfEY5FvWIqm7Cq
    MneNHyp+HfgjI6L0C1UAhUtUZaHFpKYfCbKoXH4Odwvor8f1RaxA7/IdvY+JJdx2
    2tv/ZJdAP35XXRp0XrHPQIyEnTlvWPTPNFKb3kRaEFJnJfbCGSfocSWq9mrPc1J3
    ...
    -----END PGP PRIVATE KEY BLOCK-----`;

        const privateKey = await openpgp.decryptKey({
            privateKey: await openpgp.readPrivateKey({
                armoredKey: priv_key
            }),
            passphrase
        });

    } catch (e) {
        console.log("ERROR: Unexpected error in PGP decryption", e.stack);
    }

    //...
}

Has any one else encountered a similar problem?

like image 517
craigcaulfield Avatar asked Oct 21 '25 16:10

craigcaulfield


2 Answers

It's probably because of extra spaces between lines. There should be just line breaks \n, but no space between these lines.

In your case you can concatenate all lines to such string as follows:

"-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nlQPGBGEcehABCAC2/ws+pKo/9DB2JgQI3IXUXtj666KfHiFF2GjfEY5FvWIqm7Cq\n...\n-----END PGP PRIVATE KEY BLOCK-----"
like image 154
Anastasia Shabalina Avatar answered Oct 23 '25 05:10

Anastasia Shabalina


I just ran into the same issue and the only solution I found was to rollback to openpgp 4.10.10 (https://www.npmjs.com/package/openpgp/v/4.10.10) and update my code to use the older syntax.

const openpgp = require('openpgp');

async () => {
  try {
    const passphrase = `changeit`;

    const priv_key = `-----BEGIN PGP PRIVATE KEY BLOCK-----
    
    lQPGBGEcehABCAC2/ws+pKo/9DB2JgQI3IXUXtj666KfHiFF2GjfEY5FvWIqm7Cq
    MneNHyp+HfgjI6L0C1UAhUtUZaHFpKYfCbKoXH4Odwvor8f1RaxA7/IdvY+JJdx2
    2tv/ZJdAP35XXRp0XrHPQIyEnTlvWPTPNFKb3kRaEFJnJfbCGSfocSWq9mrPc1J3
    ...
    -----END PGP PRIVATE KEY BLOCK-----`;

    const privateKey = (await openpgp.key.readArmored([priv_key])).keys[0];
    await privateKey.decrypt(passphrase);
  } catch (e) {
    console.log('ERROR: Unexpected error in PGP decryption', e.stack);
  }

  //...
};
like image 33
Corc Avatar answered Oct 23 '25 07:10

Corc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!