• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C# MD5计算

OC/C/C++ 水墨上仙 1384次浏览

本范例演示了C#如何对一个给定的字符串进行MD5加密

/// <summary>
/// Calculates a MD5 hash from the given string and uses the given
/// encoding.
/// </summary>
/// <param name="Input">Input string</param>
/// <param name="UseEncoding">Encoding method</param>
/// <returns>MD5 computed string</returns>
public static string CalculateMD5(string Input, Encoding UseEncoding)
{
    System.Security.Cryptography.MD5CryptoServiceProvider CryptoService;
    CryptoService = new System.Security.Cryptography.MD5CryptoServiceProvider();
 
    byte[] InputBytes = UseEncoding.GetBytes(Input);
    InputBytes = CryptoService.ComputeHash(InputBytes);
    return BitConverter.ToString(InputBytes).Replace("-", "");
}
 
/// <summary>
/// Calculates a MD5 hash from the given string. 
/// (By using the default encoding)
/// </summary>
/// <param name="Input">Input string</param>
/// <returns>MD5 computed string</returns>
public static string CalculateMD5(string Input)
{
    // That's just a shortcut to the base method
    return CalculateMD5(Input, System.Text.Encoding.Default);
}
//调用例子:
// The example below shows how to verify a password
// by using a MD5-hash:
 
// Password could be from user input
string PlainPassword	= "secret password";
string HashedPassword	= CalculateMD5(PlainPassword);
 
// This hash may come from the database
string StoredPassword	= "A584EFAFA8F9EA7FE5CF18442F32B07B";
 
// Are the hashes equal?
if (HashedPassword == StoredPassword)
    MessageBox.Show("Correct password!");
else
    MessageBox.Show("Sorry, bad password :-(");


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C# MD5计算
喜欢 (0)
加载中……