daily pastebin goal
39%
SHARE
TWEET

Untitled

TKGP Aug 8th, 2018 119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         /// <summary>
  2.         /// Encrypt a byte array using AES 128
  3.         /// </summary>
  4.         /// <param name="key">128 bit key</param>
  5.         /// <param name="secret">byte array that need to be encrypted</param>
  6.         /// <returns>Encrypted array</returns>
  7.         public static byte[] EncryptByteArray(byte[] key, byte[] secret)
  8.         {
  9.             using (MemoryStream ms = new MemoryStream())
  10.             using (AesManaged cryptor = new AesManaged())
  11.             {
  12.                 cryptor.Mode = CipherMode.CBC;
  13.                 cryptor.Padding = PaddingMode.PKCS7;
  14.                 cryptor.KeySize = 128;
  15.                 cryptor.BlockSize = 128;
  16.  
  17.                 //We use the random generated iv created by AesManaged
  18.                 byte[] iv = cryptor.IV;
  19.  
  20.                 using (CryptoStream cs = new CryptoStream(ms, cryptor.CreateEncryptor(key, iv), CryptoStreamMode.Write))
  21.                 {
  22.                     cs.Write(secret, 0, secret.Length);
  23.                 }
  24.                 byte[] encryptedContent = ms.ToArray();
  25.  
  26.                 //Create new byte array that should contain both unencrypted iv and encrypted data
  27.                 byte[] result = new byte[iv.Length + encryptedContent.Length];
  28.  
  29.                 //copy our 2 array into one
  30.                 System.Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
  31.                 System.Buffer.BlockCopy(encryptedContent, 0, result, iv.Length, encryptedContent.Length);
  32.  
  33.                 return result;
  34.             }
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Decrypt a byte array using AES 128
  39.         /// </summary>
  40.         /// <param name="key">key in bytes</param>
  41.         /// <param name="secret">the encrypted bytes</param>
  42.         /// <returns>decrypted bytes</returns>
  43.         public static byte[] DecryptByteArray(byte[] key, byte[] secret)
  44.         {
  45.             byte[] iv = new byte[16]; //initial vector is 16 bytes
  46.             byte[] encryptedContent = new byte[secret.Length - 16]; //the rest should be encryptedcontent
  47.  
  48.             //Copy data to byte array
  49.             System.Buffer.BlockCopy(secret, 0, iv, 0, iv.Length);
  50.             System.Buffer.BlockCopy(secret, iv.Length, encryptedContent, 0, encryptedContent.Length);
  51.  
  52.             using (MemoryStream ms = new MemoryStream())
  53.             using (AesManaged cryptor = new AesManaged())
  54.             {
  55.                 cryptor.Mode = CipherMode.CBC;
  56.                 cryptor.Padding = PaddingMode.None;
  57.                 cryptor.KeySize = 128;
  58.                 cryptor.BlockSize = 128;
  59.  
  60.                 using (CryptoStream cs = new CryptoStream(ms, cryptor.CreateDecryptor(key, iv), CryptoStreamMode.Write))
  61.                 {
  62.                     cs.Write(encryptedContent, 0, encryptedContent.Length);
  63.                 }
  64.                 return ms.ToArray();
  65.             }
  66.         }
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