国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#代碼實現對AES加密解密

2020-01-24 01:20:13
字體:
來源:轉載
供稿:網友

ES(The Advanced Encryption Standard)是美國國家標準與技術研究所用于加密電子數據的規范。它被預期能成為人們公認的加密包括金融、電信和政府數字信息的方法。

本文實例為大家介紹C#實現對AES加密解密的詳細代碼,分享給大家供大家參考,具體內容如下

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();    }   }  }

希望通過這篇文章大家對AES加密解密有所了解,對C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 瑞昌市| 区。| 榆树市| 临沧市| 甘德县| 兴义市| 鄂温| 永城市| 洪泽县| 海南省| 六枝特区| 东丰县| 乐都县| 洪雅县| 花莲市| 七台河市| 克东县| 卫辉市| 芜湖市| 广汉市| 天全县| 高雄市| 泰宁县| 广宁县| 宜州市| 山阴县| 三门峡市| 梅州市| 菏泽市| 湘阴县| 鹿邑县| 容城县| 黄山市| 云龙县| 静乐县| 大姚县| 青铜峡市| 绥棱县| 石景山区| 遂川县| 来宾市|