Advertisement
Shokedbrain

Untitled

Jun 8th, 2021
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4.  
  5. namespace artem_laba_2
  6. {
  7.     class Program
  8.     {
  9.  
  10.         public static byte[] StringToByteArray(string hex) {
  11.             byte[] bytes = new byte[hex.Length / 2];
  12.             for (int i = 0; i < hex.Length; i += 2)
  13.                 bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
  14.  
  15.             return bytes;
  16.         }
  17.  
  18.         private static byte[] EncryptAES(string input, byte[] Key, byte[] IV)
  19.         {
  20.             if (input == null || input.Length <= 0)
  21.                 throw new ArgumentNullException("cipherText");
  22.             if (Key == null || Key.Length <= 0)
  23.                 throw new ArgumentNullException("Key");
  24.             if (IV == null || IV.Length <= 0)
  25.                 throw new ArgumentNullException("Key");
  26.             byte[] encrypted;
  27.             using var aes = Aes.Create();
  28.             aes.Key = Key;
  29.             aes.IV = IV;
  30.             var encrypt = aes.CreateEncryptor(aes.Key, aes.IV); // ICryptoTransform
  31.             using var msEncrypt = new MemoryStream();
  32.             using var csEncrypt = new CryptoStream(msEncrypt, encrypt, CryptoStreamMode.Write);
  33.             using (var swEncrypt = new StreamWriter(csEncrypt))
  34.             {
  35.                 swEncrypt.Write(input);
  36.             }
  37.             encrypted = msEncrypt.ToArray();
  38.  
  39.             return encrypted;
  40.         }
  41.  
  42.         private static string DecryptAES(byte[] cipherText, byte[] Key, byte[] IV)
  43.         {
  44.             if (cipherText == null || cipherText.Length <= 0)
  45.                 throw new ArgumentNullException("cipherText");
  46.             if (Key == null || Key.Length <= 0)
  47.                 throw new ArgumentNullException("Key");
  48.             if (IV == null || IV.Length <= 0)
  49.                 throw new ArgumentNullException("Key");
  50.             var output = string.Empty;
  51.             using var aes = Aes.Create();
  52.             aes.Key = Key;
  53.             aes.IV = IV;
  54.             var decrypt = aes.CreateDecryptor(aes.Key, aes.IV); // ICryptoTransform
  55.             using var msDecrypt = new MemoryStream(cipherText);
  56.             using var csDecrypt = new CryptoStream(msDecrypt, decrypt, CryptoStreamMode.Read);
  57.             using var srDecrypt = new StreamReader(csDecrypt);
  58.             output = srDecrypt.ReadToEnd();
  59.  
  60.             return output;
  61.         }
  62.  
  63.         static void Main(string[] args)
  64.         {
  65.             Console.Write("1. Encrypt\n2. Decrypt\n");
  66.             Console.WriteLine("Choose: ");
  67.             int choice = int.Parse(Console.ReadLine()); // может быть null
  68.             byte[] aesKey, aesIV;
  69.             Console.Write("Enter key: ");
  70.             aesKey = StringToByteArray(Console.ReadLine());
  71.             Console.Write("Enter IV: ");
  72.             aesIV = StringToByteArray(Console.ReadLine());
  73.             string path;
  74.             do
  75.             {
  76.                 Console.Write("Enter path: ");
  77.                 path = Console.ReadLine();
  78.                 path = path.Trim();
  79.             } while (path == "");
  80.  
  81.             // проверка абсолютный или относительный путь
  82.             bool absolute = path.Contains('\\');
  83.             if (!absolute)
  84.                 path = Path.GetFullPath(path).ToString();    
  85.             Console.WriteLine(path);
  86.             // читаем из файла
  87.             try
  88.             {
  89.                 // шифруем
  90.                 switch (choice)
  91.                 {
  92.                     case 1:
  93.                     {
  94.                         using StreamReader sr = new StreamReader(path);
  95.                         string context = sr.ReadToEnd();
  96.                         byte[] encBytes = EncryptAES(context, aesKey, aesIV);
  97.                         string output = Path.GetFullPath("out.txt");
  98.                         Console.WriteLine($"File has been saved in {output}");
  99.                         File.WriteAllBytes(output, encBytes);
  100.                         string encrypted = BitConverter.ToString(encBytes);
  101.                         Console.WriteLine(encrypted);
  102.                     }
  103.                         break;
  104.                     case 2:
  105.                     {
  106.                         byte[] rd = File.ReadAllBytes(path);
  107.                         string decrypted = DecryptAES(rd,aesKey,aesIV);
  108.                         Console.WriteLine(decrypted);
  109.                     }
  110.                         break;
  111.                 }
  112.             }
  113.             catch (Exception e)
  114.             {
  115.                 Console.WriteLine(e.Message);
  116.             }
  117.         }
  118.     }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement