C# 實現MD5加密處理 MD5 加密

iDotNetSpace發表於2010-04-07

using System.Security.Cryptography;       //引入名稱空間
using System.Text;        //引入名稱空間

    public sealed class MD5Hashing
    {
        private static MD5 md5 = MD5.Create();
        private MD5Hashing()
        {
        }
        /**////


        /// 將字串加密
        ///

        /// 需要加密的字串
        /// MD5加密後字串

        public static string HashString(string sourceString)
        {
            return HashString("gb2312", sourceString);
        }
        /**////
        /// 字串MD5加密
        ///

        /// 編碼型別
        /// 需要加密的字串
        /// MD5加密後字串

        public static string HashString(string codeName, string sourceString)
        {
            byte[] source = md5.ComputeHash(Encoding.GetEncoding(codeName).GetBytes(sourceString));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < source.Length; i++)
            {
                sBuilder.Append(source[i].ToString("x"));
            }
            return sBuilder.ToString();
        }
    }

 

asp.net通用md5加密

using System.Web.Security;


public static string HashString(string sourceString)
    {
       return FormsAuthentication.HashPasswordForStoringInConfigFile(sourceString, "md5");
    }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-631497/,如需轉載,請註明出處,否則將追究法律責任。

相關文章