密码加密【转码】

来源:互联网 发布:linux 退出tomcat日志 编辑:程序博客网 时间:2024/06/07 06:36

        public  string EncryptPassword(string PasswordString, string PasswordFormat)

        {

            string encryptPassword = "";

            if (PasswordFormat == "SHA1")

            {

                encryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");

            }

            else if (PasswordFormat == "MD5")

            {

                byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(PasswordString));

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < encryptedBytes.Length; i++)

                {

 

                    sb.AppendFormat("{0:x2}", encryptedBytes[i]);

                }

                return sb.ToString().ToUpper();

                encryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");

            }

            return encryptPassword;

        }