using System.Security.Cryptography;
public static void Main()
var key = Encoding.ASCII.GetBytes("0123456789abcdef");
var data = Encrypt("Hello World",key);
var str = Decrypt(data, key);
public static byte[] Encrypt(string plaintext, byte[] key)
using(var desObj = Rijndael.Create())
desObj.Mode = CipherMode.CFB;
desObj.Padding = PaddingMode.PKCS7;
using(var ms = new MemoryStream())
ms.Write(desObj.IV, 0, desObj.IV.Length);
using(CryptoStream cs = new CryptoStream(ms, desObj.CreateEncryptor(), CryptoStreamMode.Write))
var plainTextBytes = Encoding.UTF8.GetBytes(plaintext);
cs.Write(plainTextBytes, 0, plainTextBytes.Length);
public static string Decrypt(byte[] cyphertext, byte[] key)
using(MemoryStream ms = new MemoryStream(cyphertext))
using(var desObj = Rijndael.Create())
desObj.Mode = CipherMode.CFB;
desObj.Padding = PaddingMode.PKCS7;
while(offset < iv.Length)
offset += ms.Read(iv, offset, iv.Length - offset);
using(var cs = new CryptoStream(ms, desObj.CreateDecryptor(), CryptoStreamMode.Read))
using(var sr = new StreamReader(cs, Encoding.UTF8))