Cannot decrypt ReadStream from file #1305
-
Hello. I am trying to symmetrically decrypt a file using a fs.ReadStream Here is my encryption and decryption: async function encryptFileSym(fileBuffer: fs.ReadStream, secret: string) {
const message = await openpgp.createMessage({ text: fileBuffer });
return await openpgp.encrypt({
message,
passwords: secret,
armor: false,
});
}
async function decryptFileSym(fileBuffer: fs.ReadStream, secret: string) {
const message = await openpgp.readMessage({ armoredMessage: fileBuffer });
return await openpgp.decrypt({
message,
passwords: secret,
});
}
async function encryptFileAndCreateReplacement(filename: string, secret: string) {
const newFileName = filename + ".enc";
const matchingKeyName = filename + ".key.enc";
const oldFileBuff = fs.createReadStream(filename);
const encryptedData = await encryptFileSym(oldFileBuff, secret);
const newFileStream = fs.createWriteStream(newFileName);
encryptedData.pipe(newFileStream);
}
async function decryptFileAndCreateReplacement(filename: string, secret: string) {
const newFileName = filename.replace(".enc", "plswork");
//delete the old file
//delete the encrypted secret file
const oldFileBuff = fs.createReadStream(filename);
const decryptedData = await decryptFileSym(oldFileBuff, secret);
const newFileStream = fs.createWriteStream(newFileName);
decryptedData.data.pipe(newFileStream);
}
openpgp.config.allowUnauthenticatedStream = true;
(async function () {
await encryptFileAndCreateReplacement("secret", "helloworld");
await decryptFileAndCreateReplacement("secret.enc", "helloworld");
})(); It throws a In essence, I am writing the encrypted to a file, and then trying to read that file and decrypt. Is this a bug, or am I missing something? I have also changed Thank you for your continued support, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hey 👋 Probably you have to pass |
Beta Was this translation helpful? Give feedback.
-
Hey! I have found the solution. When decrypting Thank you for your support, I hope this thread helps someone who might be looking! 🥳 |
Beta Was this translation helpful? Give feedback.
-
Hello, is it possible to use this code to encrypt/decrypt ZIP file? As I have tested it, but without success :(, thanks! |
Beta Was this translation helpful? Give feedback.
Hey! I have found the solution.
When decrypting
binary
files,{ format: "binary" }
needs to be added or you will lose carriage returns, etc.Thank you for your support, I hope this thread helps someone who might be looking! 🥳