简单的使用FormsAuthenticationTicket来加密解密
使用下面两个方法可以对字符串实现加密解密,很简单就不多说了。
/// <summary>
/// 对字符串进行加密
/// </summary>
/// <param name="Passowrd">待加密的字符串</param>
/// <returns>string</returns>
public static string Encrypt(string Passowrd)
{
string strResult = "";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(Passowrd, true, 2);
strResult = FormsAuthentication.Encrypt(ticket).ToString();
return strResult;
}
/// <summary>
/// 对字符串进行解密
/// </summary>
/// <param name="Passowrd">已加密的字符串</param>
/// <returns></returns>
public static string Decrypt(string Passowrd)
{
string strResult = "";
strResult = FormsAuthentication.Decrypt(Passowrd).Name.ToString();
return strResult;
}
这个是实现不可逆加密的,存密码比较适合,也很简单。
/// <summary>
/// 对字符串进行加密(不可逆)
/// </summary>
/// <param name="Password">要加密的字符串</param>
/// <param name="Format">加密方式,0 is SHA1,1 is MD5</param>
/// <returns></returns>
public static string NoneEncrypt(string Password, int Format)
{
string strResult = "";
switch (Format)
{
case 0:
strResult = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "SHA1");
break;
case 1:
strResult = FormsAuthentication.HashPasswordForStoringInConfigFile(Password, "MD5");
break;
default:
strResult = Password;
break;
}
return strResult;
}
本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。