Advertisement
Qrist

Կեսարի ծածկագիր (Caesar cipher)

Aug 11th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp8
  4. {
  5.     class Program
  6.     {
  7.         public static string Encipher(string input, int key)
  8.         {
  9.             string output = string.Empty;
  10.  
  11.             foreach (char ch in input)
  12.                 output += cipher(ch, key);
  13.  
  14.             return output;
  15.         }
  16.         public static string Decipher(string input, int key)
  17.         {
  18.             return Encipher(input, 26 - key);
  19.         }
  20.         public static char cipher(char ch, int key)
  21.         {
  22.             if (!char.IsLetter(ch))
  23.             {
  24.  
  25.                 return ch;
  26.             }
  27.  
  28.             char d = char.IsUpper(ch) ? 'A' : 'a';
  29.             return (char)((((ch + key) - d) % 26) + d);
  30.  
  31.         }
  32.      
  33.         static void Main(string[] args)
  34.         {
  35.             Console.WriteLine("Type a string to encrypt:");
  36.             string UserString = Console.ReadLine();
  37.             Console.WriteLine("\n");
  38.  
  39.             Console.Write("Enter your Key ");
  40.             int key = Convert.ToInt32(Console.ReadLine());
  41.             Console.WriteLine("\n");
  42.  
  43.             Console.WriteLine("Encrypted Data");
  44.             string cipherText = Encipher(UserString, key);
  45.             Console.WriteLine(cipherText);
  46.             Console.Write("\n");
  47.  
  48.             Console.WriteLine("Decrypted Data:");
  49.             string t = Decipher(cipherText, key);
  50.             Console.WriteLine(t);
  51.             Console.Write("\n");
  52.         }
  53.     }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement