Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace ConsoleApplication
- {
- class Program
- {
- public static int[] numbers;
- public static char[] chars;
- public static char minSymbol;
- public static char maxSymbol;
- //абвгдежзийклмнопрстуфхцчшщъыьэюя
- static void Main(string[] args)
- {
- Console.OutputEncoding = System.Text.Encoding.UTF8;
- Console.WriteLine("Введите текст для шифрования:");
- string text = Console.ReadLine();
- Console.WriteLine("Введите верхнюю границу алфавита:");
- minSymbol = Convert.ToChar(Console.ReadLine());
- Console.WriteLine("Введите нижнюю границу алфавита:");
- maxSymbol = Convert.ToChar(Console.ReadLine());
- Console.WriteLine("Введите слово-ключ");
- string keyWord = Console.ReadLine();
- // Создаем и инициализируем массив чисел и чаров относительно длины заданного алфавита
- numbers = new int[maxSymbol - minSymbol + 1];
- chars = new char[maxSymbol - minSymbol + 1];
- for (int i = 0; i <= maxSymbol - minSymbol; i++)
- {
- numbers[i] = i;
- chars[i] = (char)(numbers[i] + minSymbol);
- }
- Console.WriteLine(EncryptWithWord(text,keyWord));
- }
- public static char TranslateChar(char currentSymbol, int translation, char _minSymbol = 'а', char _maxSymbol = 'я')
- {
- int value = Convert.ToInt32(currentSymbol);
- int min = Convert.ToInt32(_minSymbol);
- int max = Convert.ToInt32(_maxSymbol);
- value = value + translation;
- if (value < min)
- {
- value = max - (min - value - 1);
- }
- else if (value > max)
- {
- value = min + (value - max - 1);
- }
- return Convert.ToChar(value);
- }
- // Метод для конвертации текста с помощью слова-ключа
- public static string EncryptWithWord(string _text, string _keyWord)
- {
- StringBuilder encrypted = new StringBuilder();
- for (int i = 0, j = 0; i < _text.Length; i++, j++)
- {
- if (j >= _keyWord.Length) j = 0;
- encrypted.Append(TranslateChar(_text[i], SecretWordConvert(_keyWord[j]),minSymbol,maxSymbol));
- }
- return "Зашифрованное слово: " + Convert.ToString(encrypted);
- }
- // Метод для конвертации символов из слова-ключа
- public static int SecretWordConvert(char charFromKeyWord)
- {
- for (int i = 0; i <= chars.Length; i++)
- if (charFromKeyWord == chars[i])
- return numbers[i+1];
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement