Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Security.Cryptography; //To use the cryptography functions
- using System.Text;
- namespace AES256
- {
- class Program
- {
- /* This is an implementation of the encryption algorithm AES256
- * for strings, very usefull to encrypt messages you don't
- * want an unauthorized person to have access.
- *
- * Created by: Andrew Vargas
- * Facebook: https://www.facebook.com/andrewvargas1991
- */
- static void Main(string[] args)
- {
- Console.Write("Digite alguma coisa: ");
- String originalString = Console.ReadLine(); //Read the string to be encrypted
- String encryptedString = Criptografia.Encrypt(originalString); //Call the encryption method
- String decryptedString = Criptografia.Decrypt(encryptedString); //Call the decryption method
- //Print the original string, the encrypted and the decrypted
- Console.WriteLine("\n\nOriginal: " + originalString);
- Console.WriteLine("Encrypted: " + encryptedString);
- Console.WriteLine("Decrypted: " + decryptedString);
- Console.WriteLine("\nPress any key to leave...");
- Console.ReadKey();
- }
- public class Criptografia //Class that contains the necessary variables and functions
- {
- /// Array of bytes utilized by the cryptography (External Key)
- private static byte[] bIV =
- { 0x50, 0x08, 0xF1, 0xDD, 0xDE, 0x3C, 0xF2, 0x18,
- 0x44, 0x74, 0x19, 0x2C, 0x53, 0x49, 0xAB, 0xBC };
- /* Important: Case you want to get the key (password) from the keyboard,
- you have to convert the string to base 64
- */
- private const string cryptoKey = "Q3JpcHRvZ3JhZmlhcyBjb20gUmluamRhZWwgLyBBRVM=";
- //Method to encrypt the string
- public static string Encrypt(string text)
- {
- try
- {
- // If the string is not void (empty), execute the encryption
- if (!string.IsNullOrEmpty(text))
- {
- // Instance the array of bytes with the keys
- byte[] bKey = Convert.FromBase64String(cryptoKey); //Return the cryptoKey
- //Print the value of cryptoKey char by char
- for (int i = 0; i < bKey.Length; i++)
- Console.Write((char)bKey[i]);
- byte[] bText = new UTF8Encoding().GetBytes(text);
- // Instance the class Rijndael (Rihndael = AES)
- Rijndael rijndael = new RijndaelManaged();
- // Defines the keysize "256 = 8 * 32"
- // There are other possibilities
- // 128 (16 characters), 192 (24 characters) e 256 (32 characters)
- rijndael.KeySize = 256;
- // Separate space in the memory to encrypt the string
- MemoryStream mStream = new MemoryStream();
- // Instance the encryptor
- CryptoStream encryptor = new CryptoStream(
- mStream, rijndael.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write);
- // Write the encrypted data in the memory
- encryptor.Write(bText, 0, bText.Length);
- // Clean the encryptor
- encryptor.FlushFinalBlock();
- // Return the array of bytes (encrypted string)
- return Convert.ToBase64String(mStream.ToArray());
- }
- else
- {
- // If the string is null
- return null;
- }
- }
- catch (Exception ex)
- {
- // If an error occurs
- throw new ApplicationException("Erro ao criptografar", ex);
- }
- }
- //Method to decrypt the string
- public static string Decrypt(string text)
- {
- /* Basically it has similar implementations to the Encrypt,
- * with few changes, therefore I will not comment
- */
- try
- {
- if (!string.IsNullOrEmpty(text))
- {
- byte[] bKey = Convert.FromBase64String(cryptoKey);
- byte[] bText = Convert.FromBase64String(text);
- Rijndael rijndael = new RijndaelManaged();
- rijndael.KeySize = 256;
- MemoryStream mStream = new MemoryStream();
- CryptoStream decryptor = new CryptoStream(mStream,
- rijndael.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write);
- decryptor.Write(bText, 0, bText.Length);
- decryptor.FlushFinalBlock();
- UTF8Encoding utf8 = new UTF8Encoding();
- return utf8.GetString(mStream.ToArray());
- }
- else
- {
- return null;
- }
- }
- catch (Exception ex)
- {
- throw new ApplicationException("Erro ao descriptografar", ex);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement