using System; public class CaesarCipher { //символы украинской азбуки const string alfabet = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя"; private string CodeEncode(string text, int a, int b) { var letterQty = alfabet.Length; var EnText = ""; for (int i = 0; i < text.Length; i++) { var c = text[i]; var index = alfabet.IndexOf(c); if (index < 0) { //если символ не найден, то добавляем его в неизменном виде EnText += c.ToString(); } else { var codeIndex = (index * b + a) % letterQty; // формула шифрования EnText += alfabet[codeIndex]; } } return EnText; } public int modInverse(int k, int n) // инверс { for (var i = 1; i < n; i++) { if((k * i) % n == 1) { return i; } } return 1; } private string CodeDecode(string text, int a, int b) { var letterQty = alfabet.Length; var DeText = ""; var inv = this.modInverse(b, letterQty); for (int i = 0; i < text.Length; i++) { var c = text[i]; var index = alfabet.IndexOf(c); if (index < 0) { //если символ не найден, то добавляем его в неизменном виде DeText += c.ToString(); } else { var codeIndex = (inv * (index + letterQty - a)) % letterQty; // формула расшифровки DeText += alfabet[codeIndex]; } } return DeText; } private string FullShift(string text, int a, int b) // полный перебор { var letterQty = alfabet.Length; string result = ""; for (int i = 0; i < text.Length; i++) { var inv = this.modInverse(b, letterQty); var c = text[i]; var index = alfabet.IndexOf(c); if (index < 0) { //если символ не найден, то добавляем его в неизменном виде result += c.ToString(); } else { var codeIndex = (inv * (index + letterQty - a)) % letterQty; result += alfabet[codeIndex]; } } return result; } //шифрование текста public string Encrypt(string plainMessage, int keyA, int keyB) => CodeEncode(plainMessage, keyA, keyB); //дешифрование текста public string Decrypt(string encryptedMessage, int keyA, int keyB) => CodeDecode(encryptedMessage, keyA, keyB); //Перебор сдвигов public string Shift(string encryptedMessage, int keyA, int keyB) => FullShift(encryptedMessage, keyA, keyB); } class Program { static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.Unicode;//вывод на укр языке Console.InputEncoding = System.Text.Encoding.Unicode;//ввод на укр языке var cipher = new CaesarCipher(); Console.Write("Введите текст: "); string message = "У Василька була синичка. Василь вирвав її з кігтів шуліки. Вона вже звикла до хати. Василь вирубав для неї гілку і причепив до стіни. Там синичка і лазила. Тепло в хаті синичці. А надворі завірюха гуде. Снігу намело багато. А іншим пташкам тяжко. Бур яни снігом замело. Голодують пташки. І вирішив Василько з другом Андрійком зробити для пташок годівниці. Прилітайте, пташки!"/*у василька була синичка. василь вирвав її з кігтів шуліки.*/; //Console.ReadLine(); Console.WriteLine(message); Console.WriteLine("Ключем А моет быть одно из этих чисел: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25"); Console.Write("Введите ключ A: "); int A = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Ключем B могут быть числа от 0 до 25"); Console.Write("Введите ключ B: "); int B = Convert.ToInt32(Console.ReadLine()); string encryptedText = cipher.Encrypt(message, A, B); Console.WriteLine("Зашифрованное сообщение: {0}", encryptedText); Console.WriteLine("Расшифрованное сообщение: {0}", cipher.Decrypt(encryptedText, A, B)); for (int a = 1; a < 26; a += 2) { int b =0; while (b < 26) { b++; Console.WriteLine($"\nПеребор сообщения: {cipher.Shift(encryptedText, a, b)} - Ключи A: {a} и B: {b}"); } } } }