node.js - 从 Node JS Crypto 中的 Salt 和密码创建 16 字节密钥用于 AES 加密
问题描述
我正在尝试使用节点 JS 加密模块匹配在 C# 中实现的 AES 256 CBC 加密。
这是我的 C# 代码
using System;
using System.Security.Cryptography;
using System.Text;
public class Program
{
public static void Main()
{
Console.WriteLine(EncryptExt("Hello World"));
Console.WriteLine(DecryptExt(EncryptExt("Hello World")));
}
public static string EncryptExt(string raw)
{
using (var csp = new AesCryptoServiceProvider())
{
ICryptoTransform e = GetCryptoTransformExt(csp, true);
byte[] inputBuffer = Encoding.UTF8.GetBytes(raw);
byte[] output = e.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
string encrypted = Convert.ToBase64String(output);
return encrypted;
}
}
public static string DecryptExt(string encrypted)
{
using (var csp = new AesCryptoServiceProvider())
{
var d = GetCryptoTransformExt(csp, false);
byte[] output = Convert.FromBase64String(encrypted);
byte[] decryptedOutput = d.TransformFinalBlock(output, 0, output.Length);
string decypted = Encoding.UTF8.GetString(decryptedOutput);
return decypted;
}
}
private static ICryptoTransform GetCryptoTransformExt(AesCryptoServiceProvider csp, bool encrypting)
{
csp.Mode = CipherMode.CBC;
csp.Padding = PaddingMode.PKCS7;
var passWord = Convert.ToString("AvbSkj3BVbf4o6mdlAofDp0/SD0susEWo0pKdmqas");
var salt = Convert.ToString("ABj4PQgf3j5gblQ0iDp0/Gb07ukQWo0a");
String iv = Convert.ToString("aAB1jhPQ89o=f619");
var spec = new Rfc2898DeriveBytes(Encoding.UTF8.GetBytes(passWord), Encoding.UTF8.GetBytes(salt), 65536);
byte[] key = spec.GetBytes(16);
csp.IV = Encoding.UTF8.GetBytes(iv);
csp.Key = key;
if (encrypting)
{
return csp.CreateEncryptor();
}
return csp.CreateDecryptor();
}
}
这是我的 Node JS 实现
const crypto = require('crypto'),
algorithm = 'aes-128-cbc',
password = 'AvbSkj3BVbf4o6mdlAofDp0/SD0susEWo0pKdmqas',
salt = 'ABj4PQgf3j5gblQ0iDp0/Gb07ukQWo0a',
iv = 'aAB1jhPQ89o=f619',
inputEncoding = 'utf8',
outputEncoding = 'base64';
function encrypt(text) {
let cipher = crypto.createCipheriv(algorithm,createHashPassword(), iv);
let encrypted = cipher.update(text, inputEncoding, outputEncoding)
encrypted += cipher.final(outputEncoding);
return encrypted;
}
function createHashPassword(){
let nodeCrypto = crypto.pbkdf2Sync(Buffer.from(password), Buffer.from(salt), 65536, 16, 'sha1');
return nodeCrypto || nodeCrypto.toString('hex');
};
function decrypt(encrypted) {
let decipher = crypto.createDecipheriv(algorithm, Buffer.from(createHashPassword(),"hex"), iv)
let dec = decipher.update(encrypted, outputEncoding, inputEncoding)
dec += decipher.final(inputEncoding);
return dec;
}
console.log(encrypt('Hello World'));
console.log(decrypt(encrypt('Hello World')));
这两个选项的加密数据都不同,因此无法解决。
到目前为止,我所看到的是,
- node crypto createCipheriv 方法只需要 32 字节的缓冲区,如果我传递一个 16 字节的缓冲区,它会说,长度无效。
- 如果我将 16 字节密钥转换为十六进制编码字符串,则加密值会更改并且与 C# 实现不匹配。
- 我无法更改 C# 实现,因为它已经在生产中并已被多个应用程序使用。
- 因此,从节点 js 中的盐和密码生成密钥似乎存在问题,与 C# 中所做的相匹配,我无法弄清楚。
代码可以在以下链接中测试:C#实现:https ://dotnetfiddle.net/bClrpW 节点JS实现:https ://runkit.com/a-vi-nash/5c062544509d8200156f6111
解决方案
似乎您正在代码中创建一个AES-128
实例C#
,因为您使用的是 16 字节的 keylen。
AES-256
keylen 是 32 字节,而不是 16 字节。
代码中的错误:
- 由于您为 key in 设置了 16 个字节
C#
,因此它使用AES-128
,而不是AES-256
。因此,您需要将生成的密钥更改node.js
为AES-128
或将双方更改为 32 字节。 - 由于您使用的是文本字符串盐和密码(未
base64
编码),因此您的node.js
一方使用了不正确的pbkdf2Sync
参数。 IV
len 用于AES
16 字节的算法,不能使用较短的。
既然你想要AES-256
这里是你改变了双方:
C#
边:
String iv = Convert.ToString("SOME_IV_SOME_IV_"); // 16 bytes IV
....
byte[] key = spec.GetBytes(32); // 32 bytes key
node.js
边:
iv = 'SOME_IV_SOME_IV_' // 16 bytes IV similar to C#
...
// Bugs in this function
function createHashPassword(){
// Change parameters to `base64` only if salt and password are base64. it may be true for salt, but it is can rarely be correct for password.
let nodeCrypto = crypto.pbkdf2Sync(Buffer.from(password), Buffer.from(salt), 65536, 32, 'sha1');
return nodeCrypto;
};
重要笔记:
- 请记住,
IV
必须将其选为随机缓冲区(既不是固定的也不是文本),并且由于您似乎是通过网络发送它,因此您也需要IV
与它一起发送。 SALT
必须是随机缓冲区(不是文本)并在两侧固定。- 我建议
PBKDF2
至少使用超过 100000 次迭代。
推荐阅读
- c++ - OpenCV C++ imgdecode 在 OSX 上生成异常
- node.js - 总是返回空数组,尽管它在 Shasta Network 上显示成功交易
- django - 如何在 UpdateView Django 中更新特定表单字段中的数据
- c# - 我的设置编辑器在我的项目属性菜单中去了哪里?
- javascript - 用于从数组打印排列的递归算法
- pandas - 添加返回后数据框结果更改为零
- cmake - 从 CMakeLists.txt 中将 CMAKE_MAKE_PROGRAM 设置为 mingw32-make
- javascript - 用 JavaScript 中的正则表达式比较两个字符串
- sql-server - 从 Google Apps 脚本连接到 Google Cloud SQL 上的 SQL Server 数据库时出错:
- azure - 使用在 ASP.NET CORE 中开发的 WebApp 从 Azure Active Directory 获取用户的电子邮件