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