Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Security.Cryptography;
- using System.Text;
- namespace Aes_Example
- {
- class AesExample
- {
- public static void Main()
- {
- Console.WriteLine("Enter password");
- string password = Console.ReadLine(), encrypted64, iv64;
- byte[] hashValue;
- var iv = RandomNumberGenerator.GetBytes(16);
- string text = File.ReadAllText("text.txt");
- hashValue = SHA256.Create().ComputeHash(Encoding.ASCII.GetBytes(password));
- Console.WriteLine("Do you want to <1>encrypt <2>decrypt");
- switch(Convert.ToInt32(Console.ReadLine()))
- {
- case 1:
- {
- try
- {
- byte[] encrypted = Encrypt(text, hashValue, iv);
- encrypted64 = Convert.ToBase64String(encrypted, 0, encrypted.Length);
- iv64 = Convert.ToBase64String(iv, 0, iv.Length);
- File.WriteAllText("encryptedtext.txt", encrypted64);
- File.WriteAllText("iv.txt", iv64);
- }
- catch(FileNotFoundException ex)
- {
- Console.WriteLine($"{ex}");
- }
- break;
- }
- case 2:
- {
- try
- {
- encrypted64 = File.ReadAllText("encryptedtext.txt");
- iv64 = File.ReadAllText("iv.txt");
- byte[] encrypted2 = Convert.FromBase64String(encrypted64);
- byte[] iv2 = Convert.FromBase64String(iv64);
- string roundtrip = Decrypt(encrypted2, hashValue, iv2);
- Console.WriteLine("Decoded text:\n{0}", roundtrip);
- }
- catch(FileNotFoundException ex)
- {
- Console.WriteLine($"{ex}");
- }
- break;
- }
- default:
- {
- Console.WriteLine("Unknown command");
- break;
- }
- }
- }
- static byte[] Encrypt(string plainText, byte[] Key, byte[] IV)
- {
- if (plainText == null || plainText.Length <= 0)
- throw new ArgumentNullException("plainText");
- if (Key == null || Key.Length <= 0)
- throw new ArgumentNullException("Key");
- if (IV == null || IV.Length <= 0)
- throw new ArgumentNullException("IV");
- byte[] encrypted;
- using (Aes aesAlg = Aes.Create())
- {
- aesAlg.Key = Key;
- aesAlg.IV = IV;
- aesAlg.Mode = CipherMode.CBC;
- aesAlg.Padding = PaddingMode.PKCS7;
- ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
- using (MemoryStream msEncrypt = new MemoryStream())
- {
- using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
- {
- using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
- {
- swEncrypt.Write(plainText);
- }
- encrypted = msEncrypt.ToArray();
- }
- }
- }
- return encrypted;
- }
- static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
- {
- if (cipherText == null || cipherText.Length <= 0)
- throw new ArgumentNullException("cipherText");
- if (Key == null || Key.Length <= 0)
- throw new ArgumentNullException("Key");
- if (IV == null || IV.Length <= 0)
- throw new ArgumentNullException("IV");
- string plaintext = null;
- using (Aes aesAlg = Aes.Create())
- {
- aesAlg.Key = Key;
- aesAlg.IV = IV;
- aesAlg.Mode = CipherMode.CBC;
- aesAlg.Padding = PaddingMode.PKCS7;
- ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
- using (MemoryStream msDecrypt = new MemoryStream(cipherText))
- {
- using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
- {
- using (StreamReader srDecrypt = new StreamReader(csDecrypt))
- {
- plaintext = srDecrypt.ReadToEnd();
- }
- }
- }
- }
- return plaintext;
- }
- }
- }
Add Comment
Please, Sign In to add comment