Details

AES Encryptor

Encrypt sensitive plaintext data using AES

public static class EncryptionHelper
{
   public static string Encrypt(string plainText, string key, string iv)
   {
       using var aes = Aes.Create();
       aes.Key = Convert.FromBase64String(key);
       aes.IV = Convert.FromBase64String(iv);
       using var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
       using var ms = new MemoryStream();
       using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
       {
           using var writer = new StreamWriter(cs);
           writer.Write(plainText);
       }
       return Convert.ToBase64String(ms.ToArray());
   }
   public static string Decrypt(string encryptedText, string key, string iv)
   {
       using var aes = Aes.Create();
       aes.Key = Convert.FromBase64String(key);
       aes.IV = Convert.FromBase64String(iv);
       using var decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
       using var ms = new MemoryStream(Convert.FromBase64String(encryptedText));
       using var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
       using var reader = new StreamReader(cs);
       return reader.ReadToEnd();
   }
}

Store the initialization vector in an environment variable or appsettings.json.