将C#AES加密移植到Node.js的Javascript

时间:2018-01-23 04:57:23

标签: javascript c# node.js encryption aes

寻求帮助使用crypto或等效模块将C#代码移植到Node.js中。

private string password="FlU4c8yQKLkYuFwsgyU4LFeIf7m3Qwy+poMBdULEMqw=";
private byte[] salt = Encoding.ASCII.GetBytes("##oEDA102ExChAnGe99#$#");
Aes encryptor = Aes.Create();
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password, salt);
string pdbStr = Convert.ToBase64String(pdb.GetBytes(32));
Console.WriteLine(pdbStr);
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);

尝试移植到javascript中,如下所示,但结果值不一样。

var password = "FlU4c8yQKLkYuFwsgyU4LFeIf7m3Qwy+poMBdULEMqw=";
var salt = "##oEDA102ExChAnGe99#$#";
var pdbBytes = crypto.pbkdf2Sync(Buffer.from(password, 'base64'), new Buffer(salt, 'base64'), 1000, 32);
var pdbStr = new Buffer(pdbBytes).toString('base64')
console.log("pdbStr", pdbStr);

控制台输出: C# - GZlqgdLbMQ753dTmx1nlJ6HgdabTjW1CeCSoIYkLM4E = JS - tuDsZJEEwxyXP7RvuYVxGmDy20AvMJAqkLoXX78sEU8 =

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

这些代码部分产生相同的结果。

但我没有得到'GZlqgdLbMQ753dTmx1nlJ6HgdabTjW1CeCSoIYkLM4E ='来自c#的结果。

<强> c#中

    byte[] password= Convert.FromBase64String("FlU4c8yQKLkYuFwsgyU4LFeIf7m3Qwy+poMBdULEMqw=");
    byte[] salt = Encoding.ASCII.GetBytes("##oEDA102ExChAnGe99#$#");
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(password,salt,1000);
    string pdbStr =Convert.ToBase64String(pdb.GetBytes(32));
    Console.WriteLine(pdbStr);
    //outpu : RMqDMSV6d8uT2NicGM212r3KMFt7ZsOI2q8+0Rr0WZQ=

<强> JS

var crypto = require("crypto");
var password = "FlU4c8yQKLkYuFwsgyU4LFeIf7m3Qwy+poMBdULEMqw=";
var salt = "##oEDA102ExChAnGe99#$#";
crypto.DEFAULT_ENCODING = 'base64';
var pdbBytes = crypto.pbkdf2Sync(new Buffer(password,'base64'), salt, 1000, 32,'sha1');
var pdbStr = new Buffer(pdbBytes).toString()
console.log("pdbStr", pdbStr);
//outpu : RMqDMSV6d8uT2NicGM212r3KMFt7ZsOI2q8+0Rr0WZQ=