Advertisement
Anonim_999

Srumbler2

May 12th, 2021
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.94 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. namespace scumbler
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             bool isContinue = true;
  10.             string userInput;
  11.  
  12.             while (isContinue)
  13.             {
  14.                 Write("Нажмите на любую клавишу...");
  15.  
  16.                 Console.ReadKey();
  17.                 Console.Clear();
  18.  
  19.                 DrawHeadMenu("HeadText.txt");
  20.                 Write("Меню:\n" +
  21.                 "Помощь\n" +
  22.                 "Зашифровать\n" +
  23.                 "Расшифровать\n" +
  24.                 "Выход\n", ConsoleColor.Green);
  25.  
  26.                 userInput = Console.ReadLine();
  27.  
  28.                 switch (userInput)
  29.                 {
  30.                     case "Помощь":
  31.                         Write("Пояснения: \nПомощь - помощ в программе\nЗашифровать - зашифрует текст с генерированным алфовитом и сдвигом\nРасшифровать - расшифрует текст с введенным алфовитом и сдвигом\nВыход - выход из программы");
  32.                         break;
  33.                     case "Зашифровать":
  34.                         EncyptText();
  35.                         Console.ReadKey();
  36.                         break;
  37.                     case "Расшифровать":
  38.                         DecipherText(userInput);
  39.                         break;
  40.                     case "Выход":
  41.                         Write("Программа завершена\n", ConsoleColor.Red);
  42.                         isContinue = false;
  43.                         break;
  44.                     default:
  45.                         Write("Вводи команду в точности как показано!\n", ConsoleColor.Red);
  46.                         break;
  47.                 }
  48.             }
  49.         }
  50.  
  51.         private static void DrawHeadMenu(string fileName)
  52.         {
  53.             string[] newFile = File.ReadAllLines($"{fileName}");
  54.             char[,] text = new char[newFile.Length, newFile[0].Length];
  55.  
  56.             for (int i = 0; i < text.GetLength(0); i++)
  57.             {
  58.  
  59.                 for (int j = 0; j < text.GetLength(1)-1; j++)
  60.                 {
  61.                     text[i, j] = newFile[i][j];
  62.                     string simbol = Convert.ToString(text[i, j]);
  63.  
  64.                     Write($"{simbol}", ConsoleColor.Magenta);
  65.                 }
  66.                 Console.WriteLine();
  67.             }
  68.         }
  69.  
  70.         private static void EncyptText()
  71.         {
  72.             char[] alphabet = ReadAlphabet("alphabet.txt");
  73.             alphabet = ShuffleAlphabet(alphabet);
  74.             Write("ключ для шифровки, сохраните его!:\n");
  75.  
  76.             foreach (char alpha in alphabet)
  77.             {
  78.                 Write($"{alpha}", ConsoleColor.Red);
  79.             }
  80.  
  81.             Write("\nВведите сдвиг шифра: ");
  82.             int shift = Convert.ToInt32(Console.ReadLine());
  83.             Write("\nВставь текст в файл encrypt.txt, который надо зашифровать, потом нажми любую клавишу", ConsoleColor.Magenta);
  84.             Console.ReadKey();
  85.             string userText = File.ReadAllText("encrypt.txt");
  86.             string compleateEncryptedText = "";
  87.             bool isFind = false;
  88.  
  89.             for (int i = 0; i < userText.Length; i++)
  90.             {
  91.  
  92.                 for (int j = 0; j < alphabet.Length / 2; j++)
  93.                 {
  94.  
  95.                     if (alphabet[j] == userText[i])
  96.                     {
  97.                         compleateEncryptedText += alphabet[j + shift];
  98.                         isFind = false;
  99.                         break;
  100.                     }
  101.                     else if (userText[i] == ' ')
  102.                     {
  103.                         compleateEncryptedText += " ";
  104.                         isFind = false;
  105.                         break;
  106.                     }
  107.                     else
  108.                     {
  109.                         isFind = true;
  110.                     }
  111.                 }
  112.                 if (isFind)
  113.                 {
  114.                     compleateEncryptedText += userText[i];
  115.                 }
  116.             }
  117.             Write($"Готовый текст: {compleateEncryptedText}");
  118.  
  119.         }
  120.  
  121.         private static void DecipherText(string userInput)
  122.         {
  123.             Write("в файле key.txt вставьте нужнный ключ для расшифровки\n", ConsoleColor.Green);
  124.             char[] key = ReadAlphabet("key.txt");
  125.             Write("Сейчас ключ: ");
  126.  
  127.             foreach (char item in key)
  128.             {
  129.                 Write($"{item}");
  130.             }
  131.  
  132.             Write("\nВставь текст в файл: DecipherText.txt, чтобы расшифровать этот текст, потом нажми любую клавишу");
  133.             Console.ReadKey();
  134.             string encryptedText = File.ReadAllText("DecipherText.txt");
  135.             Write("\nВведите свдиг шифра: ");
  136.  
  137.             int shiftIndex = Convert.ToInt32(Console.ReadLine());
  138.             string compleateDecipherText = "";
  139.             bool isFind = false;
  140.  
  141.             for (int i = 0; i < encryptedText.Length; i++)
  142.             {
  143.                 for (int j = key.Length / 2; j < key.Length; j++)
  144.                 {
  145.                     if (encryptedText[i] == key[j])
  146.                     {
  147.                         compleateDecipherText += key[j - shiftIndex];
  148.                         isFind = false;
  149.                         break;
  150.                     }
  151.                     else if (encryptedText[i] == ' ')
  152.                     {
  153.                         isFind = false;
  154.                         compleateDecipherText += " ";
  155.                         break;
  156.                     }
  157.                     else
  158.                     {
  159.                         isFind = true;
  160.                     }
  161.                 }
  162.                 if (isFind)
  163.                 {
  164.                     compleateDecipherText += encryptedText[i];
  165.                 }
  166.             }
  167.             Write($"{compleateDecipherText}\n");
  168.         }
  169.  
  170.         private static char[] ReadAlphabet(string fileName)
  171.         {
  172.             string[] alphabetFile = File.ReadAllLines($"{fileName}");
  173.             char[] alphabet = new char[alphabetFile[0].Length];
  174.  
  175.             for (int j = 0; j < alphabet.Length; j++)
  176.             {
  177.                 alphabet[j] = alphabetFile[0][j];
  178.             }
  179.  
  180.             return alphabet;
  181.         }
  182.         private static char[] ShuffleAlphabet(char[] alphabet)
  183.         {
  184.             Random rand = new Random();
  185.  
  186.             char[] alphabetShuffled = new char[alphabet.Length];
  187.             for (int i = 0; i < alphabet.Length; i++)
  188.             {
  189.                 int randomIndex = rand.Next(0, alphabet.Length);
  190.                 int randomIndex2 = rand.Next(0, alphabet.Length);
  191.                 char temp = alphabet[randomIndex];
  192.                 alphabet[randomIndex] = alphabet[randomIndex2];
  193.                 alphabet[randomIndex2] = temp;
  194.             }
  195.             alphabetShuffled = alphabet;
  196.             char[] tempArray = new char[alphabetShuffled.Length * 2];
  197.  
  198.             for (int j = 0; j < alphabetShuffled.Length; j++)
  199.             {
  200.                 tempArray[j] = alphabetShuffled[j];
  201.             }
  202.  
  203.             for (int k = tempArray.Length / 2; k < tempArray.Length; k++)
  204.             {
  205.                 tempArray[k] = alphabetShuffled[k - tempArray.Length / 2];
  206.             }
  207.             alphabetShuffled = tempArray;
  208.             return alphabetShuffled;
  209.         }
  210.  
  211.         private static void Write(string text, ConsoleColor color = ConsoleColor.Yellow)
  212.         {
  213.             Console.ForegroundColor = color;
  214.             Console.Write(text);
  215.             Console.ResetColor();
  216.         }
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement