Advertisement
amv1991

AES256 (English explanations)

Apr 19th, 2020
1,423
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 1 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4.  
  5. namespace ConsoleApp1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             /*
  12.              *          -This code is an implementation of the algorithm of cryptography AES256;
  13.              *          -In order to use it, just copy and paste in a  Console Application then compile;
  14.              *          -With the ORIGINAL file the program will create an ENCRYTED and another DECRYTED;
  15.              *          -The ORIGINAL file and the DECRYPTED are identical, to be sure of that you can download
  16.              *              the program MD5 and SHA Checker here https://raylin.wordpress.com/downloads/md5-sha-1-checksum-utility/
  17.              *              and verify the file hashes;
  18.              *              
  19.              *          
  20.              *          Created by: Andrew Vargas
  21.              *          Facebook: https://www.facebook.com/andrewvargas1991
  22.              *          PS: You can download the compiled program (in Portuguese, but with the same functionality)
  23.              *              https://mega.nz/#!T15BQK6a!y5-z7UDxhauVTQKrF23ic81KI_muE4FwV3FOsq9RGzw
  24.              */
  25.  
  26.  
  27.             //To get the complete path of the file, just drag it to the console e press ENTER
  28.             Console.Write("Choose the file to be encrypted: ");
  29.             string Original = Console.ReadLine();
  30.  
  31.             //Remove the quotation marks of the file's name case it has
  32.             Original = Original.Replace("\"", "");
  33.  
  34.             //To format the name (with path) of the encrypted file
  35.             string Encriptado =
  36.                 Path.GetDirectoryName(Original) + "\\" +
  37.                 Path.GetFileNameWithoutExtension(Original) + "_Encryted" +
  38.                 Path.GetExtension(Original);
  39.  
  40.             //To format the name (with path) of the decrypted file
  41.             string Decriptado =
  42.                 Path.GetDirectoryName(Original) + "\\" +
  43.                 Path.GetFileNameWithoutExtension(Original) + "_Decryted" +
  44.                 Path.GetExtension(Original);
  45.  
  46.             //Get the password
  47.             Console.Write("Type a password: ");
  48.             string senha = Console.ReadLine();
  49.  
  50.             //Initialize the sal in bytes, the values inside the curly brackets represent the name Andrew in Unicode characters
  51.             byte[] sal_em_bytes = { 0x41, 0x6E, 0x64, 0x72, 0x65, 0x77 };
  52.  
  53.             //PS: To have a complete list of Unicode characters visit the website https://unicode-table.com/en/
  54.  
  55.             //Create the array of bytes to receive the original file and the encrypted
  56.             byte[] arquivo_original = File.ReadAllBytes(Original);  //Read the bytes of the original file
  57.             byte[] arquivo_encriptado = Encrypt(arquivo_original, senha, sal_em_bytes);  //Encrypt the bytes of the original file
  58.             File.WriteAllBytes(Encriptado, arquivo_encriptado);     //Write the bytes of the encrypted file
  59.  
  60.             //Create the array of bytes to receive the original file and the decrypted
  61.             byte[] arquivo_decriptado = Decrypt(arquivo_encriptado, senha, sal_em_bytes);    //Decrypt the bytes of the encrypted file
  62.             File.WriteAllBytes(Decriptado, arquivo_decriptado);     //Write the bytes of the decrypted file
  63.  
  64.             Console.WriteLine("\nPress any key to leave...");
  65.             Console.ReadKey();
  66.         }
  67.  
  68.         public static byte[] Encrypt(byte[] input, string pass, byte[] salt)  //Method to encrypt an array of bytes
  69.         {
  70.             //Instance the necessary objects to obtain the password and the salt (another password) utilized to derive the key
  71.             PasswordDeriveBytes pdb =
  72.             new PasswordDeriveBytes(pass, salt);
  73.  
  74.             MemoryStream ms = new MemoryStream();
  75.             Aes aes = new AesManaged();
  76.  
  77.             //Determine the keysize (256 bits = 32bytes, having in mind that each byte is equal to 8 bits), that's why is called AES256 (use a key of 256 bits)
  78.             aes.Key = pdb.GetBytes(aes.KeySize / 8);
  79.             aes.IV = pdb.GetBytes(aes.BlockSize / 8);
  80.             CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write);
  81.             cs.Write(input, 0, input.Length);
  82.             Console.WriteLine("\nThe file was successfully encrypted with AES" + aes.KeySize);
  83.             cs.Close();
  84.             return ms.ToArray();
  85.         }
  86.  
  87.         public static byte[] Decrypt(byte[] input, string pass, byte[] salt)  //Method to decrypt an array of bytes
  88.         {
  89.             //Instance the necessary objects to obtain the password and the salt (another password) utilized to derive the key
  90.             PasswordDeriveBytes pdb =
  91.             new PasswordDeriveBytes(pass, salt);            
  92.  
  93.             MemoryStream ms = new MemoryStream();
  94.             Aes aes = new AesManaged();
  95.             aes.Key = pdb.GetBytes(aes.KeySize / 8);
  96.             aes.IV = pdb.GetBytes(aes.BlockSize / 8);
  97.             CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
  98.             cs.Write(input, 0, input.Length);
  99.             Console.WriteLine("\nThe file was successfully decrypted with AES" + aes.KeySize);
  100.             cs.Close();
  101.             return ms.ToArray();
  102.         }
  103.     }
  104. }
  105. /*
  106.         IMPORTANT:
  107.         -The password and the salt can be initialized directly;
  108.         -It is recommended to initialize of the salt directly with an array of bytes;
  109.         -To create a program that only ENCRYPT a file, erase the Decrypt method, the variable
  110.             "arquivo_decriptado" and the functions related to this array of bytes;
  111.         -To create a program that only DECRYPT a file, erase the Encrypt method and change the array of bytes
  112.             to be inserted in the parameters of the Decrypt method;
  113.         -The salt is like a secondary password to give more security.
  114. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement