SHARE
TWEET

Untitled

a guest Feb 19th, 2020 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace KoCoS.Chat.Cryptography
  10. {
  11.  
  12.     /// <summary> Ver- und Entschlüsselt strings </summary>
  13.     public class Crypter
  14.     {
  15.  
  16.         /// <summary> Verschlüsselt einen string (symmetrische Verschlüsselung) </summary>
  17.         /// <param name="clearText">Klartext</param>
  18.         /// <param name="Key">Schlüssel</param>
  19.         /// <param name="IV">Initialisierungsvektor</param>
  20.         /// <returns>verschlüsselter String</returns>
  21.         private static byte[] EncryptSymmetricalString(byte[] clearText, byte[] Key, byte[] IV)
  22.         {
  23.             MemoryStream ms = new MemoryStream();
  24.             Rijndael alg = Rijndael.Create();
  25.             alg.Key = Key;
  26.             alg.IV = IV;
  27.             CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
  28.             cs.Write(clearText, 0, clearText.Length);
  29.             cs.Close();
  30.             byte[] encryptedData = ms.ToArray();
  31.             return encryptedData;
  32.         }
  33.  
  34.         /// <summary> Verschlüsselt einen string (symmetrische Verschlüsselung) </summary>
  35.         /// <param name="clearText">Klartext</param>
  36.         /// <param name="password">Passwort (Schlüssel zum Verschlüsseln)</param>
  37.         /// <returns>verschlüsselter String</returns>
  38.         public static string EncryptSymmetricalString(string clearText, string password)
  39.         {
  40.             byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
  41.             byte[] encryptedData = EncryptSymmetricalBytes(clearBytes, password);
  42.             return Convert.ToBase64String(encryptedData);
  43.         }
  44.  
  45.         public static byte[] EncryptSymmetricalBytes(byte[] rawBytes, string password)
  46.         {
  47.             PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  48.             return EncryptSymmetricalString(rawBytes, pdb.GetBytes(32), pdb.GetBytes(16));
  49.         }
  50.  
  51.         /// <summary> Entschlüsselt einen string (symmetrische Verschlüsselung) </summary>
  52.         /// <param name="cipherData">verschlüsselte Daten</param>
  53.         /// <param name="Key">Schlüssel der zum Verschlüsseln benutzt wurde</param>
  54.         /// <param name="IV">benutzter Initialisierungsvektor</param>
  55.         /// <returns>entschlüsselter string</returns>
  56.         private static byte[] DecryptSymmetricalString(byte[] cipherData, byte[] Key, byte[] IV)
  57.         {
  58.             MemoryStream ms = new MemoryStream();
  59.             Rijndael alg = Rijndael.Create();
  60.             alg.Key = Key;
  61.             alg.IV = IV;
  62.             CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
  63.             cs.Write(cipherData, 0, cipherData.Length);
  64.             cs.Close();
  65.             byte[] decryptedData = ms.ToArray();
  66.             return decryptedData;
  67.         }
  68.  
  69.         /// <summary> Entschlüssekt einen string (symmetrische Verschlüsselung) </summary>
  70.         /// <param name="cipherText">verschlüsselter Text</param>
  71.         /// <param name="Password">Passwort welches zum Verschlüsseln benutzt wurde</param>
  72.         /// <returns>entschlüsselter string</returns>
  73.         public static string DecryptSymmetricalString(string cipherText, string Password)
  74.         {
  75.             byte[] cipherBytes = Convert.FromBase64String(cipherText);
  76.             PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  77.             byte[] decryptedData = DecryptSymmetricalString(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
  78.             return System.Text.Encoding.Unicode.GetString(decryptedData);
  79.         }
  80.  
  81.         public static byte[] DecryptSymmetricalBytes(byte[] rawBytes, string password) {
  82.             PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  83.             return DecryptSymmetricalString(rawBytes, pdb.GetBytes(32), pdb.GetBytes(16));
  84.         }
  85.  
  86.         public static byte[] EncryptAsymmetricalBytes(byte[] byteToCrypt, int byteLength, byte[] key, byte[] iv)
  87.         {
  88.             AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider();
  89.             aesCryptoServiceProvider.Key = key;
  90.             aesCryptoServiceProvider.IV = iv;
  91.  
  92.             var encryptor = aesCryptoServiceProvider.CreateEncryptor();
  93.             return encryptor.TransformFinalBlock(byteToCrypt, 0, byteLength);
  94.         }
  95.  
  96.         public static byte[] DecryptAsymmetricalBytes(byte[] byteToDecrypt, int byteLength, byte[] key, byte[] iv)
  97.         {
  98.             AesCryptoServiceProvider aesCryptoServiceProvider = new AesCryptoServiceProvider();
  99.             aesCryptoServiceProvider.Key = key;
  100.             aesCryptoServiceProvider.IV = iv;
  101.  
  102.             var decryptor = aesCryptoServiceProvider.CreateDecryptor();
  103.             return decryptor.TransformFinalBlock(byteToDecrypt, 0, byteLength);
  104.         }
  105.  
  106.         public static string GetMD5Hash(string input) {
  107.             var md5 = MD5.Create();
  108.             var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
  109.             return Convert.ToBase64String(hash);
  110.         }
  111.     }
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top