using System.Security.Cryptography;
public class CryptoAESService
private readonly string _key;
private readonly string _iv;
public CryptoAESService(string key, string iv)
public string Encrypt(string plaintext)
using (Aes aesAlg = Aes.Create())
aesAlg.Key = Encoding.UTF8.GetBytes(_key);
aesAlg.IV = Encoding.UTF8.GetBytes(_iv);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt, Encoding.UTF8))
swEncrypt.Write(plaintext);
encrypted = msEncrypt.ToArray();
return Convert.ToBase64String(encrypted);
public string Decrypt(string base64CipherText)
byte[] cipherText = Convert.FromBase64String(base64CipherText);
using (Aes aesAlg = Aes.Create())
aesAlg.Key = Encoding.UTF8.GetBytes(_key);
aesAlg.IV = Encoding.UTF8.GetBytes(_iv);
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt, Encoding.UTF8))
plainText = srDecrypt.ReadToEnd();
public static void Main()
Console.WriteLine("Hello World");
var crypto = new CryptoAESService("B0235DE99D3E4B52A732F85F43DD5ADB","d)j&1KfM0;sjKmJs");
var encrypted = crypto.Encrypt("cabbplus::::::");
Console.WriteLine("data:"+encrypted);