using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Linq;
//摘自 http://topic.csdn.net/u/20101231/09/039bdd22-3f0a-4bbb-908e-d887b4d7bbfe.html?19933
#region 对称加密类
/// <summary>
/// 对称加密解密类
/// </summary>
public static class SymmetricEncryptClass
{
#region 对称加密代码
/// <summary>
/// 对称加密方法
/// </summary>
/// <param name="strMsg">要加密的数据</param>
/// <param name="key">加密Key</param>
/// <param name="IV">加密向量</param>
/// <returns>返回加密后的数据,类型byte[]</returns>
public static byte[] SymmetricEncrptFunction(this string strMsg, string key, string IV)
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
SHA1 sha1 = new SHA1CryptoServiceProvider();
//初始密钥的设定
string strNamePlusBirthday = key + IV;
string strKey = new string(strNamePlusBirthday.Reverse().ToArray());
if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length > tdes.Key.Length)
{
tdes.Key = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().Substring(0, tdes.Key.Length));
}
else if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length < tdes.Key.Length)
{
tdes.Key = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().PadRight(tdes.Key.Length, ' '));
}
//初始向量的设定
if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length > tdes.IV.Length)
{
tdes.IV = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().Substring(0, tdes.IV.Length));
}
else if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length < tdes.IV.Length)
{
tdes.IV = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().PadRight(tdes.IV.Length, ' '));
}
byte[] encryptBuffer = Encoding.Default.GetBytes(strMsg);
MemoryStream streamMemory = new MemoryStream();
CryptoStream cryStream = null;//加密流
byte[] readBuffer;//用来接受加密数据
try
{
cryStream = new CryptoStream(streamMemory, new TripleDESCryptoServiceProvider().CreateEncryptor(tdes.Key, tdes.IV), CryptoStreamMode.Write);
cryStream.Write(encryptBuffer, 0, encryptBuffer.Length);
cryStream.FlushFinalBlock();
}
catch
{
return null;
}
finally
{
readBuffer = streamMemory.ToArray();
cryStream.Close();
streamMemory.Close();
}
return readBuffer;
}
#endregion
#region 对称解密代码
/// <summary>
/// 对称解密方法
/// </summary>
/// <param name="bufMsg">需要解密的数据</param>
/// <param name="key">解密Key</param>
/// <param name="IV">解密向量</param>
/// <returns>解密后的数据</returns>
public static string SymmetricDecryptionFunction(this byte[] bufMsg, string key, string IV)
{
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
SHA1 sha1 = new SHA1CryptoServiceProvider();
//初始密钥的设定
string strNamePlusBirthday = key + IV;
string strKey = new string(strNamePlusBirthday.Reverse().ToArray());
if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length > tdes.Key.Length)
{
tdes.Key = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().Substring(0, tdes.Key.Length));
}
else if (sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).Length < tdes.Key.Length)
{
tdes.Key = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strKey)).ToString().PadRight(tdes.Key.Length, ' '));
}
//初始向量的设定
if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length > tdes.IV.Length)
{
tdes.IV = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().Substring(0, tdes.IV.Length));
}
else if (sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).Length < tdes.IV.Length)
{
tdes.IV = Encoding.Default.GetBytes(sha1.ComputeHash(Encoding.Default.GetBytes(strNamePlusBirthday)).ToString().PadRight(tdes.IV.Length, ' '));
}
byte[] readBuffer = bufMsg;
MemoryStream streamRead = new MemoryStream(readBuffer);
CryptoStream deCryStream = null;
byte[] bufferReader;
try
{
deCryStream = new CryptoStream(streamRead, new TripleDESCryptoServiceProvider().CreateDecryptor(tdes.Key, tdes.IV), CryptoStreamMode.Read);
bufferReader = new byte[readBuffer.Length];
deCryStream.Read(bufferReader, 0, bufferReader.Length);
}
finally
{
deCryStream.Close();
streamRead.Close();
}
string deCryString = Encoding.Default.GetString(bufferReader);
return deCryString;
}
#endregion
}
#endregion