Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- string qwerty = "qwertyuiopasdfghjklzxcvbnm";
- qwerty += qwerty.ToUpper();
- char[] alphabet = qwerty.ToCharArray();
- Array.Sort(alphabet);
- Console.WriteLine("1 - зашифровать строку\n2 - дешифровать строку\n0 - выйти из программы");
- string choice = Console.ReadLine();
- while (choice != "0")
- {
- switch (choice)
- {
- case "1": Encrypt(alphabet); break;
- case "2": Decrypt(alphabet); break;
- default: Console.WriteLine("Неизвестная команда"); break;
- }
- Console.WriteLine("Что вы хотите сделать?");
- choice = Console.ReadLine();
- }
- }
- static void Encrypt(char[] alphabet)
- {
- Console.WriteLine("Введите слово:");
- string word = Console.ReadLine();
- Console.WriteLine("Введите ключ:");
- string key = Console.ReadLine();
- string result = "";
- string keyWord = "";
- int counter = 0;
- foreach (char letter in word)
- {
- if (letter != ' ')
- {
- if (counter == key.Length)
- counter = 0;
- keyWord += key[counter];
- counter++;
- }
- else keyWord += ' ';
- }
- for (int letter = 0; letter < word.Length; letter++)
- {
- if (word[letter] != ' ')
- {
- int wordIndex = Array.IndexOf(alphabet, word[letter]);
- int keyIndex = Array.IndexOf(alphabet, keyWord[letter]);
- int encIndex = (wordIndex + keyIndex) % alphabet.Length;
- result += alphabet[encIndex];
- }
- else result += ' ';
- }
- Console.WriteLine(result);
- }
- static void Decrypt(char[] alphabet)
- {
- Console.WriteLine("Введите слово:");
- string word = Console.ReadLine();
- Console.WriteLine("Введите ключ:");
- string key = Console.ReadLine();
- string result = "";
- string keyWord = "";
- int counter = 0;
- foreach (char letter in word)
- {
- if (letter != ' ')
- {
- if (counter == key.Length)
- counter = 0;
- keyWord += key[counter];
- counter++;
- }
- else keyWord += ' ';
- }
- for (int letter = 0; letter < word.Length; letter++)
- {
- if (word[letter] != ' ')
- {
- int wordIndex = Array.IndexOf(alphabet, word[letter]);
- int keyIndex = Array.IndexOf(alphabet, keyWord[letter]);
- int encIndex = (wordIndex - keyIndex + alphabet.Length) % alphabet.Length;
- result += alphabet[encIndex];
- }
- else result += ' ';
- }
- Console.WriteLine(result);
- }
- }
- }
Add Comment
Please, Sign In to add comment