采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。需要对加密和解密使用相同密钥的加密算法。由于其速度快,对称性加密通常在消息发送方需要加密大量数据时使用。所谓对称,就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。因此加密的安全性不仅取决于加密算法本身,密钥管理的安全性更是重要。因为加密和解密都使用同一个密钥,如何把密钥安全地传递到解密者手上就成了必须要解决的问题。Rijndael类表示Rijndael对称加密算法的所有实现必须从其继承的基类。命名空间:System.Security.CryptographyRijndael.Create方法:创建加密对象以执行Rijndael算法。例:Rijndael crypt = Rijndael.Create();属性:IV :获取或设置对称算法的初始化向量。Key: 获取或设置对称算法的密钥。例:byte[] key = new byte[32] { 0XA6, 0X7D, 0XE1, 0X3F, 0X35, 0X0E, 0XE1, 0XA9, 0X83, 0XA5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0X98, 0XA7, 0X33, 0X49, 0XFF, 0XE6, 0XAE, 0XBF, 0X8D, 0X8D, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40 };byte[] iv = new byte[16] { 0XF8, 0X8B, 0X01, 0XFB, 0X08, 0X85, 0X9A, 0XA4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0XF6, 0X19 };crypt.Key = key;crypt.IV = iv;CreateEncryptor()方法用当前的Key属性和初始化相量(IV)创建对称加密对象。ICryptoTransform接口定义基本的加密转换运算。CrytoStream类定义将数据流链接到加密转换的流构造函数语法:public CrytoStream(Stream stream, ICryptoTransform transform, CrytoStreamMode mode);其中,stream表示对其执行加密转换的流;tranform表示要对流执行的加密转换。CryptoStream.Write方法:将一个字节序列写入当前CrytoStream,并将流中的当前位置提升写入的字节数。语法:public override void Write(byte[] buffer, int offset, int count);CryptoStream.FlushFinalBlock方法:用缓冲区的当前状态更新数据基础源或储存库,随后清除缓冲区。MemoryStream类创建其支持存储区为内存的流。构造函数:使用初始化为零的可扩展容量初始化MemoryStream类的新实例。MemoryStream ms = new MemoryStream();MenoryStream.Read方法从当前流中读取字节块并将数据写入buffer中语法:public override int Read(byte[] buffer, int offset, int count);UTF8Encoding类表示Unicode字符的UTF-8编码。构造函数:初始化UTF8Encoding类的新实例。System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();Encoding.GetBytes方法(string)将指定的String中的所有字符编码为一个字节序列。byte[] rawData = enc.GetBytes(text);Encoding.GetString方法(Byte[])将一个字节序列解码为一个字符串。enc.GetString(encryptedData);**************************************************************
加密函数:private string Encrypt(string text){ Rijndael crypt = Rijndael.Create();byte[] key = new byte[32] { 0XA6, 0X7D, 0XE1, 0X3F, 0X35, 0X0E, 0XE1, 0XA9, 0X83, 0XA5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0X98, 0XA7, 0X33, 0X49, 0XFF, 0XE6, 0XAE, 0XBF, 0X8D, 0X8D, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40 };byte[] iv = new byte[16] { 0XF8, 0X8B, 0X01, 0XFB, 0X08, 0X85, 0X9A, 0XA4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0XF6, 0X19 };crypt.Key = key;crypt.IV = iv;MemoryStream ms = new MemoryStream();ICryptoTransform transtormEncode = new ToBase64Transform();//Base64编码CryptoStream csEncode = new CryptoStream(ms, transtormEncode, CryptoStreamMode.Write);CryptoStream csEncrypt = new CryptoStream(csEncode, crypt.CreateEncryptor(), CryptoStreamMode.Write);System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();byte[] rawData = enc.GetBytes(text);csEncrypt.Write(rawData, 0, rawData.Length);csEncrypt.FlushFinalBlock();byte[] encryptedData = new byte[ms.Length];ms.Position = 0;ms.Read(encryptedData, 0, (int)ms.Length);return enc.GetString(encryptedData);}************************************************************************解密函数:private string Decrypt(string text){Rijndael crypt = Rijndael.Create();byte[] key = new byte[32] { 0XA6, 0X7D, 0XE1, 0X3F, 0X35, 0X0E, 0XE1, 0XA9, 0X83, 0XA5, 0X62, 0XAA, 0X7A, 0XAE, 0X79, 0X98, 0XA7, 0X33, 0X49, 0XFF, 0XE6, 0XAE, 0XBF, 0X8D, 0X8D, 0X20, 0X8A, 0X49, 0X31, 0X3A, 0X12, 0X40 };byte[] iv = new byte[16] { 0XF8, 0X8B, 0X01, 0XFB, 0X08, 0X85, 0X9A, 0XA4, 0XBE, 0X45, 0X28, 0X56, 0X03, 0X42, 0XF6, 0X19 };crypt.Key = key;crypt.IV = iv;MemoryStream ms = new MemoryStream();CryptoStream csDecrypt = new CryptoStream(ms, crypt.CreateDecryptor(), CryptoStreamMode.Write);ICryptoTransform transformDecode = new FromBase64Transform();CryptoStream csDecode = new CryptoStream(csDecrypt, transformDecode, CryptoStreamMode.Write);System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();byte[] rawData = enc.GetBytes(text);csDecode.Write(rawData, 0, rawData.Length);csDecode.FlushFinalBlock();byte[] decryptedData = new byte[ms.Length];ms.Position = 0;ms.Read(decryptedData, 0, (int)ms.Length);return (enc.GetString(decryptedData));}
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 1楼bflong昨天 16:27
- 不错,还有公钥加密也很常用