c#官方推薦md5通用加密類

王磊的部落格發表於2016-06-14
/// <summary>
/// MD5加密
/// </summary>
/// <param name="input">需要加密的字串</param>
/// <returns></returns>
public static string MD5Encrypt(string input)
{
    return MD5Encrypt(input, new UTF8Encoding());
}

/// <summary>
/// md5加密16|32位
/// </summary>
/// <param name="input"></param>
/// <param name="length"></param>
/// <returns></returns>
public static string MD5Encrypt(string input, int length)
{
    string res = MD5Encrypt(input, new UTF8Encoding());
    if (length == 16)
    {
        res = res.Substring(8, 16);
    }
    return res;
}

/// <summary>
/// MD5加密
/// </summary>
/// <param name="input">需要加密的字串</param>
/// <param name="encode">字元的編碼</param>
/// <returns></returns>
public static string MD5Encrypt(string input, Encoding encode)
{
    if (string.IsNullOrEmpty(input))
    {
        return null;
    }
    MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    byte[] data = md5Hasher.ComputeHash(encode.GetBytes(input));
    StringBuilder sBuilder = new StringBuilder();
    for (int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }
    return sBuilder.ToString();
}

 

相關文章