priore

AES256 Encrypt/Decrypt

Nov 10th, 2014
245
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. // https://github.com/priore/SOAPEngine/blob/master/CSHARP/AES256Class.cs
  3. //
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Security.Cryptography;
  9. using System.IO;
  10. using System.Text;
  11.  
  12. namespace SOAPEngine.Cryptography
  13. {
  14.     public class AES256Class
  15.     {
  16.         public static string EncryptText(string input, string password)
  17.         {
  18.             // Get the bytes of the string
  19.             byte[] bytesToBeEncrypted = UTF8Encoding.UTF8.GetBytes(input);
  20.             byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
  21.  
  22.             // Hash the password with SHA256
  23.             passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  24.             byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
  25.             return Convert.ToBase64String(bytesEncrypted);
  26.         }
  27.  
  28.         public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
  29.         {
  30.             byte[] encryptedBytes = null;
  31.  
  32.             using (RijndaelManaged AES = new RijndaelManaged())
  33.             {
  34.                 AES.KeySize = 256;
  35.                 AES.BlockSize = 128;
  36.                 AES.Mode = CipherMode.ECB;
  37.                 AES.Padding = PaddingMode.PKCS7;
  38.                 AES.Key = passwordBytes;
  39.  
  40.                 using (ICryptoTransform encrypto = AES.CreateEncryptor())
  41.                 {
  42.                     encryptedBytes = encrypto.TransformFinalBlock(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
  43.                 }
  44.             }
  45.  
  46.             return encryptedBytes;
  47.         }
  48.  
  49.  
  50.         public static string DecryptText(string input, string password)
  51.         {
  52.             // Get the bytes of the string
  53.             byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
  54.             byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
  55.             passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  56.  
  57.             byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
  58.             return UTF8Encoding.UTF8.GetString(bytesDecrypted);
  59.         }
  60.  
  61.         public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
  62.         {
  63.             byte[] decryptedBytes = null;
  64.  
  65.             using (RijndaelManaged AES = new RijndaelManaged())
  66.             {
  67.                 AES.KeySize = 256;
  68.                 AES.BlockSize = 128;
  69.                 AES.Mode = CipherMode.ECB;
  70.                 AES.Padding = PaddingMode.PKCS7;
  71.                 AES.Key = passwordBytes;
  72.  
  73.                 using (ICryptoTransform decrypto = AES.CreateDecryptor())
  74.                 {
  75.                     decryptedBytes = decrypto.TransformFinalBlock(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
  76.                 }
  77.             }
  78.  
  79.             return decryptedBytes;
  80.         }
  81.     }
  82. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×