context: using Sio.Cms.Lib.ViewModels;
  using Newtonsoft.Json.Linq;
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Security.Cryptography;
  using System.Text;
  
  namespace Sio.Cms.Lib.Helpers
  {
      public class AesEncryptionHelper
      {
          public static CryptoViewModel<string> EncryptStringToBytes_Aes(JObject plainText)
          {
              var result = new CryptoViewModel<string>();
              //var Key = Convert.FromBase64String(keyString);
              //var IV = Convert.FromBase64String(iv);
              // Check arguments.
              if (plainText == null)
                  throw new ArgumentNullException("plainText");
              //if (Key == null || Key.Length <= 0)
              //    throw new ArgumentNullException("Key");
              //if (IV == null || IV.Length <= 0)
              //    throw new ArgumentNullException("IV");
              byte[] encrypted;
  
              // Create an Aes object
              // with the specified key and IV.
              using (AesManaged aesAlg = new AesManaged())
              {
                  aesAlg.Mode = CipherMode.ECB;
                  aesAlg.Padding = PaddingMode.PKCS7;
                  //aesAlg.Key = Key;
                  //aesAlg.IV = IV;
  
                  result.Base64Key = Convert.ToBase64String(aesAlg.Key);
                  result.Base64IV = Convert.ToBase64String(aesAlg.IV);
  
                  // Create an encryptor to perform the stream transform.
                  ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  
                  // Create the streams used for encryption.
                  using (MemoryStream msEncrypt = new MemoryStream())
                  {
                      using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                      {
                          using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                          {
                              //Write all data to the stream.
                              swEncrypt.Write(plainText);
                          }
                          encrypted = msEncrypt.ToArray();
                      }
                  }
              }
              // Return the encrypted bytes from the memory stream.
              result.Data = Convert.ToBase64String(encrypted);
              return result;
          }
  
          public static CryptoViewModel<JObject> DecryptStringFromBytes_Aes(string cipherText, string keyString, string iv)
          {
              CryptoViewModel<JObject> result = new CryptoViewModel<JObject>()
              {
                  Base64Key = keyString,
                  Base64IV = iv
              };
              var Key = Convert.FromBase64String(keyString);
              var IV = Convert.FromBase64String(iv);
              // Check arguments.
              if (cipherText == null || cipherText.Length <= 0)
                  throw new ArgumentNullException("cipherText");
              if (Key == null || Key.Length <= 0)
                  throw new ArgumentNullException("Key");
              if (IV == null || IV.Length <= 0)
                  throw new ArgumentNullException("IV");
  
              // Declare the string used to hold
              // the decrypted text.
              string plaintext = null;
              var fullCipher = Convert.FromBase64String(cipherText);
              // Create an Aes object
              // with the specified key and IV.
              using (AesManaged aesAlg = new AesManaged())
              {
                  aesAlg.Mode = CipherMode.ECB;
                  aesAlg.Padding = PaddingMode.PKCS7;
                  aesAlg.Key = Key;
                  aesAlg.IV = IV;
  
                  // Create a decryptor to perform the stream transform.
                  ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  
                  // Create the streams used for decryption.
                  using (MemoryStream msDecrypt = new MemoryStream(fullCipher))
                  {
                      using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                      {
                          using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                          {
                              // Read the decrypted bytes from the decrypting stream
                              // and place them in a string.
                              plaintext = srDecrypt.ReadToEnd();
                          }
                      }
                  }
  
              }
              result.Data = JObject.Parse(plaintext);
              return result;
  
          }
  
          public static string EncryptString(string text, string keyString, string strIV)
          {
              var key = Encoding.UTF8.GetBytes(keyString);
  
              using (var aesAlg = Aes.Create())
              {
                  using (var encryptor = aesAlg.CreateEncryptor(key, aesAlg.IV))
                  {
                      using (var msEncrypt = new MemoryStream())
                      {
                          using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                          using (var swEncrypt = new StreamWriter(csEncrypt))
                          {
                              swEncrypt.Write(text);
                          }
  
                          var iv = aesAlg.IV;
  
                          var decryptedContent = msEncrypt.ToArray();
  
                          var result = new byte[iv.Length + decryptedContent.Length];
  
                          Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
                          Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);
  
                          return Convert.ToBase64String(result);
                      }
                  }
              }
          }
  
          public static string DecryptString(string cipherText, string keyString, string strIV)
          {
              var fullCipher = Convert.FromBase64String(cipherText);
  
              var iv = new byte[16];
              var cipher = new byte[16];
  
              Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
              Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
              var key = Encoding.UTF8.GetBytes(keyString);
  
              using (var aesAlg = Aes.Create())
              {
                  using (var decryptor = aesAlg.CreateDecryptor(key, iv))
                  {
                      string result;
                      using (var msDecrypt = new MemoryStream(cipher))
                      {
                          using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                          {
                              using (var srDecrypt = new StreamReader(csDecrypt))
                              {
                                  result = srDecrypt.ReadToEnd();
                              }
                          }
                      }
  
                      return result;
                  }
              }
          }
          public static string EncryptString(string text, string keyString)
          {
              var key = Encoding.UTF8.GetBytes(keyString);
  
              using (var aesAlg = Aes.Create())
              {
                  using (var encryptor = aesAlg.CreateEncryptor(key, aesAlg.IV))
                  {
                      using (var msEncrypt = new MemoryStream())
                      {
                          using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                          using (var swEncrypt = new StreamWriter(csEncrypt))
                          {
                              swEncrypt.Write(text);
                          }
  
                          var iv = aesAlg.IV;
  
                          var decryptedContent = msEncrypt.ToArray();
  
                          var result = new byte[iv.Length + decryptedContent.Length];
  
                          Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
                          Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);
  
                          return Convert.ToBase64String(result);
                      }
                  }
              }
          }
  
          public static string DecryptString(string cipherText, string keyString)
          {
              var fullCipher = Convert.FromBase64String(cipherText);
  
              var iv = new byte[16];
              var cipher = new byte[16];
  
              Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
              Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
  
gt:             var key = Encoding.UTF8.GetBytes(keyString);