刚刚发了DES加密解密的代码,现在是MD5的代码了,直接复制下来用即可
MD5加密
[C#] 纯文本查看 复制代码 /// <summary>
/// MD5加密
/// </summary>
/// <param name="pToEncrypt">要加密字符串</param>
/// <returns></returns>
public static string MD5Encrypt(string pToEncrypt)
{
string sKey = "12345678";//默认8位加密密匙
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
MD5解密
[C#] 纯文本查看 复制代码 /// <summary>
/// MD5加密
/// </summary>
/// <param name="pToEncrypt">要加密字符串</param>
/// <returns></returns>
public static string MD5Encrypt(string pToEncrypt)
{
string sKey = "12345678";//默认8位加密密匙
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
|