C# 实现MD5加密处理 MD5 加密

时间:2022-10-28 17:01:31

using System.Security.Cryptography;       //引入命名空间
using System.Text;        //引入命名空间

    public sealed class MD5Hashing
    {
        private static MD5 md5 = MD5.Create();
        private MD5Hashing()
        {
        }
        /**//// <summary>
        /// 将字符串加密
        /// </summary>
        /// <param name="sourceString">需要加密的字符串</param>
        /// <returns>MD5加密后字符串</returns>

        public static string HashString(string sourceString)
        {
            return HashString("gb2312", sourceString);
        }
        /**//// <summary>
        /// 字符串MD5加密
        /// </summary>
        /// <param name="codeName">编码类型</param>
        /// <param name="sourceString">需要加密的字符串</param>
        /// <returns>MD5加密后字符串</returns>

        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");
    }