`
邪神Saber
  • 浏览: 41578 次
文章分类
社区版块
存档分类
最新评论

C# MD5加密工具类

 
阅读更多
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Util
{
    public class MD5
    {
        public static string MD5Encrypt(string str)
        {
            return MD5Encrypt(str, 16);
        }

        public static string MD5Encrypt(string strSource, int length)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(strSource);
            byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            switch (length)
            {
                case 16:
                    for (int i = 4; i < 12; i++)
                        sb.Append(hashValue[i].ToString("x2"));
                    break;
                case 32:
                    for (int i = 0; i < 16; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
                default:
                    for (int i = 0; i < hashValue.Length; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
            }
            return sb.ToString();
        }
    }

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics