Advertisement
nekonatum

Vijener decryptor combination

Jun 22nd, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace vij_brut
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Vijener a = new Vijener();
  13.             int counter = 1;
  14.             Console.Write("Введите кодовое слово: ");
  15.             string key = Console.ReadLine();
  16.             Console.Write("Введите имя файла и, если нужно, путь до него: ");
  17.             StreamReader rdr = new StreamReader(Console.ReadLine());
  18.             Console.WriteLine("1 - считать в строку, 2 - считать в массив для перебора");
  19.             string option = Console.ReadLine();
  20.             bool ok = true;
  21.             switch (option)
  22.             {
  23.                 case "1":
  24.                     string text = rdr.ReadLine();
  25.                     Console.WriteLine("Decoded text: {0}", a.Decryption(text, key, Vijener.Option.First));
  26.                     break;
  27.                 case "2":
  28.                     Console.Write("Введите число элементов: ");
  29.                     int num = Convert.ToInt32(Console.ReadLine());
  30.                     string[] items = new string[num];
  31.                     items = rdr.ReadLine().Split(new Char[] { ' ', ',', '.', ':', '\t' });
  32.                     StreamWriter wdr = new StreamWriter("out.txt");
  33.                     foreach (string[] permutation in Permutation.GetPermutations<string>(items))
  34.                     {
  35.                         string i = String.Join("", permutation);
  36.                         var dec_i = a.Decryption(i, key, Vijener.Option.First);
  37.                         Console.WriteLine("{0}. Combination {1} and decoded is {2}", counter, i, dec_i);
  38.                         wdr.WriteLine("{0}. Combination {1} and decoded is {2}", counter, i, dec_i);
  39.                         counter++;
  40.                     }
  41.                     break;
  42.                 default: Console.WriteLine("Try again"); ok = false; break;
  43.             }
  44.             Console.ReadKey();
  45.         }
  46.         public class Permutation
  47.         {
  48.  
  49.             public static IEnumerable<T[]> GetPermutations<T>(T[] items)
  50.             {
  51.                 int[] work = new int[items.Length];
  52.                 for (int i = 0; i < work.Length; i++)
  53.                 {
  54.                     work[i] = i;
  55.                 }
  56.                 foreach (int[] index in GetIntPermutations(work, 0, work.Length))
  57.                 {
  58.                     T[] result = new T[index.Length];
  59.                     for (int i = 0; i < index.Length; i++) result[i] = items[index[i]];
  60.                     yield return result;
  61.                 }
  62.             }
  63.  
  64.             public static IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len)
  65.             {
  66.                 if (len == 1)
  67.                 {
  68.                     yield return index;
  69.                 }
  70.                 else if (len == 2)
  71.                 {
  72.                     yield return index;
  73.                     Swap(index, offset, offset + 1);
  74.                     yield return index;
  75.                     Swap(index, offset, offset + 1);
  76.                 }
  77.                 else
  78.                 {
  79.                     foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
  80.                     {
  81.                         yield return result;
  82.                     }
  83.                     for (int i = 1; i < len; i++)
  84.                     {
  85.                         Swap(index, offset, offset + i);
  86.                         foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
  87.                         {
  88.                             yield return result;
  89.                         }
  90.                         Swap(index, offset, offset + i);
  91.                     }
  92.                 }
  93.             }
  94.  
  95.             private static void Swap(int[] index, int offset1, int offset2)
  96.             {
  97.                 int temp = index[offset1];
  98.                 index[offset1] = index[offset2];
  99.                 index[offset2] = temp;
  100.             }
  101.  
  102.         }
  103.     }
  104.     }
  105.     class Vijener
  106.     {
  107.         public enum Option
  108.         {
  109.             First,
  110.             Second
  111.         }
  112.  
  113.         private const string abc = "abcdefghijklmnopqrstuvwxyz";
  114.  
  115.         public string Encryption(string originalText, string secretKey, Option option)
  116.         {
  117.             StringBuilder result = new StringBuilder();
  118.  
  119.             var abcLength = abc.Length;
  120.             var lowerOriginalText = originalText.ToLower();
  121.             var lowerSecretKey = secretKey.ToLower();
  122.  
  123.             var newSecretKey = GenerateSecretKeyRelativeToInputString(lowerOriginalText, lowerSecretKey, option);
  124.  
  125.             Console.WriteLine("New secret key: {0}", newSecretKey);
  126.  
  127.             for (var i = 0; i < lowerOriginalText.Length; i++)
  128.             {
  129.                 if (lowerOriginalText[i] == ' ')
  130.                 {
  131.                     result.Append(lowerOriginalText[i]);
  132.                     continue;
  133.                 }
  134.  
  135.                 var p = GetIndexRelativeToABC(lowerOriginalText[i]);
  136.                 var k = GetIndexRelativeToABC(newSecretKey[i]);
  137.                 var charIndex = (p + k) % abcLength;
  138.                 result.Append(GetCharFromABCByIndex(charIndex));
  139.             }
  140.  
  141.             return result.ToString();
  142.         }
  143.  
  144.         public string Decryption(string encryptingText, string secretKey, Option option)
  145.         {
  146.             StringBuilder result = new StringBuilder();
  147.  
  148.             var abcLength = abc.Length;
  149.             var lowerEncryptingText = encryptingText.ToLower();
  150.             var lowerSecretKey = secretKey.ToLower();
  151.  
  152.             var newSecretKey = GenerateSecretKeyRelativeToInputString(lowerEncryptingText, lowerSecretKey, option);
  153.             for (var i = 0; i < lowerEncryptingText.Length; i++)
  154.             {
  155.                 if (lowerEncryptingText[i] == ' ')
  156.                 {
  157.                     result.Append(lowerEncryptingText[i]);
  158.                     continue;
  159.                 }
  160.  
  161.                 var c = GetIndexRelativeToABC(lowerEncryptingText[i]);
  162.                 var k = GetIndexRelativeToABC(newSecretKey[i]);
  163.                 var charIndex = (c - k + abcLength) % abcLength;
  164.                 result.Append(GetCharFromABCByIndex(charIndex));
  165.             }
  166.  
  167.             return result.ToString();
  168.         }
  169.  
  170.         private int GetIndexRelativeToABC(char inputChar)
  171.         {
  172.             return abc.IndexOf(inputChar);
  173.         }
  174.  
  175.         private char GetCharFromABCByIndex(int index)
  176.         {
  177.             if (index >= abc.Length) throw new ArgumentOutOfRangeException("index");
  178.             return abc[index];
  179.         }
  180.  
  181.         private string GenerateSecretKeyRelativeToInputString(string inputString, string secretKey, Option option)
  182.         {
  183.             StringBuilder result = new StringBuilder();
  184.  
  185.             var inputStringWithOutSpace = inputString.Replace(" ", "");
  186.  
  187.             for (var i = 0; i < inputString.Length; i++)
  188.             {
  189.                 switch (option)
  190.                 {
  191.                     case Option.First:
  192.                         var index = i >= secretKey.Length ? i % secretKey.Length : i;
  193.                         result.Append(secretKey[index]);
  194.                         break;
  195.                     case Option.Second:
  196.                         if (i >= secretKey.Length)
  197.                         {
  198.                             var idx = (i - secretKey.Length) % inputStringWithOutSpace.Length;
  199.                             result.Append(inputStringWithOutSpace[idx]);
  200.                         }
  201.                         else
  202.                         {
  203.                             result.Append(secretKey[i]);
  204.                         }
  205.                         break;
  206.                 }
  207.             }
  208.             return result.ToString();
  209.         }
  210.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement