using System.Security.Cryptography;
public static void Main()
var enc = Security.Encrypt("123", key);
var dec = Security.Decrypt(enc, key);
public static class Security
public static string KEY = "HELLO";
public DESKeyPack(byte[] data)
Buffer.BlockCopy(data, 0, Key, 0, 8);
Buffer.BlockCopy(data, 8, IV, 0, 8);
private static DESKeyPack genKeyPack(string keyString)
const string salt = "SALT";
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] data = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(keyString + salt));
DESKeyPack dkp = new DESKeyPack(data);
public static string Encrypt(string rawString, string keyString)
DESKeyPack dkp = genKeyPack(keyString);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform trans = des.CreateEncryptor(dkp.Key, dkp.IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Write);
byte[] rawData = UTF8Encoding.UTF8.GetBytes(rawString);
cs.Write(rawData, 0, rawData.Length);
return Convert.ToBase64String(ms.ToArray());
public static string Decrypt(string encString, string keyString)
DESKeyPack dkp = genKeyPack(keyString);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform trans = des.CreateDecryptor(dkp.Key, dkp.IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Write);
byte[] rawData = Convert.FromBase64String(encString);
cs.Write(rawData, 0, rawData.Length);
return UTF8Encoding.UTF8.GetString(ms.ToArray());