Advertisement
isotonicq

PlayFair

Nov 25th, 2018
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.00 KB | None | 0 0
  1. //Dawid Grzeszuk
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace Playfair
  8. {
  9.     public class Program
  10.     {
  11.         private static void Main(string[] args)
  12.         {
  13.             var tekstJawny = "Fajnie";
  14.             var zaszyfrowany = Encipher(tekstJawny, "Dres");
  15.             var odszyfrowany = Decipher(zaszyfrowany, "Dres");
  16.  
  17.             Console.WriteLine($"Tekst jawny: {tekstJawny}\nTekst zaszyfrowany: {zaszyfrowany}\nTekst odszyfrowany: {odszyfrowany}");
  18.  
  19.         }
  20.  
  21.         private static int Mod(int a, int b)
  22.         {
  23.             return (a % b + b) % b;
  24.         }
  25.  
  26.         private static List<int> FindAllOccurrences(string str, char value)
  27.         {
  28.             List<int> indexes = new List<int>();
  29.  
  30.             int index = 0;
  31.             while ((index = str.IndexOf(value, index)) != -1)
  32.                 indexes.Add(index++);
  33.  
  34.             return indexes;
  35.         }
  36.  
  37.         private static string RemoveAllDuplicates(string str, List<int> indexes)
  38.         {
  39.             string retVal = str;
  40.  
  41.             for (int i = indexes.Count - 1; i >= 1; i--)
  42.                 retVal = retVal.Remove(indexes[i], 1);
  43.  
  44.             return retVal;
  45.         }
  46.  
  47.         private static char[,] GenerateKeySquare(string key)
  48.         {
  49.             char[,] keySquare = new char[5, 5];
  50.             string defaultKeySquare = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
  51.             string tempKey = string.IsNullOrEmpty(key) ? "CIPHER" : key.ToUpper();
  52.  
  53.             tempKey = tempKey.Replace("J", "");
  54.             tempKey += defaultKeySquare;
  55.  
  56.             for (int i = 0; i < 25; ++i)
  57.             {
  58.                 List<int> indexes = FindAllOccurrences(tempKey, defaultKeySquare[i]);
  59.                 tempKey = RemoveAllDuplicates(tempKey, indexes);
  60.             }
  61.  
  62.             tempKey = tempKey.Substring(0, 25);
  63.  
  64.             for (int i = 0; i < 25; ++i)
  65.                 keySquare[(i / 5), (i % 5)] = tempKey[i];
  66.  
  67.             return keySquare;
  68.         }
  69.  
  70.         private static void GetPosition(ref char[,] keySquare, char ch, ref int row, ref int col)
  71.         {
  72.             if (ch == 'J')
  73.                 GetPosition(ref keySquare, 'I', ref row, ref col);
  74.  
  75.             for (int i = 0; i < 5; ++i)
  76.                 for (int j = 0; j < 5; ++j)
  77.                     if (keySquare[i, j] == ch)
  78.                     {
  79.                         row = i;
  80.                         col = j;
  81.                     }
  82.         }
  83.  
  84.         private static char[] SameRow(ref char[,] keySquare, int row, int col1, int col2, int encipher)
  85.         {
  86.             return new char[] { keySquare[row, Mod((col1 + encipher), 5)], keySquare[row, Mod((col2 + encipher), 5)] };
  87.         }
  88.  
  89.         private static char[] SameColumn(ref char[,] keySquare, int col, int row1, int row2, int encipher)
  90.         {
  91.             return new char[] { keySquare[Mod((row1 + encipher), 5), col], keySquare[Mod((row2 + encipher), 5), col] };
  92.         }
  93.  
  94.         private static char[] SameRowColumn(ref char[,] keySquare, int row, int col, int encipher)
  95.         {
  96.             return new char[] { keySquare[Mod((row + encipher), 5), Mod((col + encipher), 5)], keySquare[Mod((row + encipher), 5), Mod((col + encipher), 5)] };
  97.         }
  98.  
  99.         private static char[] DifferentRowColumn(ref char[,] keySquare, int row1, int col1, int row2, int col2)
  100.         {
  101.             return new char[] { keySquare[row1, col2], keySquare[row2, col1] };
  102.         }
  103.  
  104.         private static string RemoveOtherChars(string input)
  105.         {
  106.             string output = input;
  107.  
  108.             for (int i = 0; i < output.Length; ++i)
  109.                 if (!char.IsLetter(output[i]))
  110.                     output = output.Remove(i, 1);
  111.  
  112.             return output;
  113.         }
  114.  
  115.         private static string AdjustOutput(string input, string output)
  116.         {
  117.             StringBuilder retVal = new StringBuilder(output);
  118.  
  119.             for (int i = 0; i < input.Length; ++i)
  120.             {
  121.                 if (!char.IsLetter(input[i]))
  122.                     retVal = retVal.Insert(i, input[i].ToString());
  123.  
  124.                 if (char.IsLower(input[i]))
  125.                     retVal[i] = char.ToLower(retVal[i]);
  126.             }
  127.  
  128.             return retVal.ToString();
  129.         }
  130.  
  131.         private static string Cipher(string input, string key, bool encipher)
  132.         {
  133.             string retVal = string.Empty;
  134.             char[,] keySquare = GenerateKeySquare(key);
  135.             string tempInput = RemoveOtherChars(input);
  136.             int e = encipher ? 1 : -1;
  137.  
  138.             if ((tempInput.Length % 2) != 0)
  139.                 tempInput += "X";
  140.  
  141.             for (int i = 0; i < tempInput.Length; i += 2)
  142.             {
  143.                 int row1 = 0;
  144.                 int col1 = 0;
  145.                 int row2 = 0;
  146.                 int col2 = 0;
  147.  
  148.                 GetPosition(ref keySquare, char.ToUpper(tempInput[i]), ref row1, ref col1);
  149.                 GetPosition(ref keySquare, char.ToUpper(tempInput[i + 1]), ref row2, ref col2);
  150.  
  151.                 if (row1 == row2 && col1 == col2)
  152.                 {
  153.                     retVal += new string(SameRowColumn(ref keySquare, row1, col1, e));
  154.                 }
  155.                 else if (row1 == row2)
  156.                 {
  157.                     retVal += new string(SameRow(ref keySquare, row1, col1, col2, e));
  158.                 }
  159.                 else if (col1 == col2)
  160.                 {
  161.                     retVal += new string(SameColumn(ref keySquare, col1, row1, row2, e));
  162.                 }
  163.                 else
  164.                 {
  165.                     retVal += new string(DifferentRowColumn(ref keySquare, row1, col1, row2, col2));
  166.                 }
  167.             }
  168.  
  169.             retVal = AdjustOutput(input, retVal);
  170.  
  171.             return retVal;
  172.         }
  173.  
  174.         public static string Encipher(string input, string key)
  175.         {
  176.             return Cipher(input, key, true);
  177.         }
  178.  
  179.         public static string Decipher(string input, string key)
  180.         {
  181.             return Cipher(input, key, false);
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement