Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace vij_brut
- {
- class Program
- {
- static void Main(string[] args)
- {
- Vijener a = new Vijener();
- int counter = 1;
- Console.Write("Введите кодовое слово: ");
- string key = Console.ReadLine();
- Console.Write("Введите имя файла и, если нужно, путь до него: ");
- StreamReader rdr = new StreamReader(Console.ReadLine());
- Console.WriteLine("1 - считать в строку, 2 - считать в массив для перебора");
- string option = Console.ReadLine();
- bool ok = true;
- switch (option)
- {
- case "1":
- string text = rdr.ReadLine();
- Console.WriteLine("Decoded text: {0}", a.Decryption(text, key, Vijener.Option.First));
- break;
- case "2":
- Console.Write("Введите число элементов: ");
- int num = Convert.ToInt32(Console.ReadLine());
- string[] items = new string[num];
- items = rdr.ReadLine().Split(new Char[] { ' ', ',', '.', ':', '\t' });
- StreamWriter wdr = new StreamWriter("out.txt");
- foreach (string[] permutation in Permutation.GetPermutations<string>(items))
- {
- string i = String.Join("", permutation);
- var dec_i = a.Decryption(i, key, Vijener.Option.First);
- Console.WriteLine("{0}. Combination {1} and decoded is {2}", counter, i, dec_i);
- wdr.WriteLine("{0}. Combination {1} and decoded is {2}", counter, i, dec_i);
- counter++;
- }
- break;
- default: Console.WriteLine("Try again"); ok = false; break;
- }
- Console.ReadKey();
- }
- public class Permutation
- {
- public static IEnumerable<T[]> GetPermutations<T>(T[] items)
- {
- int[] work = new int[items.Length];
- for (int i = 0; i < work.Length; i++)
- {
- work[i] = i;
- }
- foreach (int[] index in GetIntPermutations(work, 0, work.Length))
- {
- T[] result = new T[index.Length];
- for (int i = 0; i < index.Length; i++) result[i] = items[index[i]];
- yield return result;
- }
- }
- public static IEnumerable<int[]> GetIntPermutations(int[] index, int offset, int len)
- {
- if (len == 1)
- {
- yield return index;
- }
- else if (len == 2)
- {
- yield return index;
- Swap(index, offset, offset + 1);
- yield return index;
- Swap(index, offset, offset + 1);
- }
- else
- {
- foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
- {
- yield return result;
- }
- for (int i = 1; i < len; i++)
- {
- Swap(index, offset, offset + i);
- foreach (int[] result in GetIntPermutations(index, offset + 1, len - 1))
- {
- yield return result;
- }
- Swap(index, offset, offset + i);
- }
- }
- }
- private static void Swap(int[] index, int offset1, int offset2)
- {
- int temp = index[offset1];
- index[offset1] = index[offset2];
- index[offset2] = temp;
- }
- }
- }
- }
- class Vijener
- {
- public enum Option
- {
- First,
- Second
- }
- private const string abc = "abcdefghijklmnopqrstuvwxyz";
- public string Encryption(string originalText, string secretKey, Option option)
- {
- StringBuilder result = new StringBuilder();
- var abcLength = abc.Length;
- var lowerOriginalText = originalText.ToLower();
- var lowerSecretKey = secretKey.ToLower();
- var newSecretKey = GenerateSecretKeyRelativeToInputString(lowerOriginalText, lowerSecretKey, option);
- Console.WriteLine("New secret key: {0}", newSecretKey);
- for (var i = 0; i < lowerOriginalText.Length; i++)
- {
- if (lowerOriginalText[i] == ' ')
- {
- result.Append(lowerOriginalText[i]);
- continue;
- }
- var p = GetIndexRelativeToABC(lowerOriginalText[i]);
- var k = GetIndexRelativeToABC(newSecretKey[i]);
- var charIndex = (p + k) % abcLength;
- result.Append(GetCharFromABCByIndex(charIndex));
- }
- return result.ToString();
- }
- public string Decryption(string encryptingText, string secretKey, Option option)
- {
- StringBuilder result = new StringBuilder();
- var abcLength = abc.Length;
- var lowerEncryptingText = encryptingText.ToLower();
- var lowerSecretKey = secretKey.ToLower();
- var newSecretKey = GenerateSecretKeyRelativeToInputString(lowerEncryptingText, lowerSecretKey, option);
- for (var i = 0; i < lowerEncryptingText.Length; i++)
- {
- if (lowerEncryptingText[i] == ' ')
- {
- result.Append(lowerEncryptingText[i]);
- continue;
- }
- var c = GetIndexRelativeToABC(lowerEncryptingText[i]);
- var k = GetIndexRelativeToABC(newSecretKey[i]);
- var charIndex = (c - k + abcLength) % abcLength;
- result.Append(GetCharFromABCByIndex(charIndex));
- }
- return result.ToString();
- }
- private int GetIndexRelativeToABC(char inputChar)
- {
- return abc.IndexOf(inputChar);
- }
- private char GetCharFromABCByIndex(int index)
- {
- if (index >= abc.Length) throw new ArgumentOutOfRangeException("index");
- return abc[index];
- }
- private string GenerateSecretKeyRelativeToInputString(string inputString, string secretKey, Option option)
- {
- StringBuilder result = new StringBuilder();
- var inputStringWithOutSpace = inputString.Replace(" ", "");
- for (var i = 0; i < inputString.Length; i++)
- {
- switch (option)
- {
- case Option.First:
- var index = i >= secretKey.Length ? i % secretKey.Length : i;
- result.Append(secretKey[index]);
- break;
- case Option.Second:
- if (i >= secretKey.Length)
- {
- var idx = (i - secretKey.Length) % inputStringWithOutSpace.Length;
- result.Append(inputStringWithOutSpace[idx]);
- }
- else
- {
- result.Append(secretKey[i]);
- }
- break;
- }
- }
- return result.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement