首页 > 解决方案 > 从 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')));

这两个选项的加密数据都不同,因此无法解决。

到目前为止,我所看到的是,

代码可以在以下链接中测试:C#实现:https ://dotnetfiddle.net/bClrpW 节点JS实现:https ://runkit.com/a-vi-nash/5c062544509d8200156f6111

标签: node.jsencryptionaescryptojsaescryptoserviceprovider

解决方案


似乎您正在代码中创建一个AES-128实例C#,因为您使用的是 16 字节的 keylen。

AES-256keylen 是 32 字节,而不是 16 字节。

代码中的错误:

  1. 由于您为 key in 设置了 16 个字节C#,因此它使用AES-128,而不是AES-256。因此,您需要将生成的密钥更改node.jsAES-128或将双方更改为 32 字节。
  2. 由于您使用的是文本字符串盐和密码(未base64编码),因此您的node.js一方使用了不正确的pbkdf2Sync参数。
  3. IVlen 用于AES16 字节的算法,不能使用较短的。

既然你想要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;
};

重要笔记:

  1. 请记住,IV必须将其选为随机缓冲区(既不是固定的也不是文本),并且由于您似乎是通过网络发送它,因此您也需要IV与它一起发送。
  2. SALT必须是随机缓冲区(不是文本)并在两侧固定。
  3. 我建议PBKDF2至少使用超过 100000 次迭代。

推荐阅读