Advertisement
isotonicq

Szyfrowanie playfair

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