//适用于C#语言
//使用前需导入以下命名空间:using System.Web.Security;
//第一个参数为需加密的字符串,第二个参数为加密的格式(只有SHA1和MD5两种,可任选一种)
public string EncryptPassword(string PasswordString,string PasswordFormat )
{
string EncryptPassword = null;
if ( "SHA1 ".Equals(PasswordFormat))
{
EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString , "SHA1 ");
}
else if ( "MD5 ".Equals(PasswordFormat))
{
EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString , "MD5 ");
}
else
{
EncryptPassword = PasswordString;
}
return EncryptPassword;
}
加密:
命名空间: System.Web.Security
FormsAuthentication:为 Web 应用程序管理 Forms 身份验证服务。无法继承此类。
FormsAuthentication.HashPasswordForStoringInConfigFile 方法: 根据指定的密码和哈希算法生成一个适合于存储在配置文件中的哈希密码。
public static string HashPasswordForStoringInConfigFile(
string password,
string passwordFormat
)
参数
password
类型:System.String
要进行哈希运算的密码。
passwordFormat
类型:System.String
要使用的哈希算法。passwordFormat 是一个 String,表示 FormsAuthPasswordFormat 枚举值之一。
FormsAuthPasswordFormat 枚举
成员名称 | 说明 | |
| Clear | 指定不加密密码。此字段为常数。 |
| SHA1 | 指定使用 SHA1 哈希算法加密密码。此字段为常数。 |
| MD5 | 指定使用 MD5 哈希算法加密密码。此字段为常数。 |