AES加密解密C#代碼

來源:互聯網
上載者:User
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      {          ///           /// AES加密          ///           /// 被加密的明文          /// 密鑰          /// 向量          /// 密文          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;          }                ///           /// AES解密          ///           /// 被解密的密文          /// 密鑰          /// 向量          /// 明文          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      {  ///   /// IV向量為固定值  ///           //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();              }          }      }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.