using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Security.Cryptography; namespace Infrastructure.Helpers { public class Md5Helper { // // 摘要: // MD5加密 // // 参数: // str: // 加密字符 // // code: // 加密位数16/32 public static string Encrypt(string str, int code) { string result = string.Empty; if (code == 16) { result = Hash(str).Substring(8, 16); } if (code == 32) { result = Hash(str); } return result; } // // 摘要: // 32位MD5加密(小写) // // 参数: // input: // 输入字段 public static string Hash(string input) { MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider(); byte[] array = mD5CryptoServiceProvider.ComputeHash(Encoding.Default.GetBytes(input)); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < array.Length; i++) { stringBuilder.Append(array[i].ToString("x2")); } return stringBuilder.ToString(); } } }