AES加密解密C#代码

大家讲道理
发布: 2016-11-10 09:27:52
原创
2853 人浏览过

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Security.Cryptography;  
using System.IO;  
   
   
namespace AESDemo  
{  
    public static class AESHelper  
    {  
        /// <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;  
        }  
    }  
}  
   
   
不使用向量的方式:  
public static class AESCrypto  
    {  
/// <summary>  
/// IV向量为固定值  
/// </summary>  
        //private static byte[] _iV = {  
        //    85, 60, 12, 116,  
        //    99, 189, 173, 19,  
        //    138, 183, 232, 248,  
        //    82, 232, 200, 242  
        //};  
   
   
        public static byte[] Decrypt(byte[] encryptedBytes, byte[] key)  
        {  
MemoryStream mStream = new MemoryStream( encryptedBytes );  
//mStream.Write( encryptedBytes, 0, encryptedBytes.Length );  
//mStream.Seek( 0, SeekOrigin.Begin );  
RijndaelManaged aes = new RijndaelManaged( );  
            aes.Mode = CipherMode.ECB;  
            aes.Padding = PaddingMode.PKCS7;  
            aes.KeySize = 128;  
aes.Key = key;  
//aes.IV = _iV;  
CryptoStream cryptoStream = new CryptoStream( mStream, aes.CreateDecryptor( ), CryptoStreamMode.Read );  
try {  
   
   
byte[] tmp = new byte[ encryptedBytes.Length + 32 ];  
int len = cryptoStream.Read( tmp, 0, encryptedBytes.Length + 32 );  
byte[] ret = new byte[ len ];  
Array.Copy( tmp, 0, ret, 0, len );  
return ret;  
}  
finally {  
cryptoStream.Close( );  
mStream.Close( );  
aes.Clear( );  
}  
}  
   
   
        public static byte[] Encrypt(byte[] plainBytes, byte[] key)  
        {  
            MemoryStream mStream = new MemoryStream();  
            RijndaelManaged aes = new RijndaelManaged();  
   
   
            aes.Mode = CipherMode.ECB;  
            aes.Padding = PaddingMode.PKCS7;  
            aes.KeySize = 128;  
            //aes.Key = _key;  
   
   
            aes.Key = key;  
            //aes.IV = _iV;  
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);  
            try 
            {  
                cryptoStream.Write(plainBytes, 0, plainBytes.Length);  
                cryptoStream.FlushFinalBlock();  
                return mStream.ToArray();  
            }  
            finally 
            {  
                cryptoStream.Close();  
                mStream.Close();  
                aes.Clear();  
            }  
        }  
    }
登录后复制

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板