========== C#代码 ==========
private static byte[] KEY = { 1, 2, 3, 4, 5, 6, 7, 8 };
private static byte[] IV = { 1, 2, 3, 4, 5, 6, 7, 8 };
public static string EncryptDes(string input)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
MemoryStream ms = null;
CryptoStream encStream = null;
StreamWriter sw = null;
string result = String.Empty;
try
{
ms = new MemoryStream();
encStream = new CryptoStream(ms, des.CreateEncryptor(KEY, IV), CryptoStreamMode.Write);
// Create a StreamWriter to write a string
// to the stream.
sw = new StreamWriter(encStream);
// Write the plaintext to the stream.
sw.WriteLine(input);
sw.Flush();
encStream.FlushFinalBlock();
ms.Flush();
byte[] bytes = ms.GetBuffer();
Console.WriteLine(BitConverter.ToString(bytes, 0, 24));
result = Convert.ToBase64String(bytes, 0, Convert.ToInt32(ms.Length, CultureInfo.InvariantCulture));
}
finally
{
//close objects
}
return result;
}
========== Node.js代码 ==========
var crypto = require(‘crypto’); var KEY = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; var IV = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; exports.encryptDes = function(input){ var cipher = crypto.createCipheriv('des’, new Buffer(KEY), new Buffer(IV)); var buf1 = cipher.update(input, ‘utf8’); var buf2 = cipher.final(); var result = new Buffer(buf1.length + buf2.length); buf1.copy(result); buf2.copy(result, buf1.length);
return result.toString('base64');
};