C#代码:
class Program { static void Main(string[] args) { Console.WriteLine("encrypted as: {0}", TripleDESEncrypt("12345", "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, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, 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; };