吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5210|回复: 7
收起左侧

[其他转载] C#中MD5实现

 关闭 [复制链接]
mycsy 发表于 2009-4-26 11:54
在C#中使用MD5可以通过“HashPasswordForStoringInConfigFile”、“MD5CryptoServiceProvider”、“Cryptography.MD5”等各种方式实现。

最简单的方式是用“HashPasswordForStoringInConfigFile”,例如:

pass = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "md5").ToLower();
该方法用于处理英文的字符串没有什么问题,但如果在使用UTF8编码的aspx中处理“GB2312”编码的字符串可能就得不到正确结果,这是由于它是以String为参数而不是byte[]。

第二种方法是用“MD5CryptoServiceProvider”,例如:

    static string MyMd5_1(string str)
    {
        //byte[] b = System.Text.Encoding.Default.GetBytes(str);
        byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(str);
        byte[] d = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
        string r = "";
        for (int i = 0; i < d.Length; i++)
            r += d[i].ToString("x").PadLeft(2, '0');
        return r;
    }
第三种方法是用“Cryptography.MD5”,例如:

    static string MyMd5_2(string str)
    {
        //byte[] b = System.Text.Encoding.Default.GetBytes(str);
        byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(str);
        System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
        byte[] d = md5.ComputeHash(b);
        string r = "";
        for (int i = 0; i < d.Length; i++)
            r += d[i].ToString("x").PadLeft(2, '0');
        return r;
    }
后两种方法实际上是以byte[]为参数的,因此可以很方便的改成处理各种语言编码的字符串。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| mycsy 发表于 2009-5-9 22:04
我晕 这么强大的东西 居然没人UP?
yeguolin1 发表于 2009-6-2 16:32
fat4 发表于 2009-8-1 23:02
zt307982962 发表于 2009-8-20 22:50
我来UP!!!!!!!!!!!!!!
freeback 发表于 2010-7-9 17:01
刚好用到,多谢楼主
jovian007 发表于 2010-8-18 15:11
这个我顶,虽然我是学VB.NET,但两个通用
killerwy 发表于 2013-1-13 22:33
本帖最后由 killerwy 于 2013-1-13 22:40 编辑

//获取md5
publicstring getmd5(string path)
         {
             FileStream get_file =new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
             System.Security.Cryptography.MD5CryptoServiceProvider get_md5 =new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(get_file);
string md5str = System.BitConverter.ToString(hash_byte);
             md5str = md5str.Replace("-", "");
return md5str;
         }
//获取sha1
publicstring getsha1(string path)
         {
             FileStream get_file =new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
             System.Security.Cryptography.SHA1CryptoServiceProvider get_sha1 =new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] hash_byte = get_sha1.ComputeHash(get_file);
string sha1str = System.BitConverter.ToString(hash_byte);
             sha1str = sha1str.Replace("-", "");
return sha1str;
         }

其实这样就搞定了哦
PS 你改下分类呗 c# 属于.net里的哦 亲


您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-6-11 00:12

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表