Guest User

Untitled

a guest
Jun 17th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SpecialOlympics
  8. {
  9.     class Program
  10.     {
  11.         interface IEncoder<T>
  12.         {
  13.             T Encode(T chipher, T key);
  14.         }
  15.  
  16.         interface IDecoder<T>
  17.         {
  18.             T Decode(T text, T key);
  19.         }
  20.  
  21.         class OneTimePad : IEncoder<char>, IDecoder<char>, IEncoder<string>, IDecoder<string>
  22.         {
  23.             private static readonly int LENGTH_OF_LATIN = 26;
  24.  
  25.             private string XorStrings(string text, string key, Func<char,char,char> operation)
  26.             {
  27.                 if(text.Count(c => 'A' <= c && c <= 'Z') > key.Length) throw new ArgumentException("Key length should be greater then letters count in text");
  28.                 var builder = new StringBuilder(text);
  29.                 var numberOfLetters = 0;
  30.                 for (var i = 0; i < builder.Length; i++)
  31.                 {
  32.                     var c = builder[i];
  33.                     if ('A' <= c && c <= 'Z')
  34.                     {
  35.                         builder[i] = operation(c,key[numberOfLetters++]);
  36.                     }
  37.                 }
  38.                 return builder.ToString();
  39.             }
  40.  
  41.  
  42.             public string Encode(string chipher, string key)
  43.             {
  44.                 return XorStrings(chipher, key, Encode);
  45.             }
  46.  
  47.             public string Decode(string text, string key)
  48.             {
  49.                 return XorStrings(text, key, Decode);
  50.             }
  51.  
  52.             public char Encode(char chipher, char key)
  53.             {
  54.                 return (char)('A' + (((chipher - 'A') + (key - 'A')) % LENGTH_OF_LATIN));
  55.             }
  56.  
  57.             public char Decode(char text, char key)
  58.             {
  59.                 var diff = (text - 'A') - (key - 'A');
  60.                 return (char)('A' + ((diff < 0 ? LENGTH_OF_LATIN + diff : diff) % LENGTH_OF_LATIN));
  61.             }
  62.         }
  63.  
  64.         static void Main(string[] args)
  65.         {
  66.             var oneTimePad = new OneTimePad();
  67.             if (args.Length < 3)
  68.             {
  69.                 Console.WriteLine("Not enought arguments");
  70.                 Console.WriteLine("First argument is action (--encode or --decode), second is key and third is text or chipher text.");
  71.             }
  72.             try
  73.             {
  74.                 switch (args[0])
  75.                 {
  76.                     case "--encode":
  77.                     case "-e":
  78.                         Console.WriteLine(oneTimePad.Encode(args[2], args[1]));
  79.                         break;
  80.                     case "--decode":
  81.                     case "-d":
  82.                         Console.WriteLine(oneTimePad.Decode(args[2], args[1]));
  83.                         break;
  84.                     default:
  85.                         Console.WriteLine(
  86.                             "First argument is action (--encode or --decode), second is key and third is text or chipher text. Only latin upper case letters are encoded");
  87.                         break;
  88.                 }
  89.             }
  90.             catch (Exception e)
  91.             {
  92.                 Console.WriteLine("Error occured: {0}",e.Message);
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment