using System; using System.Collections.Generic; using System.Text; using System.Web.Security; using System.Web.Configuration; public class Password { public static string ChangetoMd5(string password) { return FormsAuthentication.HashPasswordForStoringInConfigFile(password, FormsAuthPasswordFormat.MD5.ToString()); } }
VS 自带的加密方法
/// 3des加密字符串
/// </summary>
/// <param name="a_strString">要加密的字符串</param>
/// <param name="a_strKey">密钥</param>
/// <param name="encoding">编码方式</param>
/// <returns>加密后并经base63编码的字符串</returns>
/// <res>重载,指定编码方式</res>
public static string Encrypt3DES(string a_strString, string a_strKey, Encoding encoding)
{
TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider(); DES.Key = hashMD5.ComputeHash(encoding.GetBytes(a_strKey));
DES.Mode = CipherMode.ECB; ICryptoTransform DESEncrypt = DES.CreateEncryptor();
byte[] Buffer = encoding.GetBytes(a_strString);
return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
}
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Web.Configuration; public class Password
{
public static string ChangetoMd5(string password)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(password, FormsAuthPasswordFormat.MD5.ToString());
}
}
string password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text, "SHA1");
//MD5算法
string password1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text, "MD5");加密后生成不可逆密文保存到数据库中。用户登录时用加密计算后的密文与数据库中的密码密文比较。一致则通过验证,不一致则返回登录错误。
这种加密算法是不可逆的,所以除了用户自己,其他人无法得知用户的真实密码内容。SHA-1算法和MD5算法的区别:
SHA-1比MD5多32位密文,所以更安全。由于同样的原因,MD5比SHA-1的运算速度更快。
{
if (code == 16) //16位MD5加密(取32位加密的9~25字符)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower().Substring(8, 16);
} if (code == 32) //32位加密
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1").ToLower();
} return "00000000000000000000000000000000";
} /// <summary>
/// MD5加密
/// </summary>
/// <param name="strPwd">明码</param>
/// <returns></returns>
public string MD5(string strPwd)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);
byte[] md5data = md5.ComputeHash(data);
md5.Clear();
string str = "";
for (int i = 0; i < md5data.Length; i++)
{
str += md5data[i].ToString("x").PadLeft(2, '0');
}
return str;
}
string password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text, "SHA1");
//MD5算法
string password1 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Text, "MD5");
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(input, "md5");
}
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] passMd5 = md5.ComputeHash(passWord);
string strPass = System.Text.Encoding.UTF8.GetString(passMd5).Trim().Replace("'","!");
return strPass;