using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using CNet.DBUtility;
namespace CNet.DBUtility
{
///
/// DES加密/解密类。
///
public class DesEncrypt
{
public static string jmkeys = ConfigurationManager.AppSettings["jmkey"];
public static string encryption = "CNet948";
///
/// 加密
///
///
///
public static string Encrypt(string text)
{
return DESEnCode(text, jmkeys);
}
///
/// 解密
///
///
///
public static string Decrypt(string text)
{
return DESDeCode(text, jmkeys);
}
///
/// 加密
///
///
///
public static string Ecode(string text)
{
return DESEnCode(text, encryption);
}
///
/// 解密
///
///
///
public static string Dcode(string text)
{
return DESDeCode(text, encryption);
}
///
/// 加密字符串
///
/// 要加密的字符
/// 密钥
///
public static string DESEnCode(string pToEncrypt, string sKey)
{
try
{
pToEncrypt = System.Web.HttpUtility.UrlEncode(pToEncrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch
{
return null;
}
}
#region DESDeCode DES解密
///
/// 解密字符串
///
/// 解密字符
/// 密钥
///
public static string DESDeCode(string pToDecrypt, string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (System.Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
// return HttpContextHelper.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch
{
return null;
}
}
///
/// 获取时间戳
///
///
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
#endregion
}
}