首页 > Windows开发 > 详细

C#生成MD5字符串

时间:2017-06-03 19:50:20      阅读:254      评论:0      收藏:0      [点我收藏+]

 

using System.Security.Cryptography;
using System.Text;

public class MD5Helper
{
    private static MD5 md5 = MD5.Create();

    //使用utf8编码将字符串散列
    public static string GetMD5HashString(string sourceStr)
    {
        return GetMD5HashString(Encoding.UTF8,sourceStr);    
    }

    //使用指定编码将字符串散列
    public static string GetMD5HashString(Encoding encode,string sourceStr)
    {
        StringBuilder sb = new StringBuilder();

        byte[] source = md5.ComputeHash(encode.GetBytes(sourceStr));
        for (int i = 0; i < source.Length; i++)
        {
            sb.Append(source[i].ToString("x2"));
        }

        return sb.ToString();  
    }
}

上面的方法与下面的方法计算结果相同:

System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sourceStr, "MD5").ToLower()),sourceStr是要进行哈希运算的字符串,非Asp.Net应用需要添加System.Web.dll引用。

C#生成MD5字符串

原文:http://www.cnblogs.com/Arlar/p/6938154.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!