Advertisement
FrayxRulez

AES

May 29th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1.     public static class Cryptography
  2.     {
  3.         #region Settings
  4.  
  5.         private static int _iterations = 2;
  6.         private static int _keySize = 256;
  7.  
  8.         private static string _hash = "SHA1";
  9.         private static string _salt = "aselrias38490a32"; // Random
  10.         private static string _vector = "8947az34awl34kjq"; // Random
  11.  
  12.         #endregion
  13.  
  14.         public static string Encrypt(string value, string password)
  15.         {
  16.             return Encrypt<AesManaged>(value, password);
  17.         }
  18.         public static string Encrypt<T>(string value, string password)
  19.                 where T : SymmetricAlgorithm, new()
  20.         {
  21.             byte[] vectorBytes = Encoding.ASCII.GetBytes(_vector);
  22.             byte[] saltBytes = Encoding.ASCII.GetBytes(_salt);
  23.             byte[] valueBytes = Encoding.UTF8.GetBytes(value);
  24.  
  25.             byte[] encrypted;
  26.             using (T cipher = new T())
  27.             {
  28.                 PasswordDeriveBytes _passwordBytes =
  29.                     new PasswordDeriveBytes(password, saltBytes, _hash, _iterations);
  30.                 byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);
  31.  
  32.                 cipher.Mode = CipherMode.CBC;
  33.  
  34.                 using (ICryptoTransform encryptor = cipher.CreateEncryptor(keyBytes, vectorBytes))
  35.                 {
  36.                     using (MemoryStream to = new MemoryStream())
  37.                     {
  38.                         using (CryptoStream writer = new CryptoStream(to, encryptor, CryptoStreamMode.Write))
  39.                         {
  40.                             writer.Write(valueBytes, 0, valueBytes.Length);
  41.                             writer.FlushFinalBlock();
  42.                             encrypted = to.ToArray();
  43.                         }
  44.                     }
  45.                 }
  46.                 cipher.Clear();
  47.             }
  48.             return Convert.ToBase64String(encrypted);
  49.         }
  50.  
  51.         public static string Decrypt(string value, string password)
  52.         {
  53.             return Decrypt<AesManaged>(value, password);
  54.         }
  55.         public static string Decrypt<T>(string value, string password) where T : SymmetricAlgorithm, new()
  56.         {
  57.             byte[] vectorBytes = Encoding.ASCII.GetBytes(_vector);
  58.             byte[] saltBytes = Encoding.ASCII.GetBytes(_salt);
  59.             byte[] valueBytes = Convert.FromBase64String(value);
  60.  
  61.             byte[] decrypted;
  62.             int decryptedByteCount = 0;
  63.  
  64.             using (T cipher = new T())
  65.             {
  66.                 PasswordDeriveBytes _passwordBytes = new PasswordDeriveBytes(password, saltBytes, _hash, _iterations);
  67.                 byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);
  68.  
  69.                 cipher.Mode = CipherMode.CBC;
  70.  
  71.                 try
  72.                 {
  73.                     using (ICryptoTransform decryptor = cipher.CreateDecryptor(keyBytes, vectorBytes))
  74.                     {
  75.                         using (MemoryStream from = new MemoryStream(valueBytes))
  76.                         {
  77.                             using (CryptoStream reader = new CryptoStream(from, decryptor, CryptoStreamMode.Read))
  78.                             {
  79.                                 decrypted = new byte[valueBytes.Length];
  80.                                 decryptedByteCount = reader.Read(decrypted, 0, decrypted.Length);
  81.                             }
  82.                         }
  83.                     }
  84.                 }
  85.                 catch (Exception ex)
  86.                 {
  87.                     return String.Empty;
  88.                 }
  89.  
  90.                 cipher.Clear();
  91.             }
  92.             return Encoding.UTF8.GetString(decrypted, 0, decryptedByteCount);
  93.         }
  94.  
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement