using System.Security.Cryptography;
public static void Main()
string message = "My secret message 1234";
string password = "3d756227f8be6f85";
string ivstr = "ba7e0bb79b61f82d";
string encrypted = p.EncryptString(message, password, ivstr);
string decrypted = p.DecryptString(encrypted, password, ivstr);
Console.WriteLine(encrypted);
Console.WriteLine(decrypted);
public string EncryptString(string plainText, string password,string ivstr)
byte[] key = Encoding.UTF8.GetBytes(password);
byte[] iv = Encoding.UTF8.GetBytes(ivstr);
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.CBC;
byte[] aesKey = new byte[16];
Array.Copy(key, 0, aesKey, 0, 16);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform aesEncryptor = encryptor.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesEncryptor, CryptoStreamMode.Write);
byte[] plainBytes = Encoding.ASCII.GetBytes(plainText);
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherBytes = memoryStream.ToArray();
string cipherText = Convert.ToBase64String(cipherBytes, 0, cipherBytes.Length);
public string DecryptString(string cipherText, string password,string ivstr)
byte[] key = Encoding.UTF8.GetBytes(password);
byte[] iv = Encoding.UTF8.GetBytes(ivstr);
Aes encryptor = Aes.Create();
encryptor.Mode = CipherMode.CBC;
byte[] aesKey = new byte[16];
Array.Copy(key, 0, aesKey, 0, 16);
MemoryStream memoryStream = new MemoryStream();
ICryptoTransform aesDecryptor = encryptor.CreateDecryptor();
CryptoStream cryptoStream = new CryptoStream(memoryStream, aesDecryptor, CryptoStreamMode.Write);
string plainText = String.Empty;
byte[] cipherBytes = Convert.FromBase64String(cipherText);
cryptoStream.Write(cipherBytes, 0, cipherBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] plainBytes = memoryStream.ToArray();
plainText = Encoding.ASCII.GetString(plainBytes, 0, plainBytes.Length);