///
///类名称 :CryptTools///类说明 :加解密算法///作者 :小老虎2///完成日期 :///
public static classCryptTools
{///
///方法说明 :加密方法///作者 :///完成日期 :///
/// 需要加密的明文内容
/// 加密密钥
/// 返回加密后密文字符串
public static string Encrypt(string content, stringsecret)
{if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))throw new ArgumentNullException("Invalid Argument");byte[] Key =GetKey(secret);byte[] ContentByte =Encoding.Unicode.GetBytes(content);
MemoryStream MSTicket= newMemoryStream();
MSTicket.Write(ContentByte,0, ContentByte.Length);byte[] ContentCryptByte =Crypt(MSTicket.ToArray(), Key);string ContentCryptStr =Encoding.ASCII.GetString(Base64Encode(ContentCryptByte));returnContentCryptStr;
}///
///方法说明 :解密方法///作者 :///完成日期 :///
/// 需要解密的密文内容
/// 解密密钥
/// 返回解密后明文字符串
public static string Decrypt(string content, stringsecret)
{if ((content == null) || (secret == null) || (content.Length == 0) || (secret.Length == 0))throw new ArgumentNullException("Invalid Argument");byte[] Key =GetKey(secret);byte[] CryByte =Base64Decode(Encoding.ASCII.GetBytes(content));byte[] DecByte =Decrypt(CryByte, Key);byte[] RealDecByte;stringRealDecStr;
RealDecByte=DecByte;byte[] Prefix = new byte[Constants.Operation.UnicodeReversePrefix.Length];
Array.Copy(RealDecByte, Prefix,2);if(CompareByteArrays(Constants.Operation.UnicodeReversePrefix, Prefix))
{byte SwitchTemp = 0;for (int i = 0; i < RealDecByte.Length - 1; i = i + 2)
{
SwitchTemp=RealDecByte[i];
RealDecByte[i]= RealDecByte[i + 1];
RealDecByte[i+ 1] =SwitchTemp;
}
}
RealDecStr=Encoding.Unicode.GetString(RealDecByte);returnRealDecStr;
}//使用TripleDES加密 ,三倍DES加密
public static byte[] Crypt(byte[] source, byte[] key)
{if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{throw new ArgumentException("Invalid Argument");
}
TripleDESCryptoServiceProvider dsp= newTripleDESCryptoServiceProvider();
dsp.Mode=CipherMode.ECB;
ICryptoTransform des= dsp.CreateEncryptor(key, null);return des.TransformFinalBlock(source, 0, source.Length);
}//使用TripleDES解密 来处理,三倍DES解密
public static byte[] Decrypt(byte[] source, byte[] key)
{if ((source.Length == 0) || (source == null) || (key == null) || (key.Length == 0))
{throw new ArgumentNullException("Invalid Argument");
}
TripleDESCryptoServiceProvider dsp= newTripleDESCryptoServiceProvider();
dsp.Mode=CipherMode.ECB;
ICryptoTransform des= dsp.CreateDecryptor(key, null);byte[] ret = new byte[source.Length + 8];intnum;
num= des.TransformBlock(source, 0, source.Length, ret, 0);
ret= des.TransformFinalBlock(source, 0, source.Length);
ret= des.TransformFinalBlock(source, 0, source.Length);
num=ret.Length;byte[] RealByte = new byte[num];
Array.Copy(ret, RealByte, num);
ret=RealByte;returnret;
}//原始base64编码
public static byte[] Base64Encode(byte[] source)
{if ((source == null) || (source.Length == 0))throw new ArgumentException("source is not valid");
ToBase64Transform tb64= newToBase64Transform();
MemoryStream stm= newMemoryStream();int pos = 0;byte[] buff;while (pos + 3
{
buff= tb64.TransformFinalBlock(source, pos, 3);
stm.Write(buff,0, buff.Length);
pos+= 3;
}
buff= tb64.TransformFinalBlock(source, pos, source.Length -pos);
stm.Write(buff,0, buff.Length);returnstm.ToArray();
}//原始base64解码
public static byte[] Base64Decode(byte[] source)
{if ((source == null) || (source.Length == 0))throw new ArgumentException("source is not valid");
FromBase64Transform fb64= newFromBase64Transform();
MemoryStream stm= newMemoryStream();int pos = 0;byte[] buff;while (pos + 4
{
buff= fb64.TransformFinalBlock(source, pos, 4);
stm.Write(buff,0, buff.Length);
pos+= 4;
}
buff= fb64.TransformFinalBlock(source, pos, source.Length -pos);
stm.Write(buff,0, buff.Length);returnstm.ToArray();
}/**
* 把密钥转化为2进制byte[] 如果大于 24byte就取前24位 作为 密钥
**/
public static byte[] GetKey(stringsecret)
{if ((secret == null) || (secret.Length == 0))throw new ArgumentException("Secret is not valid");byte[] temp;
ASCIIEncoding ae= newASCIIEncoding();
temp=Hash(ae.GetBytes(secret));byte[] ret = new byte[Constants.Operation.KeySize];inti;if (temp.Length
{
System.Array.Copy(temp,0, ret, 0, temp.Length);for (i = temp.Length; i < Constants.Operation.KeySize; i++)
{
ret[i]= 0;
}
}elseSystem.Array.Copy(temp,0, ret, 0, Constants.Operation.KeySize);returnret;
}//比较两个byte数组是否相同
public static bool CompareByteArrays(byte[] source, byte[] dest)
{if ((source == null) || (dest == null))throw new ArgumentException("source or dest is not valid");bool ret = true;if (source.Length !=dest.Length)return false;else
if (source.Length == 0)return true;for (int i = 0; i < source.Length; i++)if (source[i] !=dest[i])
{
ret= false;break;
}returnret;
}//使用md5计算散列
public static byte[] Hash(byte[] source)
{if ((source == null) || (source.Length == 0))throw new ArgumentException("source is not valid");
MD5 m=MD5.Create();returnm.ComputeHash(source);
}///
///对传入的明文密码进行Hash加密,密码不能为中文///
/// 需要加密的明文密码
/// 经过Hash加密的密码
public static string HashPassword(stringoriPassword)
{if (string.IsNullOrEmpty(oriPassword))throw new ArgumentException("oriPassword is valid");
ASCIIEncoding acii= newASCIIEncoding();byte[] hashedBytes =Hash(acii.GetBytes(oriPassword));
StringBuilder sb= new StringBuilder(30);foreach (byte b inhashedBytes)
{
sb.AppendFormat("{0:X2}", b);
}returnsb.ToString();
}public static string EncryptMd5(stringencode)
{return FormsAuthentication.HashPasswordForStoringInConfigFile(encode, "md5");
}const string m_strEncryptKey = "kingfykj";#region DES加密字符串
/
// 注意:密钥必须为8位/
///
///加密字符串///
/// 明码
/// 加密后的密码
public static string DesEncryptFixKey(stringp_strInput)
{byte[] byKey = null;byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};try{
byKey= System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0, 8));
DESCryptoServiceProvider des= newDESCryptoServiceProvider();byte[] inputByteArray =Encoding.UTF8.GetBytes(p_strInput);
MemoryStream ms= newMemoryStream();
CryptoStream cs= newCryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray,0, inputByteArray.Length);
cs.FlushFinalBlock();returnConvert.ToBase64String(ms.ToArray());
}catch(System.Exception ex)
{throw(ex);
}
}#endregion
#region DES解密字符串
//const string m_strEncryptKey = "kinghuns"; zh注释
///
///解密字符串///
/// 加了密的字符串
/// 密钥
public static string DesDecryptFixKey(stringp_strInput)
{byte[] byKey = null;byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};byte[] inputByteArray = newByte[p_strInput.Length];try{
byKey= System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0, 8));
DESCryptoServiceProvider des= newDESCryptoServiceProvider();
inputByteArray=Convert.FromBase64String(p_strInput);
MemoryStream ms= newMemoryStream();
CryptoStream cs= newCryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray,0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding= newSystem.Text.UTF8Encoding();returnencoding.GetString(ms.ToArray());
}catch(System.Exception ex)
{throw(ex);
}
}#endregion
//const string m_strEncryptKey = "kinghuns";
}///
///类名称 :Constants///类说明 :加解密算法常量.///作者 :///完成日期 :///
public classConstants
{public structOperation
{public static readonly int KeySize = 24;public static readonly byte[] UnicodeOrderPrefix = new byte[2] { 0xFF, 0xFE};public static readonly byte[] UnicodeReversePrefix = new byte[2] { 0xFE, 0xFF};
}
}