AES encryption and decryption [C# implements AES encryption and decryption]

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Encryption
{
    public class Encryption
    {
        public string Encrypt(String Plaintext, string key, String IV)
        {
            return EncrypText(GetPlainArray(Plaintext), GetKeyArray(key), GetIvByte(IV));
        }

        public string Decrypt(string Ciphertext, string key, String IV)
        {
            return Decryptext(GetCipherArray(Ciphertext), GetKeyArray(key), GetIvByte(IV));
        }


        //Iv is converted from string type to byte[] type
        //The initialization vector is undoubtedly similar to the mask value used in the password encryption process.
        private static Byte[] GetIvByte(String iv)
        {
            // 16*8 = 128
            byte[] IVArray = new byte[16];
            //It is recommended not to use default
            Array.Copy(Encoding.UTF32.GetBytes(iv.PadRight(IVArray.Length, 'a')), IVArray, IVArray.Length);
            return IVArray;
        }

        //K is converted from string type to byte[] type
        private static Byte[] GetKeyArray(String key)
        {
            //24*8 = 192  16*8 = 128  32*8 = 256
            byte[] KeyArray = new byte[32];
            Array.Copy(Encoding.UTF32.GetBytes(key.PadRight(KeyArray.Length, 'a')), KeyArray, KeyArray.Length);
            return KeyArray;
        }

        / / Convert the content to be encrypted into byte type data
        private static byte[] GetPlainArray(String Plaintext)
        {
            Byte[] TextArray = Encoding.UTF32.GetBytes(Plaintext);
            return TextArray;
        }
        //Convert the content to be decrypted into byte type data
        private static byte[] GetCipherArray(String Ciphertext)
        {
            Byte[] Cipher = Convert.FromBase64String(Ciphertext);
            return Cipher;
        }

        //encryption
        private static string EncrypText(byte[] Plaintext, byte[] key, byte[] IV)
        {
            try
            {
                // RijndaelManaged rijndael = new RijndaelManaged();
                Aes aes = new AesManaged();
                //Get or set the key of the symmetric algorithm
                aes.Key = key;
                //Get or set the initialization vector of the symmetric algorithm
                aes.IV = IV;
                aes.BlockSize = 128; //Operation mode
                aes.Mode = CipherMode.ECB;
                //Block Supplement: Amount to pad = 128 - (data Plaintext length mod block length)
                aes.Padding = PaddingMode.ANSIX923;
                // Create a symmetric encryptor object with the current Key property and initialization vector (IV).
                // iCryptoTransform defines the basic operations for cryptographic transformations.
                ICryptoTransform iCryptoTransform = aes.CreateEncryptor(key, IV);
                //Convert the specified area of ​​the specified byte array.
                byte[] resultArray = iCryptoTransform.TransformFinalBlock(Plaintext, 0, Plaintext.Length);
                return Convert.ToBase64String (resultArray);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        // decrypt
        private static string Decryptext(byte[] Ciphertext, byte[] key, byte[] IV)
        {
            try
            {
                Aes aes = new AesManaged();
                aes.Key = key;
                aes.IV = IV;
                aes.BlockSize = 128;
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.ANSIX923;
                ICryptoTransform cTransform = aes.CreateDecryptor(key, IV);
                byte[] resultArray = cTransform.TransformFinalBlock(Ciphertext, 0, Ciphertext.Length);
                return Encoding.UTF32.GetString(resultArray);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325592746&siteId=291194637