C#加密NodeJS解密

C#代码:

    class Program
{
static void Main(string[] args)
{
Console.WriteLine("encrypted as: {0}", TripleDESEncrypt("", "abcdefghijklmnop", "pgfYadhCout=", false)); }
public static string TripleDESEncrypt(string toEncrypt, string key, string iv, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
byte[] ivArray = Convert.FromBase64String(iv); if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key); var tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.IV = ivArray;
// tdes.Mode = CipherMode.CBC; // which is default
// tdes.Padding = PaddingMode.PKCS7; // which is default ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, ,
toEncryptArray.Length);
return Convert.ToBase64String(resultArray, , resultArray.Length);
}
}

NodeJS的javascript代码:

var crypto = require('crypto');
var alg = 'des-ede-cbc';
var key = new Buffer('abcdefghijklmnop', 'utf-8');
var iv = new Buffer('pgfYadhCout=', 'base64'); var EncryptDES = function (data) {
var cipher = crypto.createCipheriv(alg, key, iv);
var encoded = cipher.update(data, 'ascii', 'base64');
encoded += cipher.final('base64');
return encoded;
}; var DecryptDES = function (data) {
var encrypted = new Buffer(data, 'base64');
var decipher = crypto.createDecipheriv(alg, key, iv);
var decoded = decipher.update(encrypted, 'binary', 'ascii');
decoded += decipher.final('ascii');
return decoded;
};
上一篇:H5开发中遇到的问题及解决办法


下一篇:.NET生成PDF文件