C#实现AES加解密

http://blog.sina.com.cn/s/blog_682c51460101d3hv.html

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位的。代码如下:
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(); 
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值