程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#代碼完成對AES加密解密

C#代碼完成對AES加密解密

編輯:C#入門知識

C#代碼完成對AES加密解密。本站提示廣大學習愛好者:(C#代碼完成對AES加密解密)文章只能為提供參考,不一定能成為您想要的結果。以下是C#代碼完成對AES加密解密正文


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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved