// Collection of code snippets by Arne Vajhøj // posted to eksperten.dk, usenet and other places (2002-now) using System; using System.Text; using System.Security.Cryptography; public class MainClass { private static Encoding utf = new UTF8Encoding(); private static DES des = new DESCryptoServiceProvider(); private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; public static string Encrypt(string plain, string key) { ICryptoTransform encrypt = des.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 = des.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 !", "hemmelig")); Console.WriteLine(Decrypt(Encrypt("Dette er en test !", "hemmelig"), "hemmelig")); } }