在 Node.JS Crypto (aes-256-cbc) 中加密,然后在 OpenSSL CLI 中解密

格斯·波林斯基

我正在 Node.js 中加密一个文件,并尝试使用 OpenSSL 命令行进行解密。我是一名经验丰富的开发人员,但我并没有完全受过加密方面的教育。我基本上只需要一种以编程方式加密文件的好方法,并且能够在以后使用命令行对其进行解密。

Node.JS 代码:

const crypto = require('crypto');
const fs = require('fs');
const { pipeline } = require('stream');

const algorithm = 'aes-256-cbc';
const password = 'ABC123';


crypto.scrypt(password, 'salt', 32, (err, key) => {
    if (err) throw err;
    // Then, we'll generate a random initialization vector
    crypto.randomFill(new Uint8Array(16), (err, iv) => {
        if (err) throw err;

        const cipher = crypto.createCipheriv(algorithm, key, iv);


        const input = fs.createReadStream('test.txt');
        const output = fs.createWriteStream('test.enc');

        pipeline(input, cipher, output, (err) => {
            if (err) throw err;
        });
    });
});

我的 CLI 命令:

openssl enc -aes-256-cbc -nosalt -d -in test.enc -out test2.txt

这给了我:

bad decrypt
红宝石

我相信bad decrypt意味着 openssl 不理解加密密钥

这是发生的事情。

  1. 作为第一步,crypto使用提供的密码和盐生成加密密钥:crypto.scrypt(password, 'salt', 32,...
  2. 第二步,它生成一个初始化向量 (iv): crypto.randomFill(new Uint8Array(16)
  3. 最后,它使用生成的密钥和 iv 来创建密码并实际加密文件: pipeline(input, cipher, output,

因此,要使用 openssl 解密此文件,需要提供密钥和 iv。

这是解密它的一种方法。请参阅底部添加的生成 openssl 解密命令的代码:

const crypto = require('crypto');
const fs = require('fs');
const { pipeline } = require('stream');

const algorithm = 'aes-256-cbc';
const password = 'ABC123';

crypto.scrypt(password, 'salt', 32, {}, (err, key) => {
  if (err) {
    throw err;
  }
  // Then, we'll generate a random initialization vector
  crypto.randomFill(new Uint8Array(16), (err, iv) => {
    if (err) {
      throw err;
    }

    const cipher = crypto.createCipheriv(algorithm, key, iv);


    const input = fs.createReadStream('test.txt');
    const output = fs.createWriteStream('test.enc');

    pipeline(input, cipher, output, (err) => {
      if (err) {
        throw err;
      }
    });

    // Generate openssl decrypt command
    const hexKey = key.toString('hex');
    const hexIv = Buffer.from(iv).toString('hex');
    console.log(`openssl enc -aes-256-cbc -d -in test.enc -out test2.txt -K ${hexKey} -iv ${hexIv}`);
  });
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过crypto-js解密AES 256 CBC

AES-256-CBC Mcrypt-PHP解密和Crypto-JS加密

如何使用OpenSSL加密/解密AES-256 CBC?

使用AES 256 CTR在Node JS中加密和在Golang中解密

带有OpenSSL的AES(AES-CBC-256)加密/解密预期输出被截断

在 Nodejs 中加密/解密 aes256cbc

Node.js AES-256-CBC加密问题

Node.js中的crypto模块与AES256解密中的CryptoJS的区别

Flutter / Dart AES-256-CBC从PHP中的加密解密

使用Aes-cbc-256解密使用openssl加密的文件

无法解密在服务器端使用OpenSSL AES 256 CBC加密的文件

从OpenSSL AES解密python中的AES CBC

在node.js中解密使用A128CBC-HS256加密的JWT

错误的加密(QT c++ OpenSSL AES 256 CBC)

如何用Crypto-JS解密AES 128-CBC?

寻找Java实现来解密使用openssl -aes-256-cbc -a -salt命令加密的消息吗?

通过C中的EVP API的OpenSSL AES 256 CBC

QT:AES-256-CBC 根据 PHP 代码在 C++ 中加密/解密

AES 256 位 CBC PKCS#5 在 Python 中加密/解密

AES-256-CBC 用 PHP 加密并用 Java 解密

使用OpenSSL进行AES_128_CBC加密/解密

AES-256 CBC加密在Ruby / PHP中成功,但是使用CryptoJS解密失败

如何在Linux中不创建未加密文件的情况下读取openssl aes-256-cbc加密文件?

Node.js中的PHP aes-256-cbc mcrypt_decrypt()等效项

Node.js / Golang AES 256解密

来自 BouncyCastle 的 AES256-CBC Ciphertext 在 BC 中解密,但 PHP openssl_decrypt 在输入相同的情况下失败

如何通过openssl库中的密码而不是密钥和iv通过AES-256-CBC加密文件?

OpenSSL -aes-256-cbc解密行为在1.0.2o和1.1.0g之间更改

使用Visual Studio和Openssl的AES 256 CTR加密/解密