using System.Security.Cryptography;
public static void Main() {
Console.WriteLine("AES GCM 256 String decryption");
Console.WriteLine("\n* * * Decryption * * *");
string password = "A random password to encrypt";
SHA256 mySHA256 = SHA256.Create();
byte[] Key = mySHA256.ComputeHash(Encoding.UTF8.GetBytes(password));
string soCiphertextBase64 = "aV+gDmSBbi9PjOT9FD8LcuISbEQ5F3q0X8qzf3MKiDzxo12WQVirsnltbApLMMG9JScVfTXx7PJw7EVFoKz8JLMYLMu/JsRGcfvihSK+d/yeRTBEuJHL74Hv2Zr7b4CoMJhEUmYF3KT2Onlj4lI5ChOjmgXvpSev/xc=";
Console.WriteLine("ciphertext (Base64): " + soCiphertextBase64);
string soDecryptedtext = soAesGcmDecryptFromBase64(Key, soCiphertextBase64);
Console.WriteLine("plaintext: " + soDecryptedtext);
static string soAesGcmDecryptFromBase64(byte[] key, string data) {
const int MAC_BIT_SIZE = 128;
const int NONCE_BIT_SIZE = 96;
byte[] EncryptedData = Convert.FromBase64String(data);
using (MemoryStream MStream = new MemoryStream(EncryptedData))
using (BinaryReader Binary = new BinaryReader(MStream)) {
IV = Binary.ReadBytes(NONCE_BIT_SIZE / 8);
CipherText = Binary.ReadBytes(EncryptedData.Length - IV.Length - (MAC_BIT_SIZE / 8));
Tag = Binary.ReadBytes((MAC_BIT_SIZE / 8));
byte[] associatedData = new byte[0];
byte[] decryptedData = new byte[CipherText.Length];
using(var cipher = new AesGcm(key)) {
cipher.Decrypt(IV, CipherText, Tag, decryptedData, associatedData);
decryptedtext = Encoding.UTF8.GetString(decryptedData, 0, decryptedData.Length);