起因
首先,对于一个公司,即有.net又有java开发来说,加解密是需要相互打通的,因为,根据莫非定律你迟早会有“.net加密让java解密“的情况。
.net实现
public static string Encrypt(SymmetricAlgorithm algorithm, string plaintext, string key)
{
TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
algorithm.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
algorithm.Mode = CipherMode.ECB;
Console.WriteLine("IV=" + Convert.ToBase64String(algorithm.IV));
ICryptoTransform transformer = algorithm.CreateEncryptor();
byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(plaintext);
byte[] result = transformer.TransformFinalBlock(Buffer, 0, Buffer.Length);
return Convert.ToBase64String(result);
}