des 加解密帮助类
/// <summary>
/// des 加解密帮助类
/// </summary>
public static class DESHelper
{
private static string desKey = "iuguguiy";//加解密私钥
private static string flag = "$$$";//签名算法分隔符
private static string replacAdd = "{@@@}";
public static string DesEncrypt(string encryptStr)
{
try
{
var inputArry = Encoding.Default.GetBytes(encryptStr);
var byKey = Encoding.ASCII.GetBytes(desKey);
var byIv = Encoding.ASCII.GetBytes(desKey);
var ms = new MemoryStream();
using (var cryptoProvider = new DESCryptoServiceProvider())
{
using (var cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIv), CryptoStreamMode.Write))
{
cs.Write(inputArry, 0, inputArry.Length);
cs.FlushFinalBlock();
cs.Close();
}
}
var str = Convert.ToBase64String(ms.ToArray());
ms.Close();
return str;
}
catch (Exception)
{
return encryptStr;
}
}
public static string DesDecrypt(string decryptStr)
{
try
{
var inputArry = Convert.FromBase64String(decryptStr);
var byKey = Encoding.Default.GetBytes(desKey);
var byIv = Encoding.Default.GetBytes(desKey);
var ms = new MemoryStream();
using (var cryptProvider = new DESCryptoServiceProvider())
{
using (var cs = new CryptoStream(ms, cryptProvider.CreateDecryptor(byKey, byIv), CryptoStreamMode.Write))
{
cs.Write(inputArry, 0, inputArry.Length);
cs.FlushFinalBlock();
cs.Close();
}
}
var str = Encoding.Default.GetString(ms.ToArray());
ms.Close();
return str;
}
catch (Exception)
{
return decryptStr;
}
}
public static string SignDesEncrypt(string encryptStr)
{
string bck = "";
bck = DesEncrypt(encryptStr);
bck = bck.Replace("+",replacAdd);
///Regex reg = new Regex("+");
bck = System.Web.HttpUtility.UrlEncode(bck + flag + GetMd5Str(bck));
return bck;
}
public static bool SignDesDecrypt(string encryptStr, out string bckstr)
{
encryptStr = System.Web.HttpUtility.UrlDecode(encryptStr);
bckstr = "";
if (!encryptStr.Contains(flag))
{
return false;
}
string[] arry = encryptStr.Split(flag);
if (GetMd5Str(arry[0]) == arry[1])
{
bckstr = DesDecrypt(arry[0].Replace(replacAdd,"+"));
}
else
{
return false;
}
return true;
}
//// <summary>
/// MD5 16位加密 加密后密码为小写
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
t2 = t2.ToLower();
return t2;
}
}