Anonim_999

scumbler

Mar 17th, 2021
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 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.                 "$help\n"+
  22.                 "$encrypt\n"+
  23.                 "$decipher\n"+
  24.                 "$exit\n", ConsoleColor.Green);
  25.  
  26.                 userInput = Console.ReadLine();
  27.  
  28.                 switch(userInput)
  29.                 {
  30.                     case "$help":
  31.                         Write("Пояснения: \n$help - помощ в программе\n$encrypt - зашифрует текст с генерированным алфовитом и сдвигом\n$decipher - расшифрует текст с введенным алфовитом и сдвигом\n#exit - выход из программы");    
  32.                     break;
  33.                     case "$encrypt":
  34.                         EncyptText();
  35.                         Console.ReadKey();
  36.                     break;
  37.                     case "$decipher":
  38.                         DecipherText(userInput);
  39.                     break;
  40.                     case "$exit":
  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) - 1; i++)
  57.             {
  58.  
  59.                 for(int j = 0; j < text.GetLength(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.             char[] alphabet = ReadAlphabet("alphabet.txt");
  72.             alphabet = ShuffleAlphabet(alphabet);
  73.             Write("ключ для шифровки, сохраните его!:\n");
  74.  
  75.             foreach (char alpha in alphabet)
  76.             {
  77.                 Write($"{alpha}", ConsoleColor.Red);
  78.             }
  79.  
  80.             Write("\nВведите сдвиг шифра: ");
  81.             int shift = Convert.ToInt32(Console.ReadLine());
  82.             Write("\nВведите текст, который надо зашифровать: ", ConsoleColor.Magenta);
  83.             string userText = Console.ReadLine();
  84.             string compleateEncryptedText = "";
  85.             bool isFind = false;
  86.  
  87.             for(int i = 0; i < userText.Length; i++)
  88.             {
  89.  
  90.                 for(int j = 0; j < alphabet.Length / 2; j++)
  91.                 {
  92.  
  93.                     if(alphabet[j] == userText[i])
  94.                     {
  95.                         compleateEncryptedText += alphabet[j+shift];
  96.                         isFind = false;
  97.                         break;
  98.                     }else if(userText[i] == ' ')
  99.                     {
  100.                         compleateEncryptedText += " ";
  101.                         isFind = false;
  102.                         break;
  103.                     }else
  104.                     {
  105.                         isFind = true;
  106.                     }
  107.                 }
  108.                 if (isFind)
  109.                 {
  110.                     compleateEncryptedText += userText[i];
  111.                 }
  112.             }
  113.             Write($"Готовый текст: {compleateEncryptedText}");
  114.  
  115.         }
  116.  
  117.         private static void DecipherText(string userInput)
  118.         {
  119.             Write("в файле key.txt вставьте нужнный ключ для расшифровки\n", ConsoleColor.Green);
  120.             char[] key = ReadAlphabet("key.txt");
  121.             Write("Сейчас ключ: ");
  122.            
  123.             foreach (char item in key)
  124.             {
  125.                 Write($"{item}");    
  126.             }
  127.  
  128.             Write("\nВведите текст который нужно расшифровать: ", ConsoleColor.Magenta);
  129.             string encryptedText = Console.ReadLine();
  130.             Write("\nВведите свдиг шифра: ");
  131.  
  132.             int shiftIndex = Convert.ToInt32(Console.ReadLine());
  133.             string compleateDecipherText = "";
  134.             bool isFind = false;
  135.  
  136.             for(int i = 0; i < encryptedText.Length; i++)
  137.             {
  138.                 for(int j = key.Length / 2; j < key.Length; j++)
  139.                 {
  140.                     if(encryptedText[i] == key[j])
  141.                     {
  142.                         compleateDecipherText += key[j - shiftIndex];
  143.                         isFind = false;
  144.                         break;
  145.                     }
  146.                     else if(encryptedText[i] == ' ')
  147.                     {
  148.                         isFind = false;
  149.                         compleateDecipherText += " ";
  150.                         break;
  151.                     }else
  152.                     {
  153.                         isFind = true;
  154.                     }
  155.                 }
  156.                 if(isFind)
  157.                 {
  158.                     compleateDecipherText += encryptedText[i];
  159.                 }
  160.             }
  161.             Write($"{compleateDecipherText}\n");
  162.         }
  163.  
  164.         private static char[] ReadAlphabet(string fileName)
  165.         {
  166.             string[] alphabetFile = File.ReadAllLines($"{fileName}");
  167.             char[] alphabet = new char[alphabetFile[0].Length];
  168.            
  169.             for(int j = 0; j < alphabet.Length;j++)
  170.             {
  171.                 alphabet[j] = alphabetFile[0][j];
  172.             }
  173.            
  174.             return alphabet;
  175.         }
  176.         private static char[] ShuffleAlphabet(char[] alphabet)
  177.         {
  178.             Random rand = new Random();
  179.            
  180.             char[] alphabetShuffled = new char[alphabet.Length];
  181.             for (int i = 0; i < alphabet.Length; i++)
  182.             {
  183.                 int randomIndex = rand.Next(0,alphabet.Length);
  184.                 int randomIndex2 = rand.Next(0,alphabet.Length);
  185.                 char temp = alphabet[randomIndex];
  186.                 alphabet[randomIndex] = alphabet[randomIndex2];
  187.                 alphabet[randomIndex2] = temp;
  188.             }
  189.             alphabetShuffled = alphabet;
  190.             char[] tempArray = new char[alphabetShuffled.Length * 2];
  191.  
  192.             for(int j = 0; j < alphabetShuffled.Length; j++)
  193.             {
  194.                 tempArray[j] = alphabetShuffled[j];
  195.             }
  196.  
  197.             for(int k = tempArray.Length / 2; k < tempArray.Length;k++)
  198.             {
  199.                 tempArray[k] = alphabetShuffled[k - tempArray.Length / 2];
  200.             }
  201.             alphabetShuffled = tempArray;
  202.             return alphabetShuffled;
  203.         }
  204.  
  205.         private static void Write(string text, ConsoleColor color = ConsoleColor.Yellow)
  206.         {
  207.             Console.ForegroundColor = color;
  208.             Console.Write(text);
  209.             Console.ResetColor();
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment