public static byte[] MD5(string value)
{
if (string.IsNullOrEmpty(value))
return null;
MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
byte[] input, output;
input = Encoding.UTF8.GetBytes(value);
output = MD5.ComputeHash(input);
byte[] inputbuffer = new byte[24];
for (int i = 0; i < output.Length; i++)
{
if (i < 8)
{
inputbuffer[i] = inputbuffer[16 + i] = output[i];
}
else
{
inputbuffer[i] = output[i];
}
}
return inputbuffer;
}
/// <summary>
/// 加密
/// </summary>
/// <param name="sourceValue"></param>
/// <param name="key">明文密钥(需用MD5计算的哈希值16位+前8位作为算法最终密钥)</param>
/// <returns></returns>
public static string Encrypt3DES_V2(string sourceValue, string key)
{
string str = "";
try
{
TripleDESCryptoServiceProvider tripleDESCryptography = new TripleDESCryptoServiceProvider();
tripleDESCryptography.Key = MD5(key);
tripleDESCryptography.Mode = CipherMode.ECB;
tripleDESCryptography.Padding = PaddingMode.PKCS7;
ICryptoTransform DESEncrypt = tripleDESCryptography.CreateEncryptor();
byte[] buffer = Encoding.UTF8.GetBytes(sourceValue);
str = Convert.ToBase64String(DESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length));
}
catch
{
}
return str;
}
/// <summary>
/// 解密
/// </summary>
/// <param name="sourceValue"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string Decrypt3DES_V2(string sourceValue, string key)
{
string result = "";
try
{
TripleDESCryptoServiceProvider tripleDESCryptography = new TripleDESCryptoServiceProvider();
tripleDESCryptography.Key = MD5(key);
tripleDESCryptography.Mode = CipherMode.ECB;
tripleDESCryptography.Padding = PaddingMode.PKCS7;
ICryptoTransform DESDecrypt = tripleDESCryptography.CreateDecryptor();
byte[] buffer = Convert.FromBase64String(sourceValue);
result = Encoding.GetEncoding("GB2312").GetString(DESDecrypt.TransformFinalBlock(buffer, 0, buffer.Length));
}
catch
{
}
return result;
}