網上流行的ASP版md5.perl版des算法在C#中的簡單實現
1:MD5
以前在asp時代常用的MD5算法好象是從動網流出來的,後來大家都用它,基本上有兩種 ,區別在md5.ASP的結尾部分
MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d))
MD5=LCase(WordToHex(b) & WordToHex(c))
分別對應32位和16位加密方式
在C#中對應的實現為
/// <summary>
/// 16位MD5加密方法,以前的DVBBS所使用
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <returns>加密後的字串</returns>
public string MD5Encrypt(string strSource)
{
return MD5Encrypt(strSource, 16);
}
/// <summary>
/// MD5加密,和動網上的16/32位MD5加密結果相同
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="length">16或32值之一,其它則采用.Net默認MD5加密算法</param>
/// <returns>加密後的字串</returns>
public string MD5Encrypt(string strSource, int length)
{
byte[] bytes = Encoding.ASCII.GetBytes(strSource);
byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
switch (length)
{
case 16:
for (int i = 4; i < 12; i++)
sb.Append(hashValue[i].ToString("x2"));
break;
case 32:
for (int i = 0; i < 16; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
default:
for (int i = 0; i < hashValue.Length; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
}
同樣的其它語言都實現了DES加密與.net framework的des基礎實現也不一樣,比較郁悶的是我剛開始使用.Net framework時還真的改寫過perl版的des,後面才發現其實有更簡單的辦法,因為網上流傳的perl/c/Java版的des算法都是塊加密的,設置CipherMode為ECB就好了,郁悶ing.
源代碼如下
public static byte[] DESKey = new byte[] {0x82, 0xBC, 0xA1, 0x6A, 0xF5, 0x87, 0x3B, 0xE6, 0x59, 0x6A, 0x32, 0x64, 0x7F, 0x3A, 0x2A, 0xBB, 0x2B, 0x68, 0xE2, 0x5F, 0x06, 0xFB, 0xB8, 0x2D, 0x67, 0xB3, 0x55, 0x19, 0x4E, 0xB8, 0xBF, 0xDD };
/// <summary>
/// DES加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="key">32位Key值</param>
/// <returns>加密後的字符串</returns>
public string DESEncrypt(string strSource) {
return DESEncrypt(strSource, DESKey);
}
public string DESEncrypt(string strSource,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode= CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] byt = Encoding.Unicode.GetBytes(strSource);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="strSource">待解密的字串</param>
/// <param name="key">32位Key值</param>
/// <returns>解密後的字符串</returns>
public string DESDecrypt(string strSource) {
return DESDecrypt(strSource, DESKey);
}
public string DESDecrypt(string strSource,byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] byt = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream(byt);
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, Encoding.Unicode);
return sr.ReadToEnd();
}