AES encryption and decryption (according to the specified secret key)

Time:2021-2-1

Recently, we are calling the third-party interface, which requires AES encryption and the secret key is 16 bits in length. Please record here.

First, reference the namespace:

using System.IO;

using System.Text;

using System.Security.Cryptography;

/// 
         ///AES encryption (vector free)
         /// 
         ///Encrypted plaintext
         ///Key
         ///Ciphertext
         public  string AESEncrypt(string Data, string Key)
         {
             MemoryStream mStream = new MemoryStream();
             RijndaelManaged aes = new RijndaelManaged();
 
             byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
             Byte[] bKey = new Byte[16];
             Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
 
             aes.Mode = CipherMode.ECB;
             aes.Padding = PaddingMode.PKCS7;
             aes.KeySize = 128;
             //aes.Key = _key;
             aes.Key = bKey;
             //aes.IV = _iV;
             CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
             try
             {
                 cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                 cryptoStream.FlushFinalBlock();
                 return Convert.ToBase64String(mStream.ToArray());
             }
             finally
             {
                 cryptoStream.Close();
                 mStream.Close();
                 aes.Clear();
             }
         }
#Region decryption
         /// 
         ///AES decryption (vector free)
         /// 
         ///Encrypted plaintext
         ///Key
         ///Plaintext
         public  string AESDecrypt(String Data, String Key)
         {
             Byte[] encryptedBytes = Convert.FromBase64String(Data);
             Byte[] bKey = new Byte[16];
             Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
 
             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 = bKey;
             //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 Encoding.UTF8.GetString(ret);
             }
             finally
             {
                 cryptoStream.Close();
                 mStream.Close();
                 aes.Clear();
             }
         }
         #When calling endregion: string SendData= publicSH.AESDecrypt (SendData, "hyysiendeufhvkda"); // returns the decryption information. SendData is the information to be encrypted or decrypted. The second parameter is the 16 bit secret key

 

 

Recommended Today

Re learn JavaScript of front end (3) scope, prototype chain, garbage collection mechanism, closure, event execution sequence

This article mainly introduces the scope, prototype chain, garbage collection mechanism, closure and event execution sequence in JavaScript Execution environment and scope 1execution environment The definition of  execution environment : The execution environment defines other data that a variable or function has access to , It determines their respective behaviors. Each execution environment has a variable […]