Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1.     /// <summary>
  2.     /// AesHelper jest klasą pomocniczą służącą do wykonywania najprostszych operacji na algorytmie AES.
  3.     /// </summary>
  4.     public static class AesHelper
  5.     {
  6.         /// <summary>
  7.         /// Metoda Encrypt szyfruje podany łańcuch znaków podanym hasłem i zwraca wynik tego szyfrowania.
  8.         /// </summary>
  9.         /// <param name="what">Tekst, który ma zostać zaszyfrowany</param>
  10.         /// <param name="key">Klucz, który jest potrzebny do rozszyfrowania tekstu</param>
  11.         /// <returns>Zaszyfrowany łańcuch znaków typu string</returns>
  12.         public static string Encrypt(string what, string key)
  13.         {
  14.             byte[] clearBytes = Encoding.Unicode.GetBytes(what);
  15.             using (Aes encryptor = Aes.Create())
  16.             {
  17.                 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  18.                 encryptor.Key = pdb.GetBytes(32);
  19.                 encryptor.IV = pdb.GetBytes(16);
  20.                 using (MemoryStream ms = new MemoryStream())
  21.                 {
  22.                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
  23.                     {
  24.                         cs.Write(clearBytes, 0, clearBytes.Length);
  25.                         cs.Close();
  26.                     }
  27.                     what = Convert.ToBase64String(ms.ToArray());
  28.                 }
  29.             }
  30.             return Convert.ToBase64String(Encoding.UTF8.GetBytes(new Random().Next(int.MinValue, int.MaxValue) + "<?>" + what + "<?>" + new Random().Next(int.MinValue, int.MaxValue)));
  31.         }
  32.  
  33.         /// <summary>
  34.         /// Metoda Decrypt służy do odszyfrowania podanego łańcucha znaków podanym hasłem i zwraca wynik tego szyfrowania.
  35.         /// </summary>
  36.         /// <param name="what">Zaszyfrowany tekst pochodzący z metody Encrypt</param>
  37.         /// <param name="key">Klucz, który podano podczas procesu szyfrowania</param>
  38.         /// <returns>Zwraca odszyfrowany ciąg znaków</returns>
  39.         public static string Decrypt(string what, string key)
  40.         {
  41.             what = Encoding.UTF8.GetString(Convert.FromBase64String(what)).Split(new string[] { "<?>" }, StringSplitOptions.RemoveEmptyEntries)[1];
  42.  
  43.             byte[] cipherBytes = Convert.FromBase64String(what);
  44.             using (Aes encryptor = Aes.Create())
  45.             {
  46.                 Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  47.                 encryptor.Key = pdb.GetBytes(32);
  48.                 encryptor.IV = pdb.GetBytes(16);
  49.                 using (MemoryStream ms = new MemoryStream())
  50.                 {
  51.                     using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
  52.                     {
  53.                         cs.Write(cipherBytes, 0, cipherBytes.Length);
  54.                         cs.Close();
  55.                     }
  56.                     what = Encoding.Unicode.GetString(ms.ToArray());
  57.                 }
  58.             }
  59.             return what;
  60.         }
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement