using System.Text.RegularExpressions;
using System.Security.Cryptography;
public static void Main(string[] args)
Console.WriteLine(Encrypt("4NTDMRLNJPGZRBERDJHDEKVH", "{'EVENT':'SALE','REF_ID':'29082016','SERIAL_NUMBER':'32653912','TERMINAL_ID':'','AMOUNT':'000000100000','CURRENCY_CODE':'704','STAFF_ID':'','ADD_PRINT':'','ADD_DATA':'','ORDER_ID':'','APP':'PAYMENT_STD','MERCHANT_OUTLET_ID':'000000000000000000002','MERCHANT_ID':'000000000000002','CLIENT_ID':'0001'}"));
Console.WriteLine(Decrypt("4NTDMRLNJPGZRBERDJHDEKVH", "45317454454c52616635303562645030703132303473386f7642354167344e704d4d4b7a4e557448684b2f537069652f716b322f34556b5944774961592b4d4a7448317557626e7667476442536f702b543151685835554e6b4e786d394730397936417151376f6234686b3d"));
public static string Encrypt(string encryptionKey , string textToEncrypt)
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider ();
tdes.Key = Encoding.ASCII.GetBytes(encryptionKey);
tdes.IV = new byte[] { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
byte[] stringBytes = Encoding.UTF8.GetBytes(textToEncrypt);
string st = Convert.ToBase64String(tdes.CreateEncryptor().TransformFinalBlock(stringBytes ,0, stringBytes.Length));
byte[] toBytes = Encoding.ASCII.GetBytes(st);
string s = BitConverter.ToString(toBytes).Replace("-", "").ToLower ();
Decrypt(encryptionKey , s);
public static string Decrypt(string encryptionKey , string textToDecrypt)
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider ();
des.Key = Encoding.ASCII.GetBytes(encryptionKey);
des.IV = new byte[] { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
byte[] fromByte = StringToByteArray(textToDecrypt);
string st = Encoding.ASCII.GetString(fromByte);
byte[] stringBytes = Convert.FromBase64String(st);
return Encoding.ASCII.GetString(des.CreateDecryptor().TransformFinalBlock(stringBytes , 0,stringBytes.Length));
public static byte[] StringToByteArray(string hex)
return Enumerable.Range(0, hex.Length)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))