28,391
社区成员
发帖
与我相关
我的任务
分享
private const string mKey = "+BvG1CvKdyM=";
private const string mIV = "+JiG8EvAdyM=";
/// <summary>
/// DES加密
/// </summary>
/// <param name="pToEncrypt">需要加密的字符串</param>
public static string DESEncryptPwd(string pToEncrypt)
{
byte[] KEY, IV;
byte[] inputstr = Encoding.Unicode.GetBytes(pToEncrypt);
DESCryptoServiceProvider mydes = new DESCryptoServiceProvider();
KEY = Convert.FromBase64String(mKey);
IV = Convert.FromBase64String(mIV);
ICryptoTransform it = mydes.CreateEncryptor(KEY, IV);
MemoryStream ms = null;
CryptoStream cs = null;
try
{
ms = new MemoryStream();
cs = new CryptoStream(ms, it, CryptoStreamMode.Write);
cs.Write(inputstr, 0, inputstr.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
finally
{
if (ms != null)
{
ms.Close();
}
if (cs != null)
{
cs.Close();
}
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="pToDecrypt">需要解密的字符串</param>
public static string DESDecryptPwd(string pToDecrypt)
{
byte[] desdata1;
try
{
desdata1 = Convert.FromBase64String(pToDecrypt);
}
catch
{
return "";
}
byte[] KEY, IV;
DESCryptoServiceProvider mydes = new DESCryptoServiceProvider();
KEY = Convert.FromBase64String(mKey);
IV = Convert.FromBase64String(mIV);
ICryptoTransform ct = mydes.CreateDecryptor(KEY, IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(desdata1, 0, desdata1.Length);
cs.FlushFinalBlock();
return Encoding.Unicode.GetString(ms.ToArray());
}