好久没有逛论坛了,找工作比较累啊。毕业就意味这失业。也好久没写文章了,今天到论坛上碰到一个网友问AES加密的问题,忙活了个把小时,才发现那哥们真粗心,写错了个对象,导致解密错误。反正闲着无事,把以前写的代码拿出来,改了下。贴出来,也让大家以后熟悉熟悉。顺序介绍下.NET中的加密算法,有时间再来写篇关于.NET下加密算法扩展的文章。由于一般大家用到的都是对称加密算法,所以今天只说这个。

1.关于.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位的。代码如下:

1. using System;  
2. using System.Security.Cryptography;  
3. using System.Text;  
4. using System.IO;  
5.   
6. namespace GraduationDesign  
7. {      
8. class AESEncryption   
9.     {          
10. //默认密钥向量   
11. private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56,  0x78, 0x90, 0xAB, 0xCD, 0xEF };   
12.   
13. /// <summary>  
14. /// AES加密算法  
15. /// </summary>  
16. /// <param name="plainText">明文字符串</param>  
17. /// <param name="strKey">密钥</param>  
18. /// <returns>返回加密后的密文字节数组</returns>  
19. public static byte[] AESEncrypt(string plainText , string strKey )  
20.         {  
21. //分组加密算法  
22.             SymmetricAlgorithm des = Rijndael .Create () ;                
23. byte[] inputByteArray =Encoding .UTF8  .GetBytes (plainText ) ;//得到需要加密的字节数组      
24. //设置密钥及密钥向量  
25.             des.Key =Encoding.UTF8.GetBytes (strKey );  
26.             des.IV = _key1 ;              
27. new MemoryStream();  
28. new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);  
29.             cs.Write(inputByteArray, 0, inputByteArray.Length);  
30.             cs.FlushFinalBlock();  
31. byte[] cipherBytes = ms .ToArray () ;//得到加密后的字节数组  
32.             cs.Close();  
33.             ms.Close();  
34. return cipherBytes ;              
35.         }  
36.           
37. /// <summary>  
38. /// AES解密  
39. /// </summary>  
40. /// <param name="cipherText">密文字节数组</param>  
41. /// <param name="strKey">密钥</param>  
42. /// <returns>返回解密后的字符串</returns>  
43. public static byte[] AESDecrypt(byte[] cipherText , string strKey )  
44.         {             
45.             SymmetricAlgorithm des = Rijndael .Create () ;                            
46.             des.Key =Encoding.UTF8.GetBytes (strKey );  
47.             des.IV = _key1 ;  
48. byte[] decryptBytes = new byte[cipherText .Length ] ;             
49. new MemoryStream(cipherText ) ;  
50. new CryptoStream(ms, des.CreateDecryptor (), CryptoStreamMode.Read );  
51.             cs.Read (decryptBytes , 0, decryptBytes.Length);              
52.             cs.Close();  
53.             ms.Close();           
54. return decryptBytes ;  
55.         }  
56. }  
57. class Program  
58.     {  
59. static void Main(string[] args)  
60.         {                     
61. string text = "AES加密算法测试数据"; //明文  
62. string keys="dongbinhuiasxiny";//密钥,128位              
63. byte[] encryptBytes = AESEncryption.AESEncrypt (text, keys );   
64. //将加密后的密文转换为Base64编码,以便显示,可以查看下结果  
65. "明文:"+text );  
66. "密文:"+Convert.ToBase64String (encryptBytes ));  
67. //解密  
68. byte[] decryptBytes = AESEncryption.AESDecrypt (encryptBytes, keys);  
69. //将解密后的结果转换为字符串,也可以将该步骤封装在解密算法中  
70. string result = Encoding.UTF8.GetString(decryptBytes);              
71. "解密结果:"+result);   
72.             Console.Read();   
73. }  
74. }

    

下面是运行结果:

明文:AES加密算法测试数据
密文:UNzLn8AbUUGyNkwYAK2+Crgg+HGyOfak6+ou6Gz7zPE=
解密结果:AES加密算法测试数据