using System;
using System.IO;
using System.Text;
using System.Security;
using System.Security.Cryptography;
namespace Common
{
/**//**/
/**//// <summary>
/// 加密,解密功能函數
/// </summary>
public class EncryptionUtil
{
public EncryptionUtil()
{
//
// TODO: Add constructor logic here
//
}
static private Byte[] m_Key = new Byte[8];
static private Byte[] m_IV = new Byte[8];
//為了安全,直接將key寫死在文件中,你也可以用一個屬性來實現
public string key = "(@jhtchina@)";
/**//**/
/**///////////////////////////
//加密函數
public string EncryptData(String strKey, String strData)
{
string strResult; //Return Result
//1. 字符大小不能超過 90Kb. 否則, 緩存容易溢出(看第3點)
if (strData.Length > 92160)
{
strResult = "Error. Data String too large. Keep within 90Kb.";
return strResult;
}
//2. 生成key
if (!InitKey(strKey))
{
strResult = "Error. Fail to generate key for encryption";
return strResult;
}
//3. 准備處理的字符串
//字符串的前5個字節用來存儲數據的長度
//用這個簡單的方法來記住數據的初始大小,沒有用太復雜的方法
strData = String.Format("{0,5:00000}" + strData, strData.Length);
//4. 加密數據
byte[] rbData = new byte[strData.Length];
ASCIIEncoding aEnc = new ASCIIEncoding();
aEnc.GetBytes(strData, 0, strData.Length, rbData, 0);
//加密功能實現的主要類
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
ICryptoTransform desEncrypt = descsp.CreateEncryptor(m_Key, m_IV);
//5. 准備stream
// mOut是輸出流.
// mStream是輸入流
// cs為轉換流
MemoryStream mStream = new MemoryStream(rbData);
CryptoStream cs = new CryptoStream(mStream, desEncrypt, CryptoStreamMode.Read);
MemoryStream mOut = new MemoryStream();
//6. 開始加密
int bytesRead;
byte[] output = new byte[1024];
do
{
bytesRead = cs.Read(output, 0, 1024);
if (bytesRead != 0)
mOut.Write(output, 0, bytesRead);
} while (bytesRead > 0);
//7. 返回加密結果
//因為是一個web項目,在這裡轉換為base64,因此在http上是不會出錯的
if (mOut.Length == 0)
strResult = "";
else
strResult = Convert.ToBase64String(mOut.GetBuffer(), 0, (int)mOut.Length);
return strResult;
}
/**//**/
/**///////////////////////////
//解密函數
public string DecryptData(String strKey, String strData)
{
string strResult;
//1. 生成解密key
if (!InitKey(strKey))
{
strResult = "Error. Fail to generate key for decryption";
return strResult;
}
//2. 初始化解密的主要類
int nReturn = 0;
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
ICryptoTransform desDecrypt = descsp.CreateDecryptor(m_Key, m_IV);
//3. 准備stream
// mOut為輸出流
// cs為轉換流
MemoryStream mOut = new MemoryStream();
CryptoStream cs = new CryptoStream(mOut, desDecrypt, CryptoStreamMode.Write);
byte[] bPlain = new byte[strData.Length];
try
{
bPlain = Convert.FromBase64CharArray(strData.ToCharArray(), 0, strData.Length);
}
catch (Exception)
{
strResult = "Error. Input Data is not base64 encoded.";
return strResult;
}
long lRead = 0;
long lTotal = strData.Length;
try
{
//5. 完成解密
while (lTotal >= lRead)
{
cs.Write(bPlain, 0, (int)bPlain.Length);
lRead = mOut.Length + Convert.ToUInt32(((bPlain.Length / descsp.BlockSize) * descsp.BlockSize));
};
ASCIIEncoding aEnc = new ASCIIEncoding();
strResult = aEnc.GetString(mOut.GetBuffer(), 0, (int)mOut.Length);
//6.去處存儲長度的前5個字節的數據
String strLen = strResult.Substring(0, 5);
int nLen = Convert.ToInt32(strLen);
strResult = strResult.Substring(5, nLen);
nReturn = (int)mOut.Length;
return strResult;
}
catch (Exception)
{
strResult = "Error. Decryption Failed. Possibly due to incorrect Key or corrputed data";
return strResult;
}
}
/**//**/
/**//////////////////////////////////////////////////////////////
//生成key的函數
static private bool InitKey(String strKey)
{
try
{
// 轉換key為字節流
byte[] bp = new byte[strKey.Length];
ASCIIEncoding aEnc = new ASCIIEncoding();
aEnc.GetBytes(strKey, 0, strKey.Length, bp, 0);
SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
byte[] bpHash = sha.ComputeHash(bp);
int i;
// 生成初始化DESCryptoServiceProvider的參數
for (i = 0; i < 8; i++)
m_Key[i] = bpHash[i];
for (i = 8; i < 16; i++)
m_IV[i - 8] = bpHash[i];
return true;
}
catch (Exception)
{
//錯誤處理
return false;
}
}
}
}
/**//*
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
namespace test11
{
[TestFixture]
public class Class1
{
public string test111(string currentValue)
{
//Console.Out.Write("11");
EncryptionUtil EU = new EncryptionUtil();
//int count = Convert.ToInt32(EU.DecryptData(EU.key, currentValue));
return EU.DecryptData(EU.key, EU.DecryptData(EU.key, currentValue));
}
public string test222(string currentValue)
{
EncryptionUtil EU = new EncryptionUtil();
return EU.EncryptData(EU.key, EU.EncryptData(EU.key, currentValue));
}
[Test]
public void test_Main()
{
string str_Pass = test222("0123456789");
Console.Out.WriteLine(str_Pass);
string str_Number = test111(str_Pass);
Console.Out.WriteLine(str_Number);
}
}
}
*/
//
//