Facebook
From Tiny Moth, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 73
  1.  static bool Integer(int key)
  2.         {
  3.             return (key >= 1 && key <= 255);
  4.         }
  5.         static char EncryptChar(char ch, int key)
  6.         {
  7.             char newCh = ch;    
  8.                 int ascii = (int)ch;//find the ascii code of ch
  9.                 int newAscii = ascii ^ key;  
  10.                 newCh = Convert.ToChar(newAscii);          
  11.             return newCh;
  12.         }
  13.         static string Encrypt(string word, int key)  
  14.         {
  15.             string newWord = "";
  16.             foreach (char ch in word)
  17.             {
  18.                 char newCh = EncryptChar(ch, key);
  19.                 newWord=newWord + newCh;
  20.             }
  21.             return newWord;
  22.         }
  23.         static void Main(string[] args)
  24.         {
  25.             Console.Write("Enter a phrase: ");
  26.             string phrase = Console.ReadLine();
  27.  
  28.             Console.Write("Enter a key: ");
  29.             int key = Convert.ToInt32(Console.ReadLine());
  30.             while (!Integer(key))
  31.             {
  32.                 Console.Write("Enter a character between 1-255: ");
  33.                 key = Convert.ToInt32(Console.ReadLine());
  34.             }
  35.             string newPhrase = Encrypt(phrase, key);
  36.             Console.WriteLine("The new phrase is: " + newPhrase);
  37.             Console.ReadLine();
  38.         }