/// <summary> /// MD5 32位加密(大寫) /// </summary> /// <param name="str"></param> /// <returns></returns> static string UserMd5(string str) { string cl = str; string pwd = ""; MD5 md5 = MD5.Create();//執行個體化一個md5對像 // 加密後是一個位元組類型的數組,這裡要注意編碼UTF8/Unicode等的選擇 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); // 通過使用迴圈,將位元組類型的數群組轉換為字串,此字串是常規字元格式設定化所得 for (int i = 0; i < s.Length; i++) { // 將得到的字串使用十六進位類型格式。格式後的字元是小寫字母,如果使用大寫(X)則格式後的字元是大寫字元 pwd = pwd + s[i].ToString("X"); } return pwd; } /// <summary> /// MD5 16位加密 加密後密碼為小寫 /// </summary> /// <param name="ConvertString"></param> /// <returns></returns> public static string GetMd5str(string ConvertString) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8); t2 = t2.Replace("-", ""); t2 = t2.ToLower(); return t2; } /// <summary> /// MD5 16位加密 加密後密碼為大寫 /// </summary> /// <param name="ConvertString"></param> /// <returns></returns> public static string GetMd5Str(string ConvertString) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8); t2 = t2.Replace("-", ""); return t2; } /// <summary> /// MD5加密(小寫) /// </summary> /// <param name="input"></param> /// <returns></returns> private static string Md5Hash(string input) { MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); }