Advertisement
kisame1313

Untitled

Jul 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication
  4. {
  5.     class Program
  6.     {
  7.         //абвгдеёжзийклмнопрстуфхцчшщъыьэюя
  8.  
  9.         static string secretWord=string.Empty;
  10.         static string cryptWord=string.Empty;
  11.  
  12.         static int [] secretInt;
  13.  
  14.         static bool exit = false;
  15.  
  16.         static void Main ( string [] args )
  17.         {
  18.             Console.WriteLine ( "Для шифровки сообщения нажмите клавишу \"C\"" );
  19.             Console.WriteLine ( "Для шифровки сообщения нажмите клавишу \"E\"" );
  20.             Console.WriteLine ( "Для шифровки сообщения нажмите клавишу \"Esc\"" );
  21.             while (!exit )
  22.             {
  23.                 Menu ();
  24.             }
  25.         }
  26.  
  27.         public static char TranslateChar ( char currentSymbol, int translation, char minSymbol = 'а', char maxSymbol = 'я' )
  28.         {
  29.             translation = (translation - minSymbol);
  30.             if ( currentSymbol >= minSymbol & currentSymbol <= maxSymbol )
  31.             {
  32.                 currentSymbol = Convert.ToChar ( currentSymbol + translation );
  33.                 if ( currentSymbol < minSymbol )
  34.                 {
  35.                     currentSymbol = Convert.ToChar ( maxSymbol - ( minSymbol - currentSymbol - 1 ) );
  36.                 }
  37.                 else if ( currentSymbol > maxSymbol )
  38.                 {
  39.                     currentSymbol = Convert.ToChar( minSymbol + ( currentSymbol - maxSymbol - 1 ) );
  40.                 }
  41.                 return Convert.ToChar ( currentSymbol );
  42.             }
  43.             return currentSymbol;
  44.         }
  45.  
  46.         static char Crypt ( char crypt, int shift )
  47.         {
  48.             return   TranslateChar ( crypt, shift );
  49.         }
  50.  
  51.         static int [] CryptWord ( string word )
  52.         {
  53.             char [] symbols = word.ToCharArray ();
  54.             int [] secretInt = new int [word.Length];
  55.  
  56.             for ( int i = 0 ; i < word.Length ; i++ )
  57.             {
  58.                 secretInt [i] = Convert.ToInt32 ( symbols [i] );
  59.             }
  60.             return secretInt;
  61.         }
  62.  
  63.         static string Crypting ( string stringForCrypting, string cryptingWord )
  64.         {
  65.             secretWord = stringForCrypting;
  66.             cryptWord = cryptingWord;
  67.  
  68.             secretInt = CryptWord( cryptWord );
  69.  
  70.             int step;
  71.  
  72.             string cryptedString = "";
  73.  
  74.             for ( int i = 0 ; i < secretWord.Length ; i++ )
  75.             {
  76.                 if ( i < cryptWord.Length )
  77.                 {
  78.                     step = secretInt [i];
  79.                     cryptedString += Crypt ( secretWord[i], step );
  80.                 }
  81.                 else
  82.                 {
  83.                     int ind = ( cryptingWord.Length - ( i % cryptingWord.Length ) )-1;
  84.                     step = secretInt [ind];
  85.                     cryptedString += Crypt ( secretWord[i], step );
  86.                 }
  87.             }
  88.             return cryptedString;
  89.         }
  90.  
  91.         static string Encrypting ( string encryptingWord )
  92.         {
  93.             if ( encryptingWord == cryptWord )
  94.             {
  95.                 return secretWord;
  96.             }
  97.             else
  98.             {
  99.                 return "Неверное слово: ";
  100.             }
  101.         }
  102.  
  103.         static void Menu ()
  104.         {
  105.            
  106.             ConsoleKeyInfo key = Console.ReadKey (true);
  107.  
  108.             switch ( key.Key )
  109.             {
  110.                 case ConsoleKey.C:
  111.                     Console.Write ( "Введите слово для шифровки и слово которым будет произведена шифровка: " );
  112.                     Console.WriteLine ( Crypting ( Console.ReadLine (), Console.ReadLine () ) );
  113.                     break;
  114.                 case ConsoleKey.E:
  115.                     Console.Write ( "Введите секретное слово: " );
  116.                     Console.WriteLine ( Encrypting ( Console.ReadLine () ) );
  117.                     break;
  118.                 case ConsoleKey.Escape:
  119.                     exit = true;
  120.                     break;
  121.             }
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement