/// <summary>
/// 密码加密
/// </summary>
/// <param name="PasswordString">密码</param>
/// <param name="PasswordFormat">加密类型[SHA1],[MD5]</param>
/// <returns></returns>
public static string EncryptPassword(string PasswordString, string PasswordFormat)
{
string encryptPassword = null;
if (PasswordFormat == "SHA1")
{
encryptPassword =FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
}
else if (PasswordFormat == "MD5")
{
encryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");
}
return encryptPassword;
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string DecryptDES(string decryptString, string decryptKey)
{
try
{
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = Keys;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
catch
{
return decryptString;
}
}