引用命名空间:using System.Security.Cryptography;
MD5 16位加密:
public string GetMD5_16(string str) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); return BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8).Replace("-", ""); }
MD5 32位加密:
public string GetMD5_32(string str) { string pwd = ""; MD5 md5 = MD5.Create(); byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str)); for (int i = 0; i < s.Length; i++) { pwd = pwd + s[i].ToString("X"); } return pwd; }
SHA1加密:
public string GetSHA1(string str) { SHA1 sha1 = new SHA1CryptoServiceProvider(); return BitConverter.ToString(sha1.ComputeHash(UTF8Encoding.Default.GetBytes(str))).Replace("-",""); }