Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace scumbler
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool isContinue = true;
- string userInput;
- while(isContinue)
- {
- Write("Нажмите на любую клавишу...");
- Console.ReadKey();
- Console.Clear();
- DrawHeadMenu("HeadText.txt");
- Write("Меню:\n"+
- "$help\n"+
- "$encrypt\n"+
- "$decipher\n"+
- "$exit\n", ConsoleColor.Green);
- userInput = Console.ReadLine();
- switch(userInput)
- {
- case "$help":
- Write("Пояснения: \n$help - помощ в программе\n$encrypt - зашифрует текст с генерированным алфовитом и сдвигом\n$decipher - расшифрует текст с введенным алфовитом и сдвигом\n#exit - выход из программы");
- break;
- case "$encrypt":
- EncyptText();
- Console.ReadKey();
- break;
- case "$decipher":
- DecipherText(userInput);
- break;
- case "$exit":
- Write("Программа завершена\n", ConsoleColor.Red);
- isContinue = false;
- break;
- default:
- Write("Вводи команду в точности как показано!\n",ConsoleColor.Red);
- break;
- }
- }
- }
- private static void DrawHeadMenu(string fileName)
- {
- string[] newFile = File.ReadAllLines($"{fileName}");
- char[,] text = new char[newFile.Length, newFile[0].Length];
- for(int i = 0; i < text.GetLength(0) - 1; i++)
- {
- for(int j = 0; j < text.GetLength(1); j++)
- {
- text[i,j] = newFile[i][j];
- string simbol = Convert.ToString(text[i,j]);
- Write($"{simbol}", ConsoleColor.Magenta);
- }
- Console.WriteLine();
- }
- }
- private static void EncyptText(){
- char[] alphabet = ReadAlphabet("alphabet.txt");
- alphabet = ShuffleAlphabet(alphabet);
- Write("ключ для шифровки, сохраните его!:\n");
- foreach (char alpha in alphabet)
- {
- Write($"{alpha}", ConsoleColor.Red);
- }
- Write("\nВведите сдвиг шифра: ");
- int shift = Convert.ToInt32(Console.ReadLine());
- Write("\nВведите текст, который надо зашифровать: ", ConsoleColor.Magenta);
- string userText = Console.ReadLine();
- string compleateEncryptedText = "";
- bool isFind = false;
- for(int i = 0; i < userText.Length; i++)
- {
- for(int j = 0; j < alphabet.Length / 2; j++)
- {
- if(alphabet[j] == userText[i])
- {
- compleateEncryptedText += alphabet[j+shift];
- isFind = false;
- break;
- }else if(userText[i] == ' ')
- {
- compleateEncryptedText += " ";
- isFind = false;
- break;
- }else
- {
- isFind = true;
- }
- }
- if (isFind)
- {
- compleateEncryptedText += userText[i];
- }
- }
- Write($"Готовый текст: {compleateEncryptedText}");
- }
- private static void DecipherText(string userInput)
- {
- Write("в файле key.txt вставьте нужнный ключ для расшифровки\n", ConsoleColor.Green);
- char[] key = ReadAlphabet("key.txt");
- Write("Сейчас ключ: ");
- foreach (char item in key)
- {
- Write($"{item}");
- }
- Write("\nВведите текст который нужно расшифровать: ", ConsoleColor.Magenta);
- string encryptedText = Console.ReadLine();
- Write("\nВведите свдиг шифра: ");
- int shiftIndex = Convert.ToInt32(Console.ReadLine());
- string compleateDecipherText = "";
- bool isFind = false;
- for(int i = 0; i < encryptedText.Length; i++)
- {
- for(int j = key.Length / 2; j < key.Length; j++)
- {
- if(encryptedText[i] == key[j])
- {
- compleateDecipherText += key[j - shiftIndex];
- isFind = false;
- break;
- }
- else if(encryptedText[i] == ' ')
- {
- isFind = false;
- compleateDecipherText += " ";
- break;
- }else
- {
- isFind = true;
- }
- }
- if(isFind)
- {
- compleateDecipherText += encryptedText[i];
- }
- }
- Write($"{compleateDecipherText}\n");
- }
- private static char[] ReadAlphabet(string fileName)
- {
- string[] alphabetFile = File.ReadAllLines($"{fileName}");
- char[] alphabet = new char[alphabetFile[0].Length];
- for(int j = 0; j < alphabet.Length;j++)
- {
- alphabet[j] = alphabetFile[0][j];
- }
- return alphabet;
- }
- private static char[] ShuffleAlphabet(char[] alphabet)
- {
- Random rand = new Random();
- char[] alphabetShuffled = new char[alphabet.Length];
- for (int i = 0; i < alphabet.Length; i++)
- {
- int randomIndex = rand.Next(0,alphabet.Length);
- int randomIndex2 = rand.Next(0,alphabet.Length);
- char temp = alphabet[randomIndex];
- alphabet[randomIndex] = alphabet[randomIndex2];
- alphabet[randomIndex2] = temp;
- }
- alphabetShuffled = alphabet;
- char[] tempArray = new char[alphabetShuffled.Length * 2];
- for(int j = 0; j < alphabetShuffled.Length; j++)
- {
- tempArray[j] = alphabetShuffled[j];
- }
- for(int k = tempArray.Length / 2; k < tempArray.Length;k++)
- {
- tempArray[k] = alphabetShuffled[k - tempArray.Length / 2];
- }
- alphabetShuffled = tempArray;
- return alphabetShuffled;
- }
- private static void Write(string text, ConsoleColor color = ConsoleColor.Yellow)
- {
- Console.ForegroundColor = color;
- Console.Write(text);
- Console.ResetColor();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment