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. android sdk

    android sdk 网站 为什么google 的网站都上不去. http://www.androiddevtools.cn/

  2. [SRS流媒体]RTMP/HLS 直播服务器simple-rtmp-server安装

    一个采用MIT协议授权的国产的简单的RTMP/HLS 直播服务器,其核心的价值理念在于简单高效. 使用方法: tep 1: build srs tar xf simple-rtmp-server-*. ...

  3. tomcat7需要进行升级,因为有漏洞,而且安装包没有做过优化处理

    http://www.open-open.com/lib/view/open1401931407228.html http://www.cnblogs.com/ggjucheng/archive/20 ...

  4. jackson 注脚学习参考

    (1)初级我们从几个简单的使用场景开始:重命名属性,忽略属性,以及修改属性所使用的类型.注意:下面的例子仅仅显示了成员属性(field properties),注解同样也可以用在成员方法(getter ...

  5. JavaScript跨域方法

    一.什么是跨域 JavaScript出于安全方面的考虑,不允许跨域调用其他页面的对象.但在安全限制的同时也给注入iframe或是ajax应用上带来了不少麻烦.这里把涉及到跨域的一些问题简单地整理一下: ...

  6. WebService SendTimeout 超时问题

    System.TimeoutException: 请求通道在等待 00:01:00 以后答复时超时.增加传递给请求调用的超时值,或者增加绑定上的 SendTimeout 值.分配给此操作的时间可能是更 ...

  7. VC 2010的重大变化

    auto 关键字具有新的默认含义.由于使用旧含义的情况很少见,因此大多数应用程序都不会受此更改影响. 引入了新的 static_assert 关键字,如果代码中已经存在具有某个名称的标识符,则此关键字 ...

  8. PowerShell实现文件下载(类wget)

    对Linux熟悉的读者可能会对Linux通过wget下载文件有印象,这个工具功能很强大,在.NET环境下提到下载文件大多数人熟悉的是通过System.Net.WebClient进行下载,这个程序集能实 ...

  9. oracle 数据库时间类型为字符串 时间范围大小查询

    select * from invoicedetail t2 where t2.Memo is null and to_char(to_date(t2.PrintDate,'yyyy-MM-dd hh ...

  10. 一段时间没上来了,看到有很多网友对OWA感兴趣,因为所在公司发展方向的原因,没有太多时间继续深入研究OWA,敬请见谅

    一段时间没上来了,看到有很多网友对OWA感兴趣,因为所在公司发展方向的原因,没有太多时间继续深入研究OWA,敬请见谅