Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Security.Cryptography;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- /*
- * -This code is an implementation of the algorithm of cryptography AES256;
- * -In order to use it, just copy and paste in a Console Application then compile;
- * -With the ORIGINAL file the program will create an ENCRYTED and another DECRYTED;
- * -The ORIGINAL file and the DECRYPTED are identical, to be sure of that you can download
- * the program MD5 and SHA Checker here https://raylin.wordpress.com/downloads/md5-sha-1-checksum-utility/
- * and verify the file hashes;
- *
- *
- * Created by: Andrew Vargas
- * Facebook: https://www.facebook.com/andrewvargas1991
- * PS: You can download the compiled program (in Portuguese, but with the same functionality)
- * https://mega.nz/#!T15BQK6a!y5-z7UDxhauVTQKrF23ic81KI_muE4FwV3FOsq9RGzw
- */
- //To get the complete path of the file, just drag it to the console e press ENTER
- Console.Write("Choose the file to be encrypted: ");
- string Original = Console.ReadLine();
- //Remove the quotation marks of the file's name case it has
- Original = Original.Replace("\"", "");
- //To format the name (with path) of the encrypted file
- string Encriptado =
- Path.GetDirectoryName(Original) + "\\" +
- Path.GetFileNameWithoutExtension(Original) + "_Encryted" +
- Path.GetExtension(Original);
- //To format the name (with path) of the decrypted file
- string Decriptado =
- Path.GetDirectoryName(Original) + "\\" +
- Path.GetFileNameWithoutExtension(Original) + "_Decryted" +
- Path.GetExtension(Original);
- //Get the password
- Console.Write("Type a password: ");
- string senha = Console.ReadLine();
- //Initialize the sal in bytes, the values inside the curly brackets represent the name Andrew in Unicode characters
- byte[] sal_em_bytes = { 0x41, 0x6E, 0x64, 0x72, 0x65, 0x77 };
- //PS: To have a complete list of Unicode characters visit the website https://unicode-table.com/en/
- //Create the array of bytes to receive the original file and the encrypted
- byte[] arquivo_original = File.ReadAllBytes(Original); //Read the bytes of the original file
- byte[] arquivo_encriptado = Encrypt(arquivo_original, senha, sal_em_bytes); //Encrypt the bytes of the original file
- File.WriteAllBytes(Encriptado, arquivo_encriptado); //Write the bytes of the encrypted file
- //Create the array of bytes to receive the original file and the decrypted
- byte[] arquivo_decriptado = Decrypt(arquivo_encriptado, senha, sal_em_bytes); //Decrypt the bytes of the encrypted file
- File.WriteAllBytes(Decriptado, arquivo_decriptado); //Write the bytes of the decrypted file
- Console.WriteLine("\nPress any key to leave...");
- Console.ReadKey();
- }
- public static byte[] Encrypt(byte[] input, string pass, byte[] salt) //Method to encrypt an array of bytes
- {
- //Instance the necessary objects to obtain the password and the salt (another password) utilized to derive the key
- PasswordDeriveBytes pdb =
- new PasswordDeriveBytes(pass, salt);
- MemoryStream ms = new MemoryStream();
- Aes aes = new AesManaged();
- //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)
- aes.Key = pdb.GetBytes(aes.KeySize / 8);
- aes.IV = pdb.GetBytes(aes.BlockSize / 8);
- CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write);
- cs.Write(input, 0, input.Length);
- Console.WriteLine("\nThe file was successfully encrypted with AES" + aes.KeySize);
- cs.Close();
- return ms.ToArray();
- }
- public static byte[] Decrypt(byte[] input, string pass, byte[] salt) //Method to decrypt an array of bytes
- {
- //Instance the necessary objects to obtain the password and the salt (another password) utilized to derive the key
- PasswordDeriveBytes pdb =
- new PasswordDeriveBytes(pass, salt);
- MemoryStream ms = new MemoryStream();
- Aes aes = new AesManaged();
- aes.Key = pdb.GetBytes(aes.KeySize / 8);
- aes.IV = pdb.GetBytes(aes.BlockSize / 8);
- CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
- cs.Write(input, 0, input.Length);
- Console.WriteLine("\nThe file was successfully decrypted with AES" + aes.KeySize);
- cs.Close();
- return ms.ToArray();
- }
- }
- }
- /*
- IMPORTANT:
- -The password and the salt can be initialized directly;
- -It is recommended to initialize of the salt directly with an array of bytes;
- -To create a program that only ENCRYPT a file, erase the Decrypt method, the variable
- "arquivo_decriptado" and the functions related to this array of bytes;
- -To create a program that only DECRYPT a file, erase the Encrypt method and change the array of bytes
- to be inserted in the parameters of the Decrypt method;
- -The salt is like a secondary password to give more security.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement