Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Security.Cryptography;
- namespace artem_laba_2
- {
- class Program
- {
- public static byte[] StringToByteArray(string hex) {
- byte[] bytes = new byte[hex.Length / 2];
- for (int i = 0; i < hex.Length; i += 2)
- bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
- return bytes;
- }
- private static byte[] EncryptAES(string input, byte[] Key, byte[] IV)
- {
- if (input == null || input.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("Key");
- byte[] encrypted;
- using var aes = Aes.Create();
- aes.Key = Key;
- aes.IV = IV;
- var encrypt = aes.CreateEncryptor(aes.Key, aes.IV); // ICryptoTransform
- using var msEncrypt = new MemoryStream();
- using var csEncrypt = new CryptoStream(msEncrypt, encrypt, CryptoStreamMode.Write);
- using (var swEncrypt = new StreamWriter(csEncrypt))
- {
- swEncrypt.Write(input);
- }
- encrypted = msEncrypt.ToArray();
- return encrypted;
- }
- private static string DecryptAES(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("Key");
- var output = string.Empty;
- using var aes = Aes.Create();
- aes.Key = Key;
- aes.IV = IV;
- var decrypt = aes.CreateDecryptor(aes.Key, aes.IV); // ICryptoTransform
- using var msDecrypt = new MemoryStream(cipherText);
- using var csDecrypt = new CryptoStream(msDecrypt, decrypt, CryptoStreamMode.Read);
- using var srDecrypt = new StreamReader(csDecrypt);
- output = srDecrypt.ReadToEnd();
- return output;
- }
- static void Main(string[] args)
- {
- Console.Write("1. Encrypt\n2. Decrypt\n");
- Console.WriteLine("Choose: ");
- int choice = int.Parse(Console.ReadLine()); // может быть null
- byte[] aesKey, aesIV;
- Console.Write("Enter key: ");
- aesKey = StringToByteArray(Console.ReadLine());
- Console.Write("Enter IV: ");
- aesIV = StringToByteArray(Console.ReadLine());
- string path;
- do
- {
- Console.Write("Enter path: ");
- path = Console.ReadLine();
- path = path.Trim();
- } while (path == "");
- // проверка абсолютный или относительный путь
- bool absolute = path.Contains('\\');
- if (!absolute)
- path = Path.GetFullPath(path).ToString();
- Console.WriteLine(path);
- // читаем из файла
- try
- {
- // шифруем
- switch (choice)
- {
- case 1:
- {
- using StreamReader sr = new StreamReader(path);
- string context = sr.ReadToEnd();
- byte[] encBytes = EncryptAES(context, aesKey, aesIV);
- string output = Path.GetFullPath("out.txt");
- Console.WriteLine($"File has been saved in {output}");
- File.WriteAllBytes(output, encBytes);
- string encrypted = BitConverter.ToString(encBytes);
- Console.WriteLine(encrypted);
- }
- break;
- case 2:
- {
- byte[] rd = File.ReadAllBytes(path);
- string decrypted = DecryptAES(rd,aesKey,aesIV);
- Console.WriteLine(decrypted);
- }
- break;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement