Advertisement
Guest User

Untitled

a guest
May 24th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.25 KB | None | 0 0
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.IO;
  4. using System.Text;
  5.  
  6. namespace Enigma
  7. {
  8.     class Program
  9.     {
  10.         private static string mode, inFile, outFile, algorithmName, keyFile;
  11.  
  12.         private static void CheckFile(string fileName, string type, string extension)
  13.         {
  14.             if (!File.Exists(fileName))
  15.             {
  16.                 throw new Exception("Файл " + fileName + " не найден.");
  17.             }
  18.  
  19.             if (!Path.GetExtension(fileName).Equals(extension))
  20.             {
  21.                 throw new Exception(type + " файл должен иметь расширение " + extension + ".");
  22.             }
  23.         }
  24.  
  25.         private static void ParseArgs(string[] args)
  26.         {
  27.             int len = args.Length;
  28.  
  29.             if (len != 4 && len != 5)
  30.             {
  31.                 throw new Exception("Некорректные аргументы.");
  32.             }
  33.             else
  34.             {
  35.                 mode = args[0];
  36.                 algorithmName = args[2];
  37.             }
  38.  
  39.             if (mode.Equals("encrypt"))
  40.             {
  41.                 if (len != 4)
  42.                 {
  43.                     throw new Exception("Некорректные аргументы.");
  44.                 }
  45.  
  46.                 inFile = args[1];
  47.  
  48.                 CheckFile(inFile, "Входной", ".txt");
  49.  
  50.                 outFile = args[3];
  51.  
  52.                 if (!Path.GetExtension(outFile).Equals(".bin"))
  53.                 {
  54.                     throw new Exception("Выходной файл должен иметь расширение bin.");
  55.                 }
  56.  
  57.                 StringBuilder builder = new StringBuilder();
  58.                 builder.Append(Path.GetFileNameWithoutExtension(inFile));
  59.                 builder.Append(".key.txt");
  60.                 keyFile = builder.ToString();
  61.             }
  62.             else if (mode.Equals("decrypt"))
  63.             {
  64.                 inFile = args[1];
  65.  
  66.                 CheckFile(inFile, "Входной", ".bin");
  67.  
  68.                 keyFile = args[3];
  69.  
  70.                 CheckFile(keyFile, "Ключевой", ".txt");
  71.  
  72.                 outFile = args[4];
  73.  
  74.                 if (!Path.GetExtension(outFile).Equals(".txt"))
  75.                 {
  76.                     throw new Exception("Выходной файл должен иметь расширение txt.");
  77.                 }
  78.             }
  79.             else
  80.             {
  81.                 throw new Exception("Первый аргумент может иметь значения: encrypt или decrypt");
  82.             }
  83.         }
  84.  
  85.         private static SymmetricAlgorithm GetAlgorithm(string algorithmName)
  86.         {
  87.             switch (algorithmName.ToLower())
  88.             {
  89.                 case "aes":
  90.                     {
  91.                         return new AesCryptoServiceProvider();
  92.                     }
  93.                 case "des":
  94.                     {
  95.                         return new DESCryptoServiceProvider();
  96.                     }
  97.                 case "rc2":
  98.                     {
  99.                         return new RC2CryptoServiceProvider();
  100.                     }
  101.                 case "rijndael":
  102.                     {
  103.                         return new RijndaelManaged();
  104.                     }
  105.                 default:
  106.                     {
  107.                         throw new Exception("Некорректное название алгоритма.");
  108.                     }
  109.             }
  110.         }
  111.  
  112.         private static void EncryptFile(SymmetricAlgorithm algorithm)
  113.         {
  114.             string iv, key;
  115.  
  116.             algorithm.GenerateIV();
  117.             algorithm.GenerateKey();
  118.  
  119.             iv = Convert.ToBase64String(algorithm.IV);
  120.             key = Convert.ToBase64String(algorithm.Key);
  121.  
  122.             using (StreamWriter keyStream = new StreamWriter(keyFile))
  123.             {
  124.                 keyStream.WriteLine(key);
  125.                 keyStream.WriteLine(iv);
  126.             }
  127.            
  128.             using (Stream inStream = new FileStream(inFile, FileMode.Open))
  129.             {
  130.                 using (Stream outputStream = new FileStream(outFile, FileMode.OpenOrCreate))
  131.                 {
  132.                     using (ICryptoTransform cryptoTransform = algorithm.CreateEncryptor())
  133.                     {
  134.                         using (CryptoStream cryptoStream = new CryptoStream(outputStream, cryptoTransform, CryptoStreamMode.Write))
  135.                         {
  136.                             inStream.CopyTo(cryptoStream);
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.  
  143.         private static void DecryptFile(SymmetricAlgorithm algorithm)
  144.         {
  145.             using (StreamReader keyStream = new StreamReader(keyFile))
  146.             {
  147.                 algorithm.Key = Convert.FromBase64String(keyStream.ReadLine());
  148.                 algorithm.IV = Convert.FromBase64String(keyStream.ReadLine());
  149.             }
  150.            
  151.             using (FileStream inputStream = new FileStream(inFile, FileMode.Open))
  152.             {
  153.                 using (FileStream outputStream = new FileStream(outFile, FileMode.OpenOrCreate))
  154.                 {
  155.                     using (ICryptoTransform cryptoTransform = algorithm.CreateDecryptor(algorithm.Key, algorithm.IV))
  156.                     {
  157.                         using (CryptoStream cryptoStream = new CryptoStream(inputStream, cryptoTransform, CryptoStreamMode.Read))
  158.                         {
  159.                             cryptoStream.CopyTo(outputStream);
  160.                         }
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.  
  166.         public static void Main(string[] args)
  167.         {
  168.             try
  169.             {
  170.                 ParseArgs(args);
  171.  
  172.                 using (SymmetricAlgorithm algorithm = GetAlgorithm(algorithmName))
  173.                 {
  174.                     if (mode.Equals("encrypt"))
  175.                     {
  176.                         EncryptFile(algorithm);
  177.                     }
  178.                     else
  179.                     {
  180.                         DecryptFile(algorithm);
  181.                     }
  182.                 }
  183.             }
  184.             catch (Exception e)
  185.             {
  186.                 Console.WriteLine(e.Message);
  187.             }
  188.         }
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement