MD5 encryption encapsulated

1. Fixed Fixed-length string (16-bit or 32-bit)

///  <Summary> 
/// encrypted character string with the encrypted string MD5 selectively generates a 16-bit or 32-bit
 ///  </ Summary> 
///  <param name = "password"> be encrypted string </ param> 
///  <param name = " 'bit"> bits, the general value of 16 or 32 </ param> 
///  <returns> encrypted string returned </ returns> 
public  string MD5Encrypt ( string password, int 'bit) 
{ 
the MD5CryptoServiceProvider md5Hasher = new new the MD5CryptoServiceProvider ();
 byte [] hashedDataBytes; 
hashedDataBytes = md5Hasher.ComputeHash (Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
if (bit == 16)
return tmp.ToString().A(8, 16);
else
if (bit == 32) return tmp.ToString();//默认情况
else return string.Empty;
}

 

 2. encrypted string
Copy the code
/// <summary>
/// 用MD5加密字符串
/// </summary>
/// <param name="password">待加密的字符串</param>
/// <returns></returns>
public string MD5Encrypt(string password)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes;
hashedDataBytes = md5Hasher.ComputeHash(Encoding.GetEncoding("gb2312").GetBytes(password));
StringBuilder tmp = new StringBuilder();
foreach (byte i in hashedDataBytes)
{
tmp.Append(i.ToString("x2"));
}
return tmp.ToString();
}

Guess you like

Origin www.cnblogs.com/yuanshuo/p/11528319.html