public static void Main()
int variant = 0, method = 0;
Console.WriteLine("****Виженер****");
Console.WriteLine("1. Зашифрование текста \n2. Расшифрование текста");
variant = Int32.Parse(Console.ReadLine());
Console.WriteLine("Выберите пункт:");
if (variant == 1 || variant == 2)
Console.WriteLine("1. Шифр Виженера \n2. Шифр Виженера с самоключом");
method = Int32.Parse(Console.ReadLine());
Console.WriteLine("Выберите пункт:");
if (method == 1 || method == 2)
while (gamma.Length <= 1)
gamma = Console.ReadLine();
Console.WriteLine("Введите ключ:");
Console.Write("Введите текст для шифрования: ");
text = Console.ReadLine();
Console.WriteLine(crypt(text, gamma, 1));
Console.Write("Введите текст для шифрования: ");
text = Console.ReadLine();
string temp = text.Insert(0, "р" );
gamma = temp.Remove(temp.Length - 1);
Console.WriteLine("Ключ:");
Console.WriteLine(gamma);
Console.WriteLine(crypt(text, gamma, 1));
Console.Write("Введите текст для расшифрования: ");
text = Console.ReadLine();
Console.WriteLine(decrypt(text, gamma, 1));
Console.Write("Введите текст для расшифрования: ");
text = Console.ReadLine();
Console.WriteLine(decrypt(text, gamma, 1));
Console.WriteLine("...");
if (Console.ReadLine() == "нет")
static char find_char(int index)
char[] alphabet = {'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н',
'о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я','.',',',' ','?'};
static int find_index(char leter)
char[] alphabet = {'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н',
'о','п','р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я','.',',',' ','?'};
for (index = 0; index < 37; ++index)
if (alphabet[index] == leter)
static char[] crypt(string text, string gamma, int method)
char[] cypher_text = new char[text.Length];
cypher_text[i] = find_char((find_index(text[i]) + find_index(gamma[j])) % 37);
cypher_text[0] = find_char((find_index(text[0]) + find_index(gamma[0])) % 37); ++i;
cypher_text[i] = find_char((find_index(text[i]) + find_index(cypher_text[i - 1])) % 37);
static char[] decrypt(string text, string gamma, int method)
char[] decrypted_text = new char[text.Length];
tmp = find_index(text[i]) - find_index(gamma[j]);
decrypted_text[i] = find_char(tmp % 37);
tmp = find_index(text[0]) - find_index(gamma[0]);
decrypted_text[0] = find_char(tmp % 37); i = 1;
tmp = find_index(text[i]) - find_index(text[i - 1]);
decrypted_text[i] = find_char(tmp % 37);