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 :-(");

 

转载于:https://www.cnblogs.com/lolitagis02/p/8232774.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值