node.js - Node js - 加密和解密文件

标签 node.js encryption

I want to encrypt File on client side and send it to server side and decrypt

但是当我使用内置的 Node js crypto我收到错误

客户端.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

//接收数据

//在这一侧接收文件后,我运行解密脚本

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)

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

**在服务器端接收数据也没有问题,只是解密会产生问题,不知道为什么**

Anything else you want to know about my code please tell

Using Node v10.6.0

最佳答案

尝试使用初始化向量和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);
});

关于node.js - Node js - 加密和解密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52550132/

相关文章:

javascript - Node - 从等待结果返回内联中选择一个变量

encryption - 将 AWS 凭证作为用户数据传递给 EC2 实例的最佳方法是什么?

javascript - 尝试了解如何将 Jade 与 Node.js 结合使用

node.js - 在node.js gm上绘制西里尔文字

Java:加密.jar包内的JPG文件

Java Crypto Api - 如何选择密码提供者

java - Android 中的 Base64 加密

php - 如何使用 PHP 管理服务器运行时的 secret ?

javascript - 无法从 React 客户端向 Node.js websocket 服务器发送消息

node.js - 如何在azure虚拟机上托管nodejs api应用程序?