Dette er en enkel klasse for kryptering og dekryptering av data. Dette er nok ikke den aller sikreste måten å kryptere på, men den holder ganske langt.
using System; using System.Text; using System.Security.Cryptography; public class Encrypt { //Dette er en kode som brukes til krypteringen og //dekrypteringen. Denne setter du selv.. public static string Key = "HemmeligKode"; public static byte[] EncryptBytes(byte[] InputString, string EncryptionKey) { if (EncryptionKey == null) EncryptionKey = Key; TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); des.Key = hashmd5.ComputeHash(Encoding.ASCII.GetBytes(EncryptionKey)); des.Mode = CipherMode.ECB; ICryptoTransform Transform = des.CreateEncryptor(); byte[] Buffer = InputString; return Transform.TransformFinalBlock(Buffer,0,Buffer.Length); } public static byte[] EncryptBytes(string DecryptString, string EncryptionKey) { return EncryptBytes(Encoding.ASCII.GetBytes(DecryptString),EncryptionKey); } public static string EncryptString(string InputString, string EncryptionKey) { return Convert.ToBase64String(EncryptBytes(Encoding.ASCII.GetBytes(InputString),EncryptionKey)); } public static byte[] DecryptBytes(byte[] DecryptBuffer, string EncryptionKey) { if (EncryptionKey == null) EncryptionKey = Key; TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); des.Key = hashmd5.ComputeHash(Encoding.ASCII.GetBytes(EncryptionKey)); des.Mode = CipherMode.ECB; ICryptoTransform Transform = des.CreateDecryptor(); return Transform.TransformFinalBlock(DecryptBuffer,0,DecryptBuffer.Length); } public static byte[] DecryptBytes(string DecryptString, string EncryptionKey) { return DecryptBytes(Convert.FromBase64String(DecryptString),EncryptionKey); } public static string DecryptString(string DecryptString, string EncryptionKey) { try { return Encoding.ASCII.GetString(DecryptBytes(Convert.FromBase64String(DecryptString),EncryptionKey)); } catch { return ""; } } }