internal class EncryptTransform
{
//private const int c_MaxLengthOf_IV_DES = 4;
//private const int c_MaxLengthOf_IV_RC2 = 4;
//private const int c_MaxLengthOf_IV_RIJNDAEL = 8;
//private const int c_MaxLengthOf_IV_TRIPLEDES = 4;
//private const int c_MaxLengthOf_Key_DES = 4;
//private const int c_MaxLengthOf_Key_RC2 = 4;
//private const int c_MaxLengthOf_Key_RIJNDAEL_1 = 8;
//private const int c_MaxLengthOf_Key_RIJNDAEL_2 = 12;
//private const int c_MaxLengthOf_Key_TRIPLEDES_1 = 8;
//private const int c_MaxLengthOf_Key_TRIPLEDES_2 = 12; private byte[] m_Key = null; /// <summary>
/// 获取或设置加密解密过程中使用的明文密码
/// </summary>
public byte[] Key
{
get { return m_Key; }
set { m_Key = value; }
} private byte[] m_initVec = null;
/// <summary>
/// 获取或设置加密解密过程中使用的初始化向量
/// </summary>
public byte[] InitVec
{
get { return m_initVec; }
set { m_initVec = value; }
} public EncryptTransform(byte[] key, byte[] initVector)
{
this.m_Key = key;
this.m_initVec = initVector;
}
/// <summary>
/// 根据提供的枚举信息,获得需要使用的解密算法的接口
/// </summary>
/// <param name="algorithm"></param>
/// <returns></returns>
internal ICryptoTransform GetDecryptoServiceProvider(EncryptionAlgorithm algorithm)
{
switch (algorithm)
{
case EncryptionAlgorithm.Des:
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
if (this.m_Key != null)
{
des.Key = m_Key;
}
if (m_initVec != null)
{
des.IV = m_initVec;
}
return des.CreateDecryptor();
}
case EncryptionAlgorithm.Rc2:
{
RC2 rc = new RC2CryptoServiceProvider();
rc.Mode = CipherMode.CBC;
return rc.CreateDecryptor(m_Key, m_initVec);
}
case EncryptionAlgorithm.Rijndael:
{
Rijndael rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
return rijndael.CreateDecryptor(m_Key, m_initVec);
}
case EncryptionAlgorithm.TripleDes:
{
TripleDES edes = new TripleDESCryptoServiceProvider();
edes.Mode = CipherMode.CBC;
return edes.CreateDecryptor(m_Key, m_initVec);
}
}
throw new CryptographicException("Algorithm ID '" + algorithm + "' not supported.");
} /// <summary>
/// 根据提供的枚举信息,获得需要使用的加密算法的接口
/// </summary>
/// <param name="algorithm">算法枚举</param>
/// <returns></returns>
internal ICryptoTransform GetEncryptoServiceProvider(EncryptionAlgorithm algorithm)
{ switch (algorithm)
{
case EncryptionAlgorithm.Des:
{
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.CBC;
if (null != m_Key)
{
des.Key = m_Key;
}
else
{
m_Key = des.Key;
}
if (null != m_initVec)
{
des.IV = m_initVec;
}
else
{
m_initVec = des.IV;
}
return des.CreateEncryptor();
} case EncryptionAlgorithm.Rc2:
{
RC2 rc = new RC2CryptoServiceProvider();
rc.Mode = CipherMode.CBC;
if (null != m_Key)
{
rc.Key = m_Key;
}
else
{
m_Key = rc.Key;
}
if (null != m_initVec)
{
rc.IV = m_initVec;
}
else
{
m_initVec = rc.IV;
}
return rc.CreateEncryptor();
}
case EncryptionAlgorithm.Rijndael:
{
Rijndael rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
if (null != m_Key)
{
rijndael.Key = m_Key;
}
else
{
m_Key = rijndael.Key;
}
if (null != m_initVec)
{
rijndael.IV = m_initVec;
}
else
{
m_initVec = rijndael.IV;
}
return rijndael.CreateEncryptor();
}
case EncryptionAlgorithm.TripleDes:
{
TripleDES edes = new TripleDESCryptoServiceProvider();
edes.Mode = CipherMode.CBC;
if (null != m_Key)
{
edes.Key = m_Key;
}
else
{
m_Key = edes.Key;
}
if (null != m_initVec)
{
edes.IV = m_initVec;
}
else
{
m_initVec = edes.IV;
}
return edes.CreateEncryptor();
}
default:
throw new CryptographicException("Algorithm ID '" + algorithm + "' not supported.");
} } /// <summary>
/// 提供具体实现解密的方法
/// </summary>
/// <param name="algorithm"></param>
/// <param name="bytesData">需要解密的信息</param>
/// <returns></returns>
public byte[] Decrypt(EncryptionAlgorithm algorithm, byte[] bytesData)
{
byte[] result = new byte[];
using (MemoryStream stream = new MemoryStream())
{
ICryptoTransform cryptoServiceProvider = GetDecryptoServiceProvider(algorithm);
using (CryptoStream stream2 = new CryptoStream(stream, cryptoServiceProvider, CryptoStreamMode.Write))
{
try
{
stream2.Write(bytesData, , bytesData.Length);
stream2.FlushFinalBlock();
stream2.Close();
result = stream.ToArray();
}
catch (Exception exception)
{
throw new Exception("Error while writing decrypted data to the stream: \n" + exception.Message);
}
}
stream.Close();
}
return result;
}
/// <summary>
/// 提供具体实现加密的方法
/// </summary>
/// <param name="algorithm"></param>
/// <param name="bytesData">需要加密的信息</param>
/// <returns></returns>
public byte[] Encrypt(EncryptionAlgorithm algorithm, byte[] bytesData)
{
byte[] result = new byte[];
using (MemoryStream stream = new MemoryStream())
{
ICryptoTransform cryptoServiceProvider = this.GetEncryptoServiceProvider(algorithm);
using (CryptoStream stream2 = new CryptoStream(stream, cryptoServiceProvider, CryptoStreamMode.Write))
{
try
{
stream2.Write(bytesData, , bytesData.Length);
stream2.FlushFinalBlock();
stream2.Close();
result = stream.ToArray();
}
catch (Exception exception)
{
throw new Exception("Error while writing encrypted data to the stream: \n" + exception.Message);
}
}
stream.Close();
}
return result;
} }

EncryptTransform的更多相关文章

  1. EncryptionHelper

    public static class EncryptionHelper { #region const /// <summary> /// 默认使用的适合于DES,RC2算法的Key / ...

  2. [C#]AES加密算法实现

    密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替代原先的DES,已经被多方分 ...

  3. C#简单的加密类

    1.加密 public class EncryptHepler { // 验值 static string saltValue = "XXXX"; // 密码值 static st ...

  4. Silverlight信息加密 - 通过Rfc2898DeriveBytes类使用基于HMACSHA1的伪随机数生成器实现PBKDF2

    原文: http://blog.csdn.net/xuyue1987/article/details/6706600 在上一篇文章当中,介绍到了通过Silverlight获取web.config中的值 ...

随机推荐

  1. [转]如果我有jQuery背景,我应该如何切换到AngularJS的思维模式?

    导言 stackoverflow上有一个人问了一个问题:如果我有jQuery背景,我应该如何切换到AngularJS的思维模式? 有一个回复非常经典,获得了两千多票. 为了让国内开发者也能领略到其中的 ...

  2. PreparedStatement ResultSet

    public int searchProblemDistinctCount() throws Exception { DBOperator dbo = getDBOperator(); try { P ...

  3. libevent

    libevent doc example #include <event2/event.h> void cb_func(evutil_socket_t fd, short what, vo ...

  4. nginx-upstream-keepalive;accept_mutex-proxy_http_version-1.1-proxy_set_header-connection

    nginx+resin+redis+mysql的架构: 有外部模拟用户请求的大量攻击:它尝试请求了80就断开:看到的现象是在跑有nginx80的主机上TCP连接数少 : ESTABLISHED少: 在 ...

  5. 每日英语:why can't China produce world-class CEO?

    The appointment of India-born Satya Nadella as Microsoft Corp.'s CEO has caused a bit of a stir in C ...

  6. wdlinux 一键安装

    Linux系统(支持CentOS 6.X/7.X.RedHat 6.X.Ubuntu 12.04): ssh登录服务器,执行如下操作即可,需root用户身份安装 wget http://dl.wdli ...

  7. Quartz.Net 作业调度后台管理系统,基于Extjs

    Quartz.Net是一个开源的.非常灵活的作业调度框架,具体使用方法和教程:http://www.cnblogs.com/shanyou/archive/2007/08/25/quartznettu ...

  8. 使用 jsErrLog 分析 js 报错

    1. github 地址: https://github.com/Offbeatmammal/jsErrLog/tree/master/src 2. 在所有页面引入 jsErrLog,配置出错时打日志 ...

  9. Gamma校正与线性工作流

    1 Gamma校正是什么?8位亮度值x(0-1)经过x^0.45的一个提亮过程. 2 为什么需要Gamma校正 人的眼睛是以非线性方式感知亮度,在自然界中,人感觉到的一半亮度其实只有全部能量的0.2, ...

  10. X509证书中RSA公钥的提取与载入

    原文链接: http://blog.chinaunix.net/uid-16515626-id-2741894.html   由于项目需要,我计划利用openssl开发一个基本的CA,实现证书的发放等 ...