using System;
using System.Security.Cryptography ;
using System.Text;
using System.IO;
namespace SEDO
{
/// <summary>
/// SEDO 的摘要說明。
/// SEDO 實現的是用一個封裝了4種對稱加密方法(Des,Rc2,Rijndael,TripleDes)的組件
///
/// 注意事項:
/// 1:TripleDes和Rijndael加密/解密對象使用16或者24位byte的Key
/// 2:Rijndael只能使用16位的初始化向量IV
/// 3:Des和Rc2均使用8位Byte的Key和IV
/// 4:對需要加密/解密的數據流采用何種方法進行編碼/解碼,由調用組件的用戶自己決定
/// 5:密鑰和初始化向量IV由使用者自己定義
/// 程序員: 王海波 2003-05-19 [email protected]
/// </summary>
//定義加密類型的枚舉
public enum EncryptionAlgorithm {Des = 1, Rc2, Rijndael, TripleDes};
//定義加密類
internal class EncryptTransformer
{
private EncryptionAlgorithm algorithmID;
private byte[] initVec;
private byte[] encKey;
internal EncryptTransformer(EncryptionAlgorithm algId)
{
//Save the algorithm being used.
algorithmID = algId;
}
internal ICryptoTransform GetCryptoServiceProvider(byte[] bytesKey)
{
//當數據密鑰Key或者初始化向量IV為空的時候,將使用加密對象自動產生的密鑰Key或者初始化向量IV
switch (algorithmID)
{
case EncryptionAlgorithm.Des:
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
// See if a key was provided
if (null == bytesKey)
{
encKey = des.Key;
}
else
{
des.Key = bytesKey;
encKey = des.Key;
}
// See if the clIEnt provided an initialization vector
if (null == initVec)
{ // Have the algorithm create one
initVec = des.IV;
}
else
{ //No, give it to the algorithm
des.IV = initVec;
}
return des.CreateEncryptor();
}
case EncryptionAlgorithm.TripleDes:
{
TripleDES des3 = new TripleDESCryptoServiceProvider();
des3.Mode = CipherMode.CBC;
// See if a key was provided
if (null == bytesKey)
{
encKey = des3.Key;
}
else
{
des3.Key = bytesKey;
encKey = des3.Key;
}