Advertisement
Tellexxii

EncryptedWithWord_update

Jul 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4.  
  5. namespace ConsoleApplication
  6. {
  7.     class Program
  8.     {
  9.         public static int[] numbers;
  10.         public static char[] chars;
  11.         public static char minSymbol;
  12.         public static char maxSymbol;
  13.         //абвгдежзийклмнопрстуфхцчшщъыьэюя
  14.         static void Main(string[] args)
  15.         {
  16.            
  17.             Console.OutputEncoding = System.Text.Encoding.UTF8;
  18.             Console.WriteLine("Введите текст для шифрования:");
  19.             string text = Console.ReadLine();
  20.             Console.WriteLine("Введите верхнюю границу алфавита:");
  21.             minSymbol = Convert.ToChar(Console.ReadLine());
  22.             Console.WriteLine("Введите нижнюю границу алфавита:");
  23.             maxSymbol = Convert.ToChar(Console.ReadLine());
  24.             Console.WriteLine("Введите слово-ключ");
  25.             string keyWord = Console.ReadLine();
  26.            
  27.             // Создаем и инициализируем массив чисел и чаров относительно длины заданного алфавита
  28.             numbers = new int[maxSymbol - minSymbol + 1];
  29.             chars = new char[maxSymbol - minSymbol + 1];
  30.             for (int i = 0; i <= maxSymbol - minSymbol; i++)
  31.             {
  32.                 numbers[i] = i;
  33.                 chars[i] = (char)(numbers[i] + minSymbol);
  34.             }
  35.  
  36.             Console.WriteLine(EncryptWithWord(text,keyWord));
  37.            
  38.         }
  39.  
  40.         public static char TranslateChar(char currentSymbol, int translation, char _minSymbol = 'а', char _maxSymbol = 'я')
  41.         {
  42.             int value = Convert.ToInt32(currentSymbol);
  43.             int min = Convert.ToInt32(_minSymbol);
  44.             int max = Convert.ToInt32(_maxSymbol);
  45.  
  46.            
  47.             value = value + translation;
  48.             if (value < min)
  49.             {
  50.                 value = max - (min - value - 1);
  51.             }
  52.             else if (value > max)
  53.             {
  54.                 value = min + (value - max - 1);
  55.             }
  56.             return Convert.ToChar(value);
  57.         }
  58.                
  59.         // Метод для конвертации текста с помощью слова-ключа
  60.         public static string EncryptWithWord(string _text, string _keyWord)
  61.         {
  62.             StringBuilder encrypted = new StringBuilder();
  63.             for (int i = 0, j = 0; i < _text.Length; i++, j++)
  64.             {
  65.                 if (j >= _keyWord.Length) j = 0;
  66.                 encrypted.Append(TranslateChar(_text[i], SecretWordConvert(_keyWord[j]),minSymbol,maxSymbol));
  67.             }
  68.             return "Зашифрованное слово: " + Convert.ToString(encrypted);
  69.         }
  70.         // Метод для конвертации символов из слова-ключа
  71.         public static int SecretWordConvert(char charFromKeyWord)
  72.         {
  73.             for (int i = 0; i <= chars.Length; i++)
  74.                 if (charFromKeyWord == chars[i])
  75.                     return numbers[i+1];
  76.             return 0;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement