using System.Security.Cryptography;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
public static void Main()
byte[] plaintext = Encoding.UTF8.GetBytes("{\"business_id\":\"3292aecc-886a-45e3-aa55-d3");
byte[] iv = Convert.FromHexString("76396c566a476c4c50694e376469487a");
byte[] key = Encoding.UTF8.GetBytes("ei9dzmx9f3l2CdFliYMb7iwJ7d0ce58d");
byte[] ciphertext_streams = AesEncrypt_streams(key, iv, plaintext);
Console.WriteLine(Convert.ToBase64String(ciphertext_streams));
byte[] decrypted_streams = AesDecrypt_streams(key, iv, ciphertext_streams);
Console.WriteLine(Encoding.UTF8.GetString(decrypted_streams));
byte[] ciphertext_BC = AesEncrypt_BC(key, iv, plaintext);
Console.WriteLine(Convert.ToBase64String(ciphertext_BC));
byte[] decrypted_BC = AesDecrypt_BC(key, iv, ciphertext_BC);
Console.WriteLine(Encoding.UTF8.GetString(decrypted_BC));
byte[] ciphertext = AesEncrypt(key, iv, plaintext);
Console.WriteLine(Convert.ToBase64String(ciphertext));
byte[] decrypted = AesDecrypt(key, iv, ciphertext);
Console.WriteLine(Encoding.UTF8.GetString(decrypted));
private static byte[] AesEncrypt(byte[] key, byte[] iv, byte[] plaintext)
byte[] ciphertext = null;
using (Aes aesAlg = Aes.Create())
ciphertext = aesAlg.EncryptCfb(plaintext, iv, PaddingMode.None, 8);
private static byte[] AesDecrypt(byte[] key, byte[] iv, byte[] ciphertext)
using (Aes aesAlg = Aes.Create())
decrypted = aesAlg.DecryptCfb(ciphertext, iv, PaddingMode.None, 8);
private static byte[] AesEncrypt_BC(byte[] key, byte[] iv, byte[] plaintext)
var cipher = CipherUtilities.GetCipher("AES/CFB8");
cipher.Init(true, new ParametersWithIV(new KeyParameter(key), iv));
return cipher.DoFinal(plaintext);
private static byte[] AesDecrypt_BC(byte[] key, byte[] iv, byte[] ciphertext)
var cipher = CipherUtilities.GetCipher("AES/CFB8");
cipher.Init(false, new ParametersWithIV(new KeyParameter(key), iv));
return cipher.DoFinal(ciphertext);
private static byte[] AesEncrypt_streams(byte[] key, byte[] iv, byte[] plaintext)
byte[] ciphertext = null;
using (Aes aesAlg = Aes.Create())
aesAlg.Padding = PaddingMode.None;
aesAlg.Mode = CipherMode.CFB;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
csEncrypt.Write(plaintext);
ciphertext = msEncrypt.ToArray();
private static byte[] AesDecrypt_streams(byte[] key, byte[] iv, byte[] ciphertext)
using (Aes aesAlg = Aes.Create())
aesAlg.Padding = PaddingMode.None;
aesAlg.Mode = CipherMode.CFB;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(ciphertext))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (MemoryStream msDecryptForCpy = new MemoryStream())
csDecrypt.CopyTo(msDecryptForCpy);
decrypted = msDecryptForCpy.ToArray();