|
Posted on
2004-07-16 00:48
hbiftsaa
阅读( 2940)
评论()
编辑
收藏
举报
在.NET中,我们可以直接使用System.Security.Cryptography空间中的通过加密/解密类进行操作. 这几天在玩数据加密的时候用到了. 感觉用起来不是很方便. 于是写了一个通用(hoho,算是比较通用的吧)的加密/解密的类. 现在只完成了对称加密类的封装. 代码:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
![](/Images/OutliningIndicators/None.gif)
namespace CnBlogs.Helper.hBifTs
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) AppEncrypt#region AppEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// AppEncrypt : 抽象类,用于提供加密/解密类的基本功能.
/// </summary>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public abstract class AppEncrypt : IDisposable {
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Private variable & propery#region Private variable & propery
protected byte[] key;
protected byte[] iv;
![](/Images/OutliningIndicators/InBlock.gif)
protected ICryptoTransform CryptoTrans;
protected SymmetricAlgorithm CryptObj;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public byte[] Key {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return key;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { key = value;}
}
![](/Images/OutliningIndicators/InBlock.gif)
public byte[] IV
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) get { return iv;}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) set { iv = value;}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public AppEncrypt(byte[] Key,byte[] IV) {
key = Key;
iv = IV;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Dispose#region Dispose
![](/Images/OutliningIndicators/InBlock.gif)
public virtual void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if( CryptoTrans != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CryptoTrans.Dispose();
CryptoTrans = null;
}
if( CryptObj != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CryptObj.Clear();
CryptObj = null;
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) Common Function#region Common Function
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 将Source中的数据进行加密/解密,结果输出到destination中.
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
protected virtual void Translate(Stream source,ref Stream destination)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
CryptoStream cstr = new CryptoStream(destination, CryptoTrans,CryptoStreamMode.Write);
long read = 0;
long length = source.Length;
int len;
byte[] bin = new byte[100];
while( read < length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
len = source.Read(bin,0,bin.Length);
cstr.Write(bin,0,len);
read += len;
cstr.Flush();
}
cstr.Close();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 加密
/// </summary>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public virtual MemoryStream Encrypt(Stream source) {
CryptoTrans = CryptObj.CreateEncryptor(key,iv);
Stream mem = new MemoryStream();
Translate(source,ref mem);
return (MemoryStream)mem;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 加密
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public virtual void Encrypt(Stream source,ref Stream destination) {
CryptoTrans = CryptObj.CreateEncryptor(key,iv);
Translate(source,ref destination);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 解密
/// </summary>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public virtual Stream Dencrypt(Stream source) {
CryptoTrans = CryptObj.CreateDecryptor(key,iv);
Stream mem = new MemoryStream();
Translate(source ,ref mem);
return (MemoryStream)mem;
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 解密
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public virtual void Dencrypt(Stream source,ref Stream destination) {
CryptoTrans = CryptObj.CreateDecryptor(key,iv);
Translate(source,ref destination);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// 创建随机密钥
/// </summary>
/// <param name="Key"></param>
/// <param name="IV"></param>
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public virtual void CreateRandomKey(out byte[] Key,out byte[] IV) {
CryptObj.GenerateKey();
Key = CryptObj.Key;
CryptObj.GenerateIV();
IV = CryptObj.IV;
}
#endregion
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) DESEncrypt#region DESEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// DESEncrypt : 提供DES加密方式
/// </summary>
public class DESEncrypt : AppEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
public DESEncrypt() : this(
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0x23, 0x50, 0xA3, 0x51, 0xA, 0x32, 0x10, 0xE8},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0x67, 0xDD, 0xF9, 0x7D, 0x19, 0x63, 0x8B, 0xBE}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ) {}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public DESEncrypt(byte[] Key,byte[] IV):base(Key,IV) {
CryptObj = new DESCryptoServiceProvider();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) TripleDESEncrypt#region TripleDESEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// TripleDESEncrypt : 提供三重DES加密方式
/// </summary>
public class TripleDESEncrypt : AppEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
public TripleDESEncrypt() : this(
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0x27,0xAA,0xA0,0xAB,0x43,0xEC,0x6E,0xEB,0xED,0xC6,0x37,0xD,0x81,0x48,0x2E,0x5F,0x2E,0xAB,0x81,0xAC,0xA8,0x85,0x2C,0x6,},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0xCA,0x9E,0xD4,0xBF,0xCB,0x4A,0x6,0x3D,}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ) {}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public TripleDESEncrypt(byte[] Key,byte[] IV) : base(Key,IV) {
CryptObj = new TripleDESCryptoServiceProvider();
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) RC2Encrypt#region RC2Encrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// RC2Encrypt : 提供RC2加密方式
/// </summary>
public class RC2Encrypt : AppEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
public RC2Encrypt():this(
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0x43,0x89,0x35,0x68,0xE4,0xE9,0x84,0x39,0x1B,0xB2,0x18,0x40,0x4A,0x91,0x5F,0x52,},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0xF9,0x29,0xEC,0x12,0x7A,0xE4,0xFC,0x87,}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ) {}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public RC2Encrypt(byte[] Key,byte[] IV) : base( Key,IV) {
CryptObj = new RC2CryptoServiceProvider();
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif) RijndaelEncrypt#region RijndaelEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// RijndaelEncrypt : 提供Rijndael加密方式
/// </summary>
public class RijndaelEncrypt : AppEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
public RijndaelEncrypt():this(
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0xB2,0xBC,0xEB,0xF5,0xA1,0xD9,0x7F,0x8A,0x37,0x95,0xE9,0xE7,0x42,0x81,0x9F,0xB,0x4C,
0xA,0x31,0x39,0xC9,0x25,0xFF,0xEA,0xFA,0x75,0x2,0xCB,0x16,0xCC,0xE5,0x13,},
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) new byte[] {0xD1,0x93,0x9F,0x7A,0xD1,0xA9,0xCA,0x2D,0x5D,0xD9,0x8C,0xF0,0xBC,0xBA,0x40,0x10,}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) ) {}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public RijndaelEncrypt(byte[] Key,byte[] IV):base(Key,IV) {
CryptObj = new RijndaelManaged();
}
}
#endregion
}
![](/Images/OutliningIndicators/None.gif) 使用只要直接调用基类的Encrypt和Dencrypt函数即可.. 测试用例如下:
using System;
using System.IO;
using System.Text;
using CnBlogs.Helper.hBifTs;
using NUnit.Framework;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
namespace CnBlogs.Helper.hBifTs.TestCases
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) /**//// <summary>
/// Summary description for TestAppEncrypt.
/// </summary>
///
[TestFixture]
public class TestAppEncrypt
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
private AppEncrypt enc;
private MemoryStream mem;
![](/Images/OutliningIndicators/InBlock.gif)
public TestAppEncrypt()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) [SetUp] public void Setup() {
byte[] msg = Encoding.Unicode.GetBytes("hello,I got the new world,猪啊..");
mem = new MemoryStream( msg);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) [TearDown] public void TearDown() {
mem.Close();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public void Common() {
Stream dest = new MemoryStream();
enc.Encrypt( mem,ref dest);
Stream dest1 = new MemoryStream( ((MemoryStream)dest).ToArray());
Stream dest2 = new MemoryStream();
enc.Dencrypt( dest1,ref dest2);
Assert.AreEqual( Encoding.Unicode.GetString(mem.ToArray()),Encoding.Unicode.GetString(((MemoryStream)dest2).ToArray()));
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) [Test] public void TestDES() {
enc = new DESEncrypt();
Common();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) [Test] public void TestTripleDES() {
enc = new TripleDESEncrypt();
Common();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) [Test] public void TestRC2() {
enc = new RC2Encrypt();
Common();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) [Test] public void TestRijndael() {
enc = new RijndaelEncrypt();
Common();
}
}
}
![](/Images/OutliningIndicators/None.gif) 应 灵感之源的要求,提供代码下载: AppEncrypts.zip
|