// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Text; using System.Security.Cryptography; public class MainClass { private static Encoding utf = new UTF8Encoding(); private static Rijndael aes = new RijndaelManaged(); private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; public static string Encrypt(string plain, string key) { ICryptoTransform encrypt = aes.CreateEncryptor(utf.GetBytes(key), iv); return Convert.ToBase64String(encrypt.TransformFinalBlock(utf.GetBytes(plain), 0, utf.GetByteCount(plain))); } public static string Decrypt(string cipher, string key) { ICryptoTransform decrypt = aes.CreateDecryptor(utf.GetBytes(key), iv); byte[] b = Convert.FromBase64String(cipher); return utf.GetString(decrypt.TransformFinalBlock(b, 0, b.Length)); } public static void Main(string[] args) { Console.WriteLine(Encrypt("Dette er en test !", "hemmeligabcdefgh12345678")); Console.WriteLine(Decrypt(Encrypt("Dette er en test !", "hemmeligabcdefgh12345678"), "hemmeligabcdefgh12345678")); } }