Reputation: 13
I'm attempting to decrypt an OpenPGP-encrypted message in a Node.js script using the openpgp library. My script runs into an error where the decryption fails with the message:
Decryption failed completely. Error details: Error decrypting message: No decryption key packets found The message is encrypted with the key 7851C0CAFDBF2903 (RSA 2048), and I have the corresponding private key, which is not encrypted. However, I receive the following GPG warning when I manually try to decrypt the message using the command line:
vbnet Copy code gpg: encrypted with rsa2048 key, ID 7851C0CAFDBF2903, created 2024-10-22 gpg: used key is not marked for encryption use. gpg: WARNING: cipher algorithm CAST5 not found in recipient preferences Here is the code I'm using to try to decrypt the file:
const fs = require('fs');
const openpgp = require('openpgp');
async function decryptFile(encryptedFilePath, privateKeyFilePath, passphrase, outputFilePath) {
try {
const encryptedData = fs.readFileSync(encryptedFilePath, 'utf8');
const privateKeyArmored = fs.readFileSync(privateKeyFilePath, 'utf8');
// Parse the private key
const privateKey = await openpgp.readKey({ armoredKey: privateKeyArmored });
console.log('Private key fingerprint:', privateKey.getFingerprint());
// Decrypt the private key if necessary
let decryptedPrivateKey = privateKey;
if (privateKey.isEncrypted) {
decryptedPrivateKey = await openpgp.decryptKey({
privateKey,
passphrase
});
}
const message = await openpgp.readMessage({ armoredMessage: encryptedData });
const { data: decryptedData } = await openpgp.decrypt({
message,
decryptionKeys: decryptedPrivateKey,
format: 'utf8'
});
// Ensure output directory exists
const outputDir = outputFilePath.substring(0, outputFilePath.lastIndexOf('/'));
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
fs.writeFileSync(outputFilePath, decryptedData, 'utf8');
console.log('Decryption successful! File saved to:', outputFilePath);
} catch (error) {
console.error('Decryption failed:', error.message);
throw error;
}
}
async function main() {
const encryptedFilePath = './Testd/Trade_21112024.asc';
const privateKeyFilePath = './key/private.key';
const passphrase = '';
const outputFilePath = './output/Trade_21112024.csv';
try {
await decryptFile(encryptedFilePath, privateKeyFilePath, passphrase, outputFilePath);
} catch (error) {
console.error('Program failed:', error.message);
}
}
main();
Issues I'm encountering:
Decryption Key Error: The error message No decryption key packets found occurs during the decryption process, even though the private key exists and is accessible.
GPG Warning: The command-line GPG tool provides a warning about the key "not being marked for encryption use" and a missing cipher algorithm CAST5, which is not found in the recipient's preferences.
What I've tried:
I've confirmed that the private key is correct and present in the private.key file. I ensured that the private key is not encrypted (i.e., it does not require a passphrase). I tried using both the primary private key and the subkeys for decryption in my script. I have successfully decrypted the message manually with GPG, but I can't replicate that behavior in the Node.js script. Questions:
Why is the decryption failing with the error No decryption key packets found in Node.js, even though I can decrypt the message manually with GPG? How can I resolve the warning regarding the key "not marked for encryption use" and the missing CAST5 cipher algorithm? Is there something in my Node.js decryption code that I might be overlooking, especially regarding handling subkeys or specific decryption algorithms? Any help or insights would be greatly appreciated!
Upvotes: 1
Views: 193