public static void Main()
Console.WriteLine("Hello World");
namespace Demo.Cryptography
using System.Security.Cryptography;
public class PKCSKeyGenerator
private byte[] key = new byte[8];
private byte[] iv = new byte[8];
private DESCryptoServiceProvider des = new DESCryptoServiceProvider();
public PKCSKeyGenerator()
public PKCSKeyGenerator(string keystring, byte[] salt, int iterationsMd5, int segments)
this.Generate(keystring, salt, iterationsMd5, segments);
public ICryptoTransform Encryptor
return this.des.CreateEncryptor(this.key, this.iv);
public ICryptoTransform Decryptor
return des.CreateDecryptor(key, iv);
public ICryptoTransform Generate(string keystring, byte[] salt, int iterationsMd5, int segments)
byte[] keyMaterial = new byte[hashLength * segments];
passwordBytes = Encoding.UTF8.GetBytes(keystring);
byte[] data00 = new byte[passwordBytes.Length + salt.Length];
Array.Copy(passwordBytes, data00, passwordBytes.Length);
Array.Copy(salt, 0, data00, passwordBytes.Length, salt.Length);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] hashtarget = new byte[hashLength + data00.Length];
for (int j = 0; j < segments; j++)
Array.Copy(result, hashtarget, result.Length);
Array.Copy(data00, 0, hashtarget, result.Length, data00.Length);
for (int i = 0; i < iterationsMd5; i++)
result = md5.ComputeHash(result);
Array.Copy(result, 0, keyMaterial, j * hashLength, result.Length);
Array.Copy(keyMaterial, 0, this.key, 0, 8);
Array.Copy(keyMaterial, 8, this.iv, 0, 8);
public class DemoEncryption
public string EncryptUsernamePassword(string clearText)
if (string.IsNullOrEmpty(clearText))
byte[] salt = { 0xc7, 0x73, 0x21, 0x8c, 0x7e, 0xc8, 0xee, 0x99 };
PKCSKeyGenerator crypto = new PKCSKeyGenerator("MyPassword", salt, 20, 1);
ICryptoTransform cryptoTransform = crypto.Encryptor;
var cipherBytes = cryptoTransform.TransformFinalBlock(Encoding.UTF8.GetBytes(clearText), 0, clearText.Length);
return Convert.ToBase64String(cipherBytes);
public string DecryptUsernamePassword(string cipherText)
if (string.IsNullOrEmpty(cipherText))
byte[] salt = { 0xc7, 0x73, 0x21, 0x8c, 0x7e, 0xc8, 0xee, 0x99 };
PKCSKeyGenerator crypto = new PKCSKeyGenerator("MyPassword", salt, 20, 1);
ICryptoTransform cryptoTransform = crypto.Decryptor;
var cipherBytes = Convert.FromBase64String(cipherText);
var clearBytes = cryptoTransform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
return Encoding.UTF8.GetString(clearBytes);