Paste Search Dynamic
keySize Encrypt
  1. using system;
  2. using system.IO;
  3. using system.Security.Cryptography;
  4. using system.Text;
  5.  
  6. public class Test
  7. {
  8.           public static string Encrypt(string plainText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
  9.   {
  10.     byte[] bytes1 = Encoding.ASCII.GetBytes(initVector);
  11.     byte[] bytes2 = Encoding.ASCII.GetBytes(saltValue);
  12.     byte[] bytes3 = Encoding.UTF8.GetBytes(plainText);
  13.     byte[] bytes4 = new PasswordDeriveBytes(passPhrase, bytes2, hashAlgorithm, passwordIterations).GetBytes(keySize / 8);
  14.     RijndaelManaged rijndaelManaged = new RijndaelManaged();
  15.     rijndaelManaged.Mode = CipherMode.CBC;
  16.     ICryptoTransform encryptor = rijndaelManaged.CreateEncryptor(bytes4, bytes1);
  17.     MemoryStream memoryStream = new MemoryStream();
  18.     CryptoStream cryptoStream = new CryptoStream((Stream) memoryStream, encryptor, CryptoStreamMode.Write);
  19.     cryptoStream.Write(bytes3, 0, bytes3.Length);
  20.     cryptoStream.FlushFinalBlock();
  21.     byte[] array = memoryStream.ToArray();
  22.     memoryStream.Close();
  23.     cryptoStream.Close();
  24.     return Convert.ToBase64String(array);
  25.   }
  26.  
  27.   public static string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
  28.   {
  29.     byte[] bytes1 = Encoding.ASCII.GetBytes(initVector);
  30.     byte[] bytes2 = Encoding.ASCII.GetBytes(saltValue);
  31.     byte[] buffer = Convert.FromBase64String(cipherText);
  32.     byte[] bytes3 = new PasswordDeriveBytes(passPhrase, bytes2, hashAlgorithm, passwordIterations).GetBytes(keySize / 8);
  33.     RijndaelManaged rijndaelManaged = new RijndaelManaged();
  34.     rijndaelManaged.Mode = CipherMode.CBC;
  35.     ICryptoTransform decryptor = rijndaelManaged.CreateDecryptor(bytes3, bytes1);
  36.     MemoryStream memoryStream = new MemoryStream(buffer);
  37.     CryptoStream cryptoStream = new CryptoStream((Stream) memoryStream, decryptor, CryptoStreamMode.Read);
  38.     byte[] numArray = new byte[buffer.Length];
  39.     int count = cryptoStream.Read(numArray, 0, numArray.Length);
  40.     memoryStream.Close();
  41.     cryptoStream.Close();
  42.     return Encoding.UTF8.GetString(numArray, 0, count);
  43.   }
  44.  
  45.         public static void Main()
  46.         {
  47.                     string plainText = "";
  48.     string cipherText = "BnCxGiN4aJDE+qUe2yIm8Q==";
  49.     string passPhrase = "^F79ejk56$£";
  50.     string saltValue = "DHj47&*)$h";
  51.     string hashAlgorithm = "MD5";
  52.     int passwordIterations = 1024;
  53.     string initVector = "&!£$%^&*()CvHgE!";
  54.     int keySize = 256;
  55.     Encrypt(plainText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);
  56.     string str = Decrypt(cipherText, passPhrase, saltValue, hashAlgorithm, passwordIterations, initVector, keySize);
  57.         Console.WriteLine("key" + str);
  58.  
  59.         }
  60. }
Parsed in 0.026 seconds