using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
public static void Main(string[] args)
string encoded_key= "ZneN1NKt22Q/NNZBA8se6dxmATjRKouWq6ecpRsL/50=";
string encoded_iv= "edHnM2KFhIqrPBk0";
string Text = "{\"entity_id\":\"100\",\"member_id\":\"2165464\",\"email\":\"arun.p@neubergdiagnostics.com\",\"first_name\":\"Arun\",\"last_name\":\"P\",\"gender\":\"M\",\"mobile\":\"9600960461\",\"dob\":\"19-07-1993\",\"requested_at\":\"1686294340\"}";
byte[] encryptedBytes = encrypt(Text, Convert.FromBase64String(encoded_key), Convert.FromBase64String(encoded_iv));
string encodedText = UrlBase64.Encode(encryptedBytes, PaddingPolicy.Preserve);
Console.WriteLine ("\nEncrypted combined payload:\n" + encodedText);
byte[] encryptedMessage = new byte[encryptedBytes.Length - 16];
byte[] encryptedAuthTag = new byte[16];
Array.Copy(encryptedBytes, 0, encryptedMessage, 0, encryptedMessage.Length);
Array.Copy(encryptedBytes, encryptedMessage.Length, encryptedAuthTag, 0, encryptedAuthTag.Length);
string encodedMessage = UrlBase64.Encode(encryptedMessage, PaddingPolicy.Preserve);
string encodedAuthTag = UrlBase64.Encode(encryptedAuthTag, PaddingPolicy.Preserve);
Console.WriteLine ("\nEncrypted message:\n" + encodedMessage);
Console.WriteLine ("\nAuthentication tag:\n" + encodedAuthTag);
byte[] combinedBytes = new byte[encryptedMessage.Length + encryptedAuthTag.Length];
System.Buffer.BlockCopy(encryptedMessage, 0, combinedBytes, 0, encryptedMessage.Length);
System.Buffer.BlockCopy(encryptedAuthTag, 0, combinedBytes, encryptedMessage.Length, encryptedAuthTag.Length);
string res = decrypt(UrlBase64.Encode(combinedBytes, PaddingPolicy.Preserve), Convert.FromBase64String(encoded_key), Convert.FromBase64String(encoded_iv));
Console.WriteLine ("\nDecrypted message:\n" + res);
res = decrypt(encodedText, Convert.FromBase64String(encoded_key), Convert.FromBase64String(encoded_iv));
Console.WriteLine ("\nDecrypted combined payload:\n" + res);
public static byte[] encrypt(string PlainText, byte[] key, byte[] iv)
byte[] encryptedBytes = null;
byte[] plainBytes = Encoding.UTF8.GetBytes(PlainText);
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
AeadParameters parameters =
new AeadParameters(new KeyParameter(key), 128, iv, null);
cipher.Init(true, parameters);
encryptedBytes = new byte[cipher.GetOutputSize(plainBytes.Length)];
int retLen = cipher.ProcessBytes
(plainBytes, 0, plainBytes.Length, encryptedBytes, 0);
cipher.DoFinal(encryptedBytes, retLen);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
public static string decrypt(string encodedText, byte[] key, byte[] iv)
string sR = string.Empty;
byte[] encryptedBytes = UrlBase64.Decode(encodedText);
GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
AeadParameters parameters =
new AeadParameters(new KeyParameter(key), 128, iv, null);
cipher.Init(false, parameters);
byte[] plainBytes = new byte[cipher.GetOutputSize(encryptedBytes.Length)];
Int32 retLen = cipher.ProcessBytes
(encryptedBytes, 0, encryptedBytes.Length, plainBytes, 0);
cipher.DoFinal(plainBytes, retLen);
sR = Encoding.UTF8.GetString(plainBytes);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);