using System.Security.Cryptography;
public static void Main()
var alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (var i = 0; i < 100; i++)
var text = $"{Math.Floor((double)i / 10)}{i % 10}";
encrypted = DesEncrypt(text, key, salt);
decrypted = DesDecrypt(encrypted, key, salt);
Console.WriteLine($"Text: {text} | Encrypted: {encrypted} | Decrypted: {decrypted}");
private static string DesEncrypt(string plaintText, string strKey, string salt)
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
key = Encoding.UTF8.GetBytes(strKey);
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(plaintText + salt);
MemoryStream Objmst = new MemoryStream();
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateEncryptor(key, IV), CryptoStreamMode.Write);
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
return Convert.ToBase64String(Objmst.ToArray());
catch (System.Exception ex)
private static string DesDecrypt(string cipherText, string strKey, string salt)
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[cipherText.Length];
key = Encoding.UTF8.GetBytes(strKey);
DESCryptoServiceProvider ObjDES = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(cipherText);
MemoryStream Objmst = new MemoryStream();
CryptoStream Objcs = new CryptoStream(Objmst, ObjDES.CreateDecryptor(key, IV), CryptoStreamMode.Write);
Objcs.Write(inputByteArray, 0, inputByteArray.Length);
Encoding encoding = Encoding.UTF8;
return encoding.GetString(Objmst.ToArray()).TrimEnd(salt.ToCharArray());
catch (System.Exception ex)
if (ex is FormatException) Console.WriteLine("The token is in the wrong format");
if (ex is System.Security.Cryptography.CryptographicException) Console.WriteLine("Invalid token");