简体   繁体   English

使用 node.js 加密的图像加密不起作用

[英]Image encryption using node.js crypto does not work

I created an encryption app using node.js crypto.我使用 node.js 加密创建了一个加密应用程序。 But the encryption/decryption seems to only work on.txt files.但加密/解密似乎只适用于 .txt 文件。 I encrypted an image file, but when i decrypted it, it doesnt return the original image.我加密了一个图像文件,但是当我解密它时,它不会返回原始图像。

const crypto = require("crypto");
const fs = require("fs");
//
function encrypt(text, password) {
  let salt = crypto.randomBytes(16).toString("hex");

  let key = crypto.scryptSync(password, salt, 16, { N: 16384 });
  key = key.toString("hex");

  const cipher = crypto.createCipheriv("aes-256-gcm", key, salt);

  const encrypted = cipher.update(text, "utf8", "hex") + cipher.final("hex");
  const tag = cipher.getAuthTag().toString("hex");

  return salt + tag + encrypted;
}
function decrypt(text, password) {
  const salt = text.substr(0, 32);
  const tag = Buffer.from(text.substr(32, 32), "hex");
  const ciphertext = text.substr(64, text.length);

  let key = crypto.scryptSync(password, salt, 16, { N: 16384 });
  key = key.toString("hex");

  const cipher = crypto.createDecipheriv("aes-256-gcm", key, salt);
  cipher.setAuthTag(tag);
  try {
    let decrypted = cipher.update(ciphertext, "hex", "utf8") + cipher.final("utf8");
    return decrypted;
  } catch (e) {
    return true;
  }
}
//
function encryptFile(fileLocation, password) {
  fs.readFile(fileLocation, "utf8", (err, data) => {
    if (err) return console.log(err);
    if (data) {
      const encText = encrypt(data, password);
      fs.writeFileSync(fileLocation, encText);
      console.log("File Encrypted");
    }
  });
}
function decryptFile(fileLocation, password) {
  fs.readFile(fileLocation, "utf8", (err, data) => {
    if (err) {
      throw err;
    } else {
      const decText = decrypt(data, password);
      if (decText === true) {
        console.log("Wrong Password/Salt");
      } else {
        fs.writeFileSync(fileLocation, decText);
        console.log("File Decrypted");
      }
    }
  });
}
//

const fileLocation = "./sample.txt";
// encryptFile(fileLocation, "password");
decryptFile(fileLocation, "password");

I did it on repl so you can have a look - https://replit.com/@sampauls8520/file-enc I have provided an image file as well so that you can encrypt/decrypt that.我在 repl 上做了,所以你可以看看 - https://replit.com/@sampauls8520/file-enc我也提供了一个图像文件,以便你可以加密/解密它。

You can't read binary data (this includes images) using the UTF-8 text encoding, nor write them back.您无法使用 UTF-8 文本编码读取二进制数据(包括图像),也无法将它们写回。

You will need to rework your code so that it doesn't operate on text strings, but with binary buffers.您将需要重新编写代码,以便它不会对文本字符串进行操作,而是使用二进制缓冲区。

IOW:爱荷华州:

  • don't specify an encoding for fs.readFile / fs.writeFile ("If no encoding is specified, then the raw buffer is returned." say the docs)不要为fs.readFile / fs.writeFile指定编码(“如果未指定编码,则返回原始缓冲区。”说文档)
  • don't specify either input or output encoding for cipher.update() / cipher.final()不要为cipher.update() / cipher.final()指定输入或 output 编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM