using System.Security.Cryptography;
public static void Main()
var serializedRequest = @"{""encryptedData"":""UXGtgNgbJmQDr3Jd0wrSSwk95X8DBfeKN/W7xy3sz1YDDPBhJw2AXIfG5SdCFmEAHvaTk0SaIPFi2deJTgawtUTyW6chIApxhCYNCVIx7XU="",""iv"":""rrwHcs5cS8z9cElBzZRSnw=="",""hash"":""DijdTPCTwrrDKvO8Tiy7QMeYbc69Tz63M4neMNRUVto=""}";
var request = JsonConvert.DeserializeObject<EncryptedMessage>(serializedRequest);
var encryptedData = request.EncryptedData.FromBase64();
var iV = request.IV.FromBase64();
var hash = request.Hash.FromBase64();
var key = secret.ToUTF8().ToSHA256();
var decryptedData = encryptedData.DecryptAES(key, iV);
var decryptedHash = decryptedData.ToSHA256();
var hashMatches = decryptedHash.SequenceEqual(hash);
if(hashMatches is false) {
Console.WriteLine("Secret is invalid");
var serializedData = decryptedData.FromUTF8();
Console.WriteLine(serializedData);
public record EncryptedMessage(string EncryptedData, string IV, string Hash);
public static class ExtensionMethods
public static byte[] FromBase64(this string value) {
return Convert.FromBase64String(value);
public static string FromUTF8(this byte[] value) {
return Encoding.UTF8.GetString(value);
public static byte[] ToUTF8(this string value)
return Encoding.UTF8.GetBytes(value);
public static byte[] ToSHA256(this byte[] value)
using var sha256 = SHA256.Create();
return sha256.ComputeHash(value);
public static byte[] DecryptAES(this byte[] value, byte[] key, byte[] iv) {
using var aes = Aes.Create();
using var cryptoTransform = aes.CreateDecryptor(key, iv);
using var memoryStream = new MemoryStream();
using var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write);
cryptoStream.Write(value);
cryptoStream.FlushFinalBlock();
return memoryStream.ToArray();