【问题标题】:Node js - Encrypt and Decrypt FileNode js - 加密和解密文件
【发布时间】:2019-03-04 03:28:01
【问题描述】:

我想在客户端加密文件并将其发送到服务器端并 解密

但是当我使用内置的节点 js crypto 时,我得到了错误

client.js

const crypto = require('crypto');
const cipher = crypto.createCipher('aes192', 'a password');
const encInput = fs.createReadStream("abc.txt");
const encOutput = fs.createWriteStream("abc.txt.enc");

       encInput.pipe(cipher).pipe(encOutput).on('close', function() {
         // DATA SENT TO SERVER SIDE
         //USING PIPELINE TO SEND DATA TO SERVER
       });

这部分做得很完美,它在客户端创建一个加密文件并将其发送到服务器端

服务器.js

//receive Data

//AFTER RECEIVING FILE ON this side I run decrypt script

const crypto = require('crypto');
const decipher = crypto.createDecipher('aes192', 'a password');

const decInput = fs.createReadStream("abc.txt.enc");
const decOutput = fs.createWriteStream("abc.txt");
decInput.pipe(decipher).pipe(decOutput);

这会报错

Error: error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length
    at Decipher._flush (internal/crypto/cipher.js:141:28)
    at Decipher.prefinish (_stream_transform.js:141:10)
    at Decipher.emit (events.js:182:13)
    at prefinish (_stream_writable.js:630:14)
    at finishMaybe (_stream_writable.js:638:5)
    at afterWrite (_stream_writable.js:481:3)
    at onwrite (_stream_writable.js:471:7)
    at Decipher.afterTransform (_stream_transform.js:94:3)
    at Decipher._transform (internal/crypto/cipher.js:136:3)
    at Decipher.Transform._read (_stream_transform.js:190:10)
    Emitted 'error' event at:
    at Decipher.onerror (_stream_readable.js:687:12)
    at Decipher.emit (events.js:182:13)
    at done (_stream_transform.js:208:19)
    at _flush (_stream_transform.js:142:7)
    at Decipher._flush (internal/crypto/cipher.js:143:5)
    at Decipher.prefinish (_stream_transform.js:141:10)
    [... lines matching original stack trace ...]
    at afterWrite (_stream_writable.js:481:3)

我知道客户端没有问题,它正在使用管道套接字完美发送数据

**在服务器端接收数据也没有问题,只是解密造成了问题,我知道为什么**

关于我的代码你还想知道什么

使用节点 v10.6.0

【问题讨论】:

  • 上传后在客户端和服务器上比较abc.txt.enc文件。他们平等吗?两个文件编码相同?
  • @Lemix 是的,两者都是一样的......我现在看到的是在服务器端它完美地解密了文件但之后给出了这个错误......
  • 我现在发现的是,当我用相同的代码解密在客户端制作的文件时,它工作正常,但如果我用相同的代码解密在服务器端收到的文件,它会给我这个错误跨度>
  • 尝试使用初始化向量(createCipherivcreateDecipheriv 方法)。我的答案中的示例。
  • 我重现了类似的错误。我设法通过使用 base64 编码来解决它。

标签: node.js encryption


【解决方案1】:

尝试使用初始化向量和base64文件存储格式:

const crypto = require('crypto');
const path = require('path');
const fs = require('fs');
const base64 = require('base64-stream');
const iv = new Buffer('1065faf25ac8560968c58ce6dc0ae36f', 'hex'); // 16 byte iv
const kk = new Buffer('84521db468d282c4ce21cdde65e508ce3d1924d1be5c4754', 'hex'); // 24 byte key (192 bits)
const cipher = crypto.createCipheriv('aes192', kk, iv, { encoding: 'base64' });
const encInput = fs.createReadStream(path.join(__dirname, "abc.txt"));
const encOutput = fs.createWriteStream(path.join(__dirname, "abc.txt.enc"));

encInput.pipe(cipher).pipe(encOutput).on('close', function () {
    const decipher = crypto.createDecipheriv('aes192', kk, iv);
    const decInput = fs.createReadStream(path.join(__dirname, "abc.txt.enc"));
    const decOutput = fs.createWriteStream(path.join(__dirname, "abc.txt.dec"));
    decInput.pipe(base64.decode()).pipe(decipher).pipe(decOutput);
});

【讨论】:

  • 仍然相同的错误...我上面发布的代码在解码我发送到服务器端的文件时出现问题,如果我在同一侧进行加密和解密,它不会给我任何错误但是当我加密时客户端的文件并将其发送到服务器并在服务器端解密它会产生错误....虽然它解密文件并且它工作正常但给我错误退出服务器文件并且我无法继续处理更多的东西我想在下面做...我尝试了一个但同样的问题...感谢您的努力
  • @NeerajWalia 在我的示例中,如果我在加密时使用encode: utf8,我会收到您的错误。结果,使用指定编码创建的文件与未指定编码的文件相同,但会出错。也许这些信息会有所帮助。
  • 当我完全使用您的代码时,它工作正常,但您在同一个文件中执行 enc 和 dec 假设 client.js ... 但我想将文件从客户端发送到服务器,我正在做使用管道,所以我想在客户端使用 enc 并在服务器端使用流、管道、套接字发送文件 dec ...
猜你喜欢
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2021-10-20
相关资源
最近更新 更多