码迷,mamicode.com
首页 > Web开发 > 详细

.Net实现Md5加密字符串

时间:2015-07-18 13:49:55      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;
using System.IO;
using System.Security.Cryptography;

namespace ZC.Common.Simp
{


    public static class MD5
    {
        public static string G(string str,int Length=16)
        {
            return MD5Helper.MD5(str,Length);
        }

    }


    public partial class MD5Helper
    {
        #region MD5函数
        /// <summary>
        /// MD5函数,需引用:using System.Security.Cryptography;
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string MD5(string str)
        {
            //微软md5方法参考return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
            byte[] b = Encoding.Default.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, ‘0‘);
            return ret;
        }

        public static string MD5(string str, int code)
        {
            string strEncrypt = string.Empty;
            if (code == 16)
            {
                strEncrypt = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").Substring(8, 16);
            }
            if (code == 32)
            {
                strEncrypt = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
            }
            return strEncrypt;
        }
        #endregion
    }
}

.Net实现Md5加密字符串

标签:

原文地址:http://www.cnblogs.com/huangdegen/p/4656677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!