using System;
public class CaesarCipher
{
//символы украинской азбуки
const string alfabet = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя";
private string CodeEncode(string text, int k)
{
//добавляем в алфавит большие буквы
var fullAlfabet = alfabet + alfabet.ToUpper();
var letterQty = fullAlfabet.Length;
var retVal = "";
for (int i = 0; i < text.Length; i++)
{
var c = text[i];
var index = fullAlfabet.IndexOf(c);
if (index < 0)
{
//если символ не найден, то добавляем его в неизменном виде
retVal += c.ToString();
}
else
{
var codeIndex = (letterQty + index + k) % letterQty;
retVal += fullAlfabet[codeIndex];
}
}
return retVal;
}
//шифрование текста
public string Encrypt(string plainMessage, int key)
=> CodeEncode(plainMessage, key);
//дешифрование текста
public string Decrypt(string encryptedMessage, int key)
=> CodeEncode(encryptedMessage, -key);
}
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("Введите текст: ");
var message = Console.ReadLine();
Console.Write("Введите ключ: ");
var secretKey = Convert.ToInt32(Console.ReadLine());
var encryptedText = cipher.Encrypt(message, secretKey);
Console.WriteLine("Зашифрованное сообщение: {0}", encryptedText);
Console.WriteLine("Расшифрованное сообщение: {0}", cipher.Decrypt(encryptedText, secretKey));
Console.ReadLine();
}
}