using System.Collections.Generic;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Paddings;
using System.Security.Cryptography;
static byte[] IV = Encoding.UTF32.GetBytes("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
static void Main(string[] args)
var encoding = new UTF32Encoding();
IBlockCipherPadding _padding;
byte[] key = StringToByteArray("4C84C1898FC592F192C8059215F6AB8068D480CC3B8579438532947703067A731781F75B450412B910076C20CE0670274705031F245EC56B14301F54338ED7BFE52A0692DF014A5E01660D3E30D33DB03D41F836354F5A5AB23BDA5F92585314");
byte[] ciphertxt = encoding.GetBytes("0000000000000000000000000000000000000000000000000000000000000000");
DecryptAnother(ciphertxt, key);
public static byte[] kdf(byte[] z, byte[] otherInfo)
IDigest digest = new Sha256Digest();
byte[] result = new byte[digest.GetDigestSize()];
digest.Update((byte)(1 >> 24));
digest.Update((byte)(1 >> 16));
digest.Update((byte)(1 >> 8));
digest.BlockUpdate(z, 0, z.Length);
digest.BlockUpdate(otherInfo, 0, otherInfo.Length);
digest.DoFinal(result, 0);
public static void DecryptAnother(byte[] ciphertxt, byte[] key)
byte[] aesKeyData = kdf(key, ciphertxt);
var cipher = new GcmBlockCipher(new AesFastEngine());
KeyParameter keyParameter = ParameterUtilities.CreateKeyParameter("AES", aesKeyData);
ICipherParameters cipherParameters = new ParametersWithIV(keyParameter, IV);
cipher.Init(false, cipherParameters);
byte[] output = new byte[cipher.GetOutputSize(ciphertxt.Length)];
int len = cipher.ProcessBytes(ciphertxt, 0, ciphertxt.Length, output, 0);
Console.WriteLine(cipher.DoFinal(output, len));
Console.WriteLine(ex.Message);
private static byte[] StringToByteArray(string HexString)
int NumberChars = HexString.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(HexString.Substring(i, 2), 16);