// 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; namespace E { public class DESUtil { public static string Crypt(string plain) { Encoding utf = new UTF8Encoding(); DES des = new DESCryptoServiceProvider(); byte[] key = utf.GetBytes("hemmelig"); byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; ICryptoTransform encrypt = des.CreateEncryptor(key, iv); byte[] cipher = encrypt.TransformFinalBlock(utf.GetBytes(plain), 0, utf.GetByteCount(plain)); return Convert.ToBase64String(cipher); } public static string Decrypt(string plain2) { Encoding utf = new UTF8Encoding(); DES des = new DESCryptoServiceProvider(); byte[] key = utf.GetBytes("hemmelig"); byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; ICryptoTransform decrypt = des.CreateDecryptor(key, iv); byte[] cipher2 = Convert.FromBase64String(plain2); return utf.GetString(decrypt.TransformFinalBlock(cipher2, 0, cipher2.Length)); } } class MainClass { public static void Main(string[] args) { string s1 = "Dette er en lille test"; string s2 = DESUtil.Crypt(s1); string s3 = DESUtil.Decrypt(s2); Console.WriteLine(s1 + "->" + s2 + "->" + s3); } } }