1. Home
  2. Virtual Card Management
  3. C# AES Example

C# AES Example

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Security.Cryptography;



    public class Aes
    {
        public static string ivStr = "uDMl7JqkHZQDZ+nK";
        public static string keyStr = "0qxH9vaizwIeLju+fmGIVfAwgG79XT7K";

        public static string Encrypt(string PlainText)
        {
            RijndaelManaged aes = new RijndaelManaged();
            aes.BlockSize = 128;
            aes.KeySize = 256;

            // It is equal in java 
            /// Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");    
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            byte[] keyArr = ASCIIEncoding.UTF8.GetBytes(keyStr);
            byte[] KeyArrBytes32Value = new byte[
                32
            ];
            Array.Copy(keyArr, KeyArrBytes32Value,
            32);

            // Initialization vector.   
            // It could be any value or generated using a random number generator.
            byte[] ivArr = ASCIIEncoding.UTF8.GetBytes(ivStr);
            byte[] IVBytes16Value = new byte[
                16
            ];
            Array.Copy(ivArr, IVBytes16Value,
            16);

            aes.Key = KeyArrBytes32Value;
            aes.IV = IVBytes16Value;

            ICryptoTransform encrypto = aes.CreateEncryptor();

            byte[] plainTextByte = ASCIIEncoding.UTF8.GetBytes(PlainText);
            byte[] CipherText = encrypto.TransformFinalBlock(plainTextByte,
            0, plainTextByte.Length);
            return Convert.ToBase64String(CipherText);
        }

        public static string Decrypt(string CipherText)
        {
            RijndaelManaged aes = new RijndaelManaged();
            aes.BlockSize = 128;
            aes.KeySize = 256;

            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            byte[] keyArr = ASCIIEncoding.UTF8.GetBytes(keyStr);
            byte[] KeyArrBytes32Value = new byte[
                32
            ];
            Array.Copy(keyArr, KeyArrBytes32Value,
            32);

            // Initialization vector.   
            // It could be any value or generated using a random number generator.
            byte[] ivArr = ASCIIEncoding.UTF8.GetBytes(ivStr);
            byte[] IVBytes16Value = new byte[
                16
            ];
            Array.Copy(ivArr, IVBytes16Value,
            16);

            aes.Key = KeyArrBytes32Value;
            aes.IV = IVBytes16Value;

            ICryptoTransform decrypto = aes.CreateDecryptor();

            byte[] encryptedBytes = Convert.FromBase64CharArray(CipherText.ToCharArray(),
            0, CipherText.Length);
            byte[] decryptedData = decrypto.TransformFinalBlock(encryptedBytes,
            0, encryptedBytes.Length);
            return ASCIIEncoding.UTF8.GetString(decryptedData);
        }
    

Was this article helpful to you? Yes No

How can we help?