标签:
demo效果
Md5类
其中有一个引用 using System.Security.Cryptography;
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace MD5加密 { public class MD5Fun { /// <summary> /// 加密字符串 /// </summary> /// <param name="iptStr"></param> /// <returns></returns> public static string encryStr(string iptStr) { using (MD5 md5Hash = MD5.Create()) { return MD5Fun.GetMd5Hash(md5Hash,iptStr); } } /// <summary> /// 字符串构建 /// </summary> /// <param name="md5Hash"></param> /// <param name="input"></param> /// <returns></returns> public static string GetMd5Hash(MD5 md5Hash,string input) { byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length;i++ ) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); } /// <summary> /// 将给定的字符串按照指定的MD5格式装换后,与给定的密文进行比较 /// </summary> /// <param name="md5Hash"></param> /// <param name="input"></param> /// <param name="hash"></param> /// <returns></returns> public static bool VerifyMd5Hash(MD5 md5Hash,string input,string hash) { string hashOfInput = GetMd5Hash(md5Hash,input); //OrdinalIgnoreCase 忽略大小写进行较 StringComparer comparer = StringComparer.OrdinalIgnoreCase; // Compare 相同返回0,不同返回1 var result = comparer.Compare(hashOfInput, hash); if (0 == comparer.Compare(hashOfInput, hash)) { return true; } else { return false; } } /// <summary> /// 比较字符串加密后 与给定的密文是否相同 /// </summary> /// <param name="str"></param> /// <param name="md5Str"></param> /// <returns></returns> public static bool validateStr(string str,string md5Str) { using(MD5 md5Hash = MD5.Create()) { if (VerifyMd5Hash(md5Hash, str, md5Str)) { return true; } else { return false; } } } } }
标签:
原文地址:http://www.cnblogs.com/zix1314/p/5616736.html