Reputation: 3538
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?
Upvotes: 4
Views: 8834
Reputation: 79
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-----"
Upvotes: 7
Reputation: 36
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);
}
//...
};
Upvotes: 1