我正在尝试使用节点 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