[C#] C# MD5计算 →→→→→进入此内容的聊天室

来自 , 2019-06-23, 写在 C#, 查看 103 次.
URL http://www.code666.cn/view/1d7c2aae
  1. /// <summary>
  2. /// Calculates a MD5 hash from the given string and uses the given
  3. /// encoding.
  4. /// </summary>
  5. /// <param name="Input">Input string</param>
  6. /// <param name="UseEncoding">Encoding method</param>
  7. /// <returns>MD5 computed string</returns>
  8. public static string CalculateMD5(string Input, Encoding UseEncoding)
  9. {
  10.     System.Security.Cryptography.MD5CryptoServiceProvider CryptoService;
  11.     CryptoService = new System.Security.Cryptography.MD5CryptoServiceProvider();
  12.  
  13.     byte[] InputBytes = UseEncoding.GetBytes(Input);
  14.     InputBytes = CryptoService.ComputeHash(InputBytes);
  15.     return BitConverter.ToString(InputBytes).Replace("-", "");
  16. }
  17.  
  18. /// <summary>
  19. /// Calculates a MD5 hash from the given string.
  20. /// (By using the default encoding)
  21. /// </summary>
  22. /// <param name="Input">Input string</param>
  23. /// <returns>MD5 computed string</returns>
  24. public static string CalculateMD5(string Input)
  25. {
  26.     // That's just a shortcut to the base method
  27.     return CalculateMD5(Input, System.Text.Encoding.Default);
  28. }
  29.  
  30. //调用例子:
  31. // The example below shows how to verify a password
  32. // by using a MD5-hash:
  33.  
  34. // Password could be from user input
  35. string PlainPassword    = "secret password";
  36. string HashedPassword   = CalculateMD5(PlainPassword);
  37.  
  38. // This hash may come from the database
  39. string StoredPassword   = "A584EFAFA8F9EA7FE5CF18442F32B07B";
  40.  
  41. // Are the hashes equal?
  42. if (HashedPassword == StoredPassword)
  43.     MessageBox.Show("Correct password!");
  44. else
  45.     MessageBox.Show("Sorry, bad password :-(");
  46. //csharp/2059

回复 "C# MD5计算"

这儿你可以回复上面这条便签

captcha