Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 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 ConsoleApp4
  8. {
  9.     using System;
  10.  
  11.     public class Cipher
  12.     {
  13.  
  14.         private static readonly String key = "hill";
  15.         private static readonly int[][] mat = new int[][]
  16.         {
  17.             new int[] {key[0] - 'a' , key[1] - 'a' },
  18.             new int[] {key[2] - 'a' , key[3] - 'a' }
  19.         };
  20.  
  21.         private const int MOD = 26;
  22.  
  23.         public String Encrypt(String str)
  24.         {
  25.             str += '?';
  26.             int len = str.Length;
  27.             var sb = new StringBuilder();
  28.             for (int i = 0; i < len && str[i] != '?'; i += 2)
  29.             {
  30.                 //jamal?
  31.                 int[] cur = new int[] { str[i] - 'a', str[i + 1] - 'a' };
  32.                 char[] arr = mult(cur, mat);
  33.                 sb.Append(arr[0]).Append(arr[1]);
  34.             }
  35.             return sb.ToString();
  36.  
  37.         }
  38.  
  39.         private char[] mult(int[] cur, int[][] matrix)
  40.         {
  41.             char[] toReturn = new char[2];
  42.             for (int i = 0; i < 2; ++i)
  43.             {
  44.                 int res = 0;
  45.                 for (int j = 0; j < 2; ++j)
  46.                 {
  47.                     res += matrix[i][j] * cur[j];
  48.                     res %= MOD;//26
  49.                 }
  50.                 toReturn[i] = (char)(res + 'a');
  51.             }
  52.             return toReturn;
  53.         }
  54.         public String Decrypt(String enc)
  55.         {
  56.             int d = det();
  57.             int dinv = FindDinv(d);
  58.             if (dinv == -1) throw new Exception("no multiplicative inverse");
  59.             int[][] adj = adjugate();
  60.  
  61.             for(int i = 0; i < adj.Length; ++i)
  62.             {
  63.                 for(int j = 0; j < adj[i].Length; ++j)
  64.                 {
  65.                     adj[i][j] = ((dinv * adj[i][j]) + MOD * 100) % MOD;
  66.                 }
  67.             }
  68.  
  69.             var sb = new StringBuilder();
  70.  
  71.             for (int i = 0; i < enc.Length; i += 2)
  72.             {
  73.                 int[] cur = new int[] { enc[i] - 'a', enc[i + 1] - 'a' };
  74.                 char[] arr = mult(cur, adj);
  75.                 sb.Append(arr[0]).Append(arr[1]);
  76.             }
  77.  
  78.             return sb.ToString();
  79.         }
  80.  
  81.         private int[][] adjugate()
  82.         {
  83.             return new int[][]
  84.             {
  85.                 new int[]{ (mat[1][1] +2600) % MOD, (-mat[0][1] + 2600) % MOD},
  86.                 new int[]{ (-mat[1][0] + 2600) % MOD, (mat[0][0] + 2600) % MOD}
  87.             };
  88.         }
  89.  
  90.         private int FindDinv(int d)
  91.         {
  92.             for (int dinv = 1; dinv < MOD; ++dinv)
  93.             {
  94.                 bool valid = (d * dinv) % MOD == 1;
  95.                 if (valid)
  96.                     return dinv;
  97.             }
  98.             return -1;
  99.         }
  100.  
  101.         private int det()
  102.         {
  103.             int det = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
  104.             while (det < 0) det += MOD;
  105.             return det % MOD;
  106.         }
  107.         public static void Main()
  108.         {
  109.             Cipher cipher = new Cipher();
  110.             String msg = "shortexample";
  111.             String enc = cipher.Encrypt(msg);
  112.             String dec = cipher.Decrypt(enc);
  113.             String toShow = $"Encrypted:{enc}\nDecrypted:{dec}";
  114.             Console.WriteLine(toShow);
  115.             Console.ReadKey();
  116.         }
  117.  
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement