using System.Security.Cryptography; using System.Text; namespace RS.Common { /// /// MD5加密 /// public class Md5Util { /// /// MD5加密 /// /// 加密字符 /// 加密位数16/32 /// public static string Encrypt(string str, int code = 32) { HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm; if (string.IsNullOrEmpty(str)) return string.Empty; byte[] bytes = Encoding.UTF8.GetBytes(str); byte[] hashValue = provider.ComputeHash(bytes); StringBuilder sb = new StringBuilder(); switch (code) { case 16://16位密文是32位密文的9到24位字符 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; } return sb.ToString(); } } }