摘抄一 AES简介
AES(Advanced Encryption Standard,高级加密标准)又叫Rijndael加密法,用来替代DES算法。常见AES加密模式有ECB、CBC、CFB、OFB和CTR等五种, CFB、OFB都带反馈,做流加密用的多,CBC和CTR、ECB多用于独立block加密,由于ECB算法有点小缺点,所以CBC和CTR这两种加解密方式用的较多,也是很多标准规范要求的实现算法。
AES跟Rijndael相比有点小区别,就是使用固定块(block size)为128bits(16字节)(原Rijndael块大小更灵活),密钥长度支持128、192或256位。
(http://www.metsky.com/archives/585.html)
摘抄二 向量的作用
DES一共就有4个参数参与运作:明文、密文、密钥、向量。为了初学者容易理解,可以把4个参数的关系写成:密文=明文+密钥+向量;明文=密文-密钥-向量。为什么要向量这个参数呢?因为如果有一篇文章,有几个词重复,那么这个词加上密钥形成的密文,仍然会重复,这给破解者有机可乘,破解者可以根据重复的内容,猜出是什么词,然而一旦猜对这个词,那么,他就能算出密钥,整篇文章就被破解了!加上向量这个参数以后,每块文字段都会依次加上一段值,这样,即使相同的文字,加密出来的密文,也是不一样的,算法的安全性大大提高!
(http://blog.csdn.net/hnzhangshilong/article/details/7334469)
摘抄三 什么是对称加密算法
采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密。
需要对加密和解密使用相同密钥的加密算法。由于其速度,对称性加密通常在消息发送方需要加密大量数据时使用。对称性加密也称为密钥加密。
所谓对称,就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。
因此对称式加密本身不是安全的。
常用的对称加密有:
DES、IDEA、RC2、RC4、SKIPJACK、RC5、AES算法等
(百度百科,对称加密算法)
摘抄四 .Net里的对称加密算法
.NET Framework类库提供了对称加密、散列函数、非对称加密、数字签名等现有的主流加密算法。.NET中默认实现了4种对称加密算法:DES、TripleDES、RC2、Rijndeal。其中前3种都比较老了哦。而第四种Rijndeal的全称就是:高级加密标准(Advanced Encryption Standard,AES)也就是我们说的AES,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。
2.C#实现AES的加解密
顺便提一下,有些人搞不懂为什么加密后数据会变长,这是因为对称加密算法是分组加密,也就是按照预先设定好的块大小进行加密。加密时,首先将明文进行分块,对于最后一块,如果少于块的大小,是需要进行填充的。因此有不同的填充模式。还有有的时候,解密数据时报错:数据长度不符合要求时,也多半是由于程序中加密出了错,因为加密后的密文应该是块大小的整数倍的。.NET中的密码算法可以进行很多配置,还可以进行扩展。如采用不同的链接模式,填充模式、块大小,密钥长度等。需要详细了解的话可以先看一下对称加密的基础知识,再参考下MSDN。下面是用C#实现的AES加解密算法:AES密钥默认长度是128位的。代码如下:
using System; using System.Security.Cryptography; using System.Text; using System.IO; namespace GraduationDesign { class AESEncryption { //默认密钥向量 private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> /// AES加密算法 /// </summary> /// <param name="plainText">明文字符串</param> /// <param name="strKey">密钥</param> /// <returns>返回加密后的密文字节数组</returns> public static byte[] AESEncrypt(string plainText , string strKey ) { //分组加密算法 SymmetricAlgorithm des = Rijndael .Create () ; byte[] inputByteArray =Encoding .UTF8 .GetBytes (plainText ) ;//得到需要加密的字节数组 //设置密钥及密钥向量 des.Key =Encoding.UTF8.GetBytes (strKey ); des.IV = _key1 ; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); byte[] cipherBytes = ms .ToArray () ;//得到加密后的字节数组 cs.Close(); ms.Close(); return cipherBytes ; } /// <summary> /// AES解密 /// </summary> /// <param name="cipherText">密文字节数组</param> /// <param name="strKey">密钥</param> /// <returns>返回解密后的字符串</returns> public static byte[] AESDecrypt(byte[] cipherText , string strKey ) { SymmetricAlgorithm des = Rijndael .Create () ; des.Key =Encoding.UTF8.GetBytes (strKey ); des.IV = _key1 ; byte[] decryptBytes = new byte[cipherText .Length ] ; MemoryStream ms = new MemoryStream(cipherText ) ; CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor (), CryptoStreamMode.Read ); cs.Read (decryptBytes , 0, decryptBytes.Length); cs.Close(); ms.Close(); return decryptBytes ; } } class Program { static void Main(string[] args) { string text = "AES加密算法测试数据"; //明文 string keys="dongbinhuiasxiny";//密钥,128位 byte[] encryptBytes = AESEncryption.AESEncrypt (text, keys ); //将加密后的密文转换为Base64编码,以便显示,可以查看下结果 Console.WriteLine ("明文:"+text ); Console.WriteLine ("密文:"+Convert.ToBase64String (encryptBytes )); //解密 byte[] decryptBytes = AESEncryption.AESDecrypt (encryptBytes, keys); //将解密后的结果转换为字符串,也可以将该步骤封装在解密算法中 string result = Encoding.UTF8.GetString(decryptBytes); Console.WriteLine("解密结果:"+result); Console.Read(); } }
下面是运行结果:
明文:AES加密算法测试数据
密文:UNzLn8AbUUGyNkwYAK2+Crgg+HGyOfak6+ou6Gz7zPE=
解密结果:AES加密算法测试数据
摘抄五
#region DES加密和解密 /// <summary> /// DES加密 /// </summary> /// <param name="Data">被加密的明文</param> /// <param name="Key">密钥</param> /// <param name="Vector">向量</param> /// <returns>密文</returns> public static Byte[] DESEncrypt(Byte[] Data, String Key, String Vector) { //将密钥转为字节数组,取其前8位 Byte[] bKey = new Byte[8]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); //将向量转为字节数组,取其前8位 Byte[] bVector = new Byte[8]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] Cryptograph = null; // 加密后的密文 DESCryptoServiceProvider EncryptProvider = new DESCryptoServiceProvider(); EncryptProvider.Mode = CipherMode.CBC; EncryptProvider.Padding = PaddingMode.Zeros; try { // 开辟一块内存流 using (MemoryStream Memory = new MemoryStream()) { // 把内存流对象包装成加密流对象 using (CryptoStream Encryptor = new CryptoStream(Memory, EncryptProvider.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write)) { // 明文数据写入加密流 Encryptor.Write(Data, 0, Data.Length); Encryptor.FlushFinalBlock(); Cryptograph = Memory.ToArray(); } } } catch { Cryptograph = null; } return Cryptograph; } /// <summary> /// DES解密 /// </summary> /// <param name="Data">被解密的密文</param> /// <param name="Key">密钥</param> /// <param name="Vector">向量</param> /// <returns>明文</returns> public static Byte[] DESDecrypt(Byte[] Data, String Key, String Vector) { Byte[] bKey = new Byte[8]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); Byte[] bVector = new Byte[8]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] original = null; DESCryptoServiceProvider CryptoProvider = new DESCryptoServiceProvider(); CryptoProvider.Mode = CipherMode.CBC; CryptoProvider.Padding = PaddingMode.Zeros; try { // 开辟一块内存流,存储密文 using (MemoryStream Memory = new MemoryStream(Data)) { // 把内存流对象包装成加密流对象 using (CryptoStream Decryptor = new CryptoStream(Memory, CryptoProvider.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read)) { // 明文存储区 using (MemoryStream originalMemory = new MemoryStream()) { Byte[] Buffer = new Byte[1024]; Int32 readBytes = 0; while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0) { originalMemory.Write(Buffer, 0, readBytes); } original = originalMemory.ToArray(); } } } } catch { original = null; } return original; } #endregion #region AES加密和解密 /// <summary> /// AES加密 /// </summary> /// <param name="Data">被加密的明文</param> /// <param name="Key">密钥</param> /// <param name="Vector">向量</param> /// <returns>密文</returns> public static Byte[] AESEncrypt(Byte[] Data, String Key, String Vector) { Byte[] bKey = new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); Byte[] bVector = new Byte[16]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] Cryptograph = null; // 加密后的密文 Rijndael Aes = Rijndael.Create(); try { // 开辟一块内存流 using (MemoryStream Memory = new MemoryStream()) { // 把内存流对象包装成加密流对象 using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write)) { // 明文数据写入加密流 Encryptor.Write(Data, 0, Data.Length); Encryptor.FlushFinalBlock(); Cryptograph = Memory.ToArray(); } } } catch { Cryptograph = null; } return Cryptograph; } /// <summary> /// AES解密 /// </summary> /// <param name="Data">被解密的密文</param> /// <param name="Key">密钥</param> /// <param name="Vector">向量</param> /// <returns>明文</returns> public static Byte[] AESDecrypt(Byte[] Data, String Key, String Vector) { Byte[] bKey = new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length); Byte[] bVector = new Byte[16]; Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length); Byte[] original = null; // 解密后的明文 Rijndael Aes = Rijndael.Create(); try { // 开辟一块内存流,存储密文 using (MemoryStream Memory = new MemoryStream(Data)) { // 把内存流对象包装成加密流对象 using (CryptoStream Decryptor = new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read)) { // 明文存储区 using (MemoryStream originalMemory = new MemoryStream()) { Byte[] Buffer = new Byte[1024]; Int32 readBytes = 0; while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0) { originalMemory.Write(Buffer, 0, readBytes); } original = originalMemory.ToArray(); } } } } catch { original = null; } return original; } #endregion
using System; using System.Text; using System.Security.Cryptography; using System.IO; namespace MyCryptography { /// <summary> /// DES加密解密 /// </summary> public class DES { /// <summary> /// 获取密钥 /// </summary> private static string Key { get { return @"P@+#wG+Z"; } } /// <summary> /// 获取向量 /// </summary> private static string IV { get { return @"L%n67}G/Mk@k%:~Y"; } } /// <summary> /// DES加密 /// </summary> /// <param name="plainStr">明文字符串</param> /// <returns>密文</returns> public static string DESEncrypt(string plainStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } } catch { } des.Clear(); return encrypt; } /// <summary> /// DES解密 /// </summary> /// <param name="encryptStr">密文字符串</param> /// <returns>明文</returns> public static string DESDecrypt(string encryptStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } } catch { } des.Clear(); return decrypt; } } /// <summary> /// AES加密解密 /// </summary> public class AES { /// <summary> /// 获取密钥 /// </summary> private static string Key { get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; } } /// <summary> /// 获取向量 /// </summary> private static string IV { get { return @"L+/~f4,Ir)b$=pkf"; } } /// <summary> /// AES加密 /// </summary> /// <param name="plainStr">明文字符串</param> /// <returns>密文</returns> public static string AESEncrypt(string plainStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Encoding.UTF8.GetBytes(plainStr); string encrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); encrypt = Convert.ToBase64String(mStream.ToArray()); } } } catch { } aes.Clear(); return encrypt; } /// <summary> /// AES加密 /// </summary> /// <param name="plainStr">明文字符串</param> /// <param name="returnNull">加密失败时是否返回 null,false 返回 String.Empty</param> /// <returns>密文</returns> public static string AESEncrypt(string plainStr, bool returnNull) { string encrypt = AESEncrypt(plainStr); return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt); } /// <summary> /// AES解密 /// </summary> /// <param name="encryptStr">密文字符串</param> /// <returns>明文</returns> public static string AESDecrypt(string encryptStr) { byte[] bKey = Encoding.UTF8.GetBytes(Key); byte[] bIV = Encoding.UTF8.GetBytes(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; Rijndael aes = Rijndael.Create(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write)) { cStream.Write(byteArray, 0, byteArray.Length); cStream.FlushFinalBlock(); decrypt = Encoding.UTF8.GetString(mStream.ToArray()); } } } catch { } aes.Clear(); return decrypt; } /// <summary> /// AES解密 /// </summary> /// <param name="encryptStr">密文字符串</param> /// <param name="returnNull">解密失败时是否返回 null,false 返回 String.Empty</param> /// <returns>明文</returns> public static string AESDecrypt(string encryptStr, bool returnNull) { string decrypt = AESDecrypt(encryptStr); return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt); } } }