EgorYankovsky

ok4

Nov 30th, 2022
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System.Security.Cryptography;
  2. using System.Text;
  3.  
  4. namespace Aes_Example
  5. {
  6.    class AesExample
  7.    {
  8.       public static void Main()
  9.       {
  10.          Console.WriteLine("Enter password");
  11.          string password = Console.ReadLine(), encrypted64, iv64;
  12.          byte[] hashValue;
  13.          var iv = RandomNumberGenerator.GetBytes(16);
  14.          string text = File.ReadAllText("text.txt");
  15.          
  16.          hashValue = SHA256.Create().ComputeHash(Encoding.ASCII.GetBytes(password));
  17.          
  18.          Console.WriteLine("Do you want to <1>encrypt <2>decrypt");
  19.          switch(Convert.ToInt32(Console.ReadLine()))
  20.          {
  21.             case 1:
  22.             {
  23.                 try
  24.                 {
  25.                     byte[] encrypted = Encrypt(text, hashValue, iv);
  26.                     encrypted64 = Convert.ToBase64String(encrypted, 0, encrypted.Length);
  27.                     iv64 = Convert.ToBase64String(iv, 0, iv.Length);
  28.                     File.WriteAllText("encryptedtext.txt", encrypted64);
  29.                     File.WriteAllText("iv.txt", iv64);
  30.                 }
  31.                 catch(FileNotFoundException ex)
  32.                 {
  33.                     Console.WriteLine($"{ex}");
  34.                 }
  35.                 break;
  36.             }
  37.             case 2:
  38.             {
  39.                 try
  40.                 {
  41.                     encrypted64 = File.ReadAllText("encryptedtext.txt");
  42.                     iv64 = File.ReadAllText("iv.txt");
  43.                     byte[] encrypted2 = Convert.FromBase64String(encrypted64);
  44.                     byte[] iv2 = Convert.FromBase64String(iv64);
  45.                     string roundtrip = Decrypt(encrypted2, hashValue, iv2);
  46.                     Console.WriteLine("Decoded text:\n{0}", roundtrip);
  47.                 }
  48.                 catch(FileNotFoundException ex)
  49.                 {
  50.                     Console.WriteLine($"{ex}");
  51.                 }
  52.                 break;
  53.             }
  54.             default:
  55.             {
  56.                 Console.WriteLine("Unknown command");
  57.                 break;
  58.             }
  59.          }
  60.       }
  61.  
  62.  
  63.  
  64.       static byte[] Encrypt(string plainText, byte[] Key, byte[] IV)
  65.       {
  66.          if (plainText == null || plainText.Length <= 0)
  67.             throw new ArgumentNullException("plainText");
  68.          if (Key == null || Key.Length <= 0)
  69.             throw new ArgumentNullException("Key");
  70.          if (IV == null || IV.Length <= 0)
  71.             throw new ArgumentNullException("IV");
  72.          byte[] encrypted;
  73.  
  74.          using (Aes aesAlg = Aes.Create())
  75.          {
  76.             aesAlg.Key = Key;
  77.             aesAlg.IV = IV;
  78.             aesAlg.Mode = CipherMode.CBC;
  79.             aesAlg.Padding = PaddingMode.PKCS7;
  80.             ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  81.             using (MemoryStream msEncrypt = new MemoryStream())
  82.             {
  83.                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  84.                {
  85.                   using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
  86.                   {
  87.                      swEncrypt.Write(plainText);
  88.                   }
  89.                   encrypted = msEncrypt.ToArray();
  90.                }
  91.             }
  92.          }
  93.          return encrypted;
  94.       }
  95.  
  96.      
  97.       static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV)
  98.       {
  99.          if (cipherText == null || cipherText.Length <= 0)
  100.             throw new ArgumentNullException("cipherText");
  101.          if (Key == null || Key.Length <= 0)
  102.             throw new ArgumentNullException("Key");
  103.          if (IV == null || IV.Length <= 0)
  104.             throw new ArgumentNullException("IV");
  105.          string plaintext = null;
  106.          using (Aes aesAlg = Aes.Create())
  107.          {
  108.             aesAlg.Key = Key;
  109.             aesAlg.IV = IV;
  110.             aesAlg.Mode = CipherMode.CBC;
  111.             aesAlg.Padding = PaddingMode.PKCS7;
  112.             ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  113.             using (MemoryStream msDecrypt = new MemoryStream(cipherText))
  114.             {
  115.                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  116.                {
  117.                   using (StreamReader srDecrypt = new StreamReader(csDecrypt))
  118.                   {
  119.                      plaintext = srDecrypt.ReadToEnd();
  120.                   }
  121.                }
  122.             }
  123.          }
  124.  
  125.          return plaintext;
  126.       }
  127.    }
  128. }
Add Comment
Please, Sign In to add comment