使用using System.Security.Cryptography进行md5加密;

private string encrypt(string src)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] dir = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(src));
            StringBuilder resualt = new StringBuilder();
            for (int i = 0; i < dir.Length; i++)
            {
                string x = Convert.ToString(dir[i], 16);
                if (x.Length < 2)
                    x = "0" + x;
                resualt.Append(x);
            }
            return resualt.ToString();
        }


使用System.Web.Security进行md5加密
public string md5(string str, int code)
    {

        if (code == 16)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
//其实"MD5"还可以有别的类型,如SHA1,也是另一种加密方式
        }

        if (code == 32)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
        }

        return "00000000000000000000000000000000";
    }

posted on 2006-12-31 10:56    阅读(208)  评论(0)    收藏  举报