Advertisement
Guest User

Untitled

a guest
Aug 17th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.48 KB | None | 0 0
  1. /*Write a program that encodes and decodes a string using given encryption key (cipher).
  2.  The key consists of a sequence of characters.
  3.  The encoding/decoding is done by performing XOR (exclusive or) operation over the first letter of the string with the first of the key,
  4.  the second – with the second, etc. When the last key character is reached, the next is the first.*/
  5. using System;
  6. using System.Text;
  7.  
  8. class EncryptingAndDecrypting
  9. {
  10.     static void Main()
  11.     {
  12.         Console.Write("Enter a text: ");
  13.         string text = Console.ReadLine();
  14.         Console.Write("Enter a key: ");
  15.         string key = Console.ReadLine();
  16.  
  17.         if (key.Length < text.Length)//I want to make the length of the key same to the text length or bigger
  18.         {
  19.             while (key.Length < text.Length)
  20.             {
  21.                 key += key;
  22.             }
  23.         }
  24.  
  25.         string encryptedText = EncryptingTheString(text, key);
  26.         string decryptedText = EncryptingTheString(encryptedText, key);
  27.  
  28.         Console.WriteLine("The encrypted text is: {0}", encryptedText);
  29.         Console.WriteLine("The decrypted text is: {0}", decryptedText);
  30.     }
  31.  
  32.     private static string EncryptingTheString(string text, string key)
  33.     {
  34.         StringBuilder encryptedString = new StringBuilder();
  35.  
  36.         for (int i = 0; i < text.Length; i++)
  37.         {
  38.             encryptedString.Append((char)(text[i] ^ key[i]));
  39.         }
  40.         return encryptedString.ToString();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement