简单的MD5密码加密和解密方法。MD5的算法是不可逆的,MD5被广泛用于密码验证和消息体完整性验证。
下面的例子用到了密码加密和登陆时的解密的基本方法。当然这样很容易被暴力破解,可以做其他改进,如先设计一个足够复杂的密码,然后将他的MD5值与原密码MD5值相加后再求一次MD5值,这样可以增加破解难度。
简单示例如下:
[csharp] view plain copy static void Main(string[] args) { Console.WriteLine("input password"); string source = Console.ReadLine(); string hash = GetMd5Hash(source); Console.WriteLine("password: {0}, MD5 {1}", source, hash); Console.WriteLine("input password"); string psd = Console.ReadLine(); if (VerifyMd5Hash(psd, hash))//验证成功返回OK Console.WriteLine("OK"); else Console.WriteLine("ERROR"); Console.ReadKey(); } static string GetMd5Hash(string input)//获取密码对应的MD5字符串 { using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) { return BitConverter.ToString(md5.ComputeHash (UTF8Encoding.Default.GetBytes(input))).Replace("-", ""); } } static bool VerifyMd5Hash(string input, string Hash)//比较输入密码 { string hashOfInput = GetMd5Hash(input); // StringComparer comparer = StringComparer.OrdinalIgnoreCase;//忽略大小写的比较器 return hashOfInput.CompareTo(Hash) == 0 ? true : false; // return comparer.Compare(hashOfInput, Hash) == 0 ? true : false;
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 站壳网