hozer

[IS] MH cryptosystem

Oct 9th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Lab2
  5. {
  6.     class Program
  7.     {
  8.         const int size = 5;
  9.         static void Main(string[] args)
  10.         {
  11.             int m, t;
  12.             int[] key = new int[size], enkey = new int[size];
  13.             string iobuff;
  14.             string[] arrkey = new string[size];
  15.             m = 63; t = 29;
  16.             key = new int[size] {2, 3, 6, 13, 27};
  17.             //input key information
  18.             /*
  19.             Console.Write("Input m: ");
  20.             m = int.Parse(Console.ReadLine());
  21.  
  22.             Console.Write("Input t: ");
  23.             t = int.Parse(Console.ReadLine());
  24.  
  25.             Console.Write("Input key: ");
  26.             iobuff = Console.ReadLine();
  27.             arrkey = iobuff.Split(' ');
  28.             for (int i = 0; i < arrkey.Length; i++)
  29.             {
  30.                 key[i] = int.Parse(arrkey[i]);
  31.             }*/
  32.  
  33.             //generate key for encryption
  34.             enkey = genKey(m, t, key);
  35.             Console.Write("Secret key: ");
  36.             foreach (int n in enkey)
  37.             {
  38.                 Console.Write("{0} ", n);
  39.             }
  40.             Console.WriteLine();
  41.  
  42.             //encrypt
  43.             Console.WriteLine("Input string: ");
  44.             iobuff = Console.ReadLine();
  45.             Console.WriteLine("Encrypted string: {0}", genMt(m, t));
  46.  
  47.             int[] cryp = encrypt(iobuff, enkey);
  48.  
  49.             //gen decrypt key and decrypt
  50.             enkey = genKey(m, genMt(m, t), enkey);
  51.             foreach (int n in enkey)
  52.             {
  53.                 Console.Write("{0} ", n);
  54.             }
  55.             Console.WriteLine();
  56.  
  57.             //Console.WriteLine("Input string: ");
  58.             //iobuff = Console.ReadLine();
  59.             Console.WriteLine("Encrypted string: {0}", decrypt(cryp, enkey, genMt(m, t), m));
  60.  
  61.  
  62.             Console.ReadKey();
  63.         }
  64.  
  65.  
  66.         static int[] encrypt(string s, int[] key)
  67.         {
  68.             StringBuilder ctext = new StringBuilder(s);
  69.             int[] crypted = new int[ctext.Length];
  70.  
  71.             for (int i = 0; i < ctext.Length; i++)
  72.             {
  73.                 if (char.IsLetter(ctext[i]))
  74.                 {
  75.                     int nc = 0;
  76.                     bool upper = char.IsUpper(ctext[i]);
  77.                     if (upper) ctext[i] = (char)(ctext[i] + 32);
  78.                     string bin = numToBin(ctext[i] - 96);
  79.                     for (int j = 0; j < bin.Length; j++)
  80.                     {
  81.                         nc += key[j] * (bin[j] - 48);
  82.                     }
  83.                     crypted[i] = nc;
  84.                     if (upper) ctext[i] = (char)(ctext[i] - 32);
  85.                 }
  86.                 else if (ctext[i] == ' ') crypted[i] = 0;
  87.             }
  88.  
  89.             return crypted;
  90.         }
  91.  
  92.         static string decrypt(int[] crypted, int[] key, int mt, int m)
  93.         {
  94.             StringBuilder text = new StringBuilder(crypted.Length);
  95.             for (int i = 0; i < crypted.Length; i++)
  96.             {
  97.                 if (crypted[i] == 0)
  98.                 {
  99.                     text.Append(' ');
  100.                     continue;
  101.                 }
  102.                     int nc = 0;
  103.                     char[] bin = new char[size];
  104.                     crypted[i] = (crypted[i]*mt) % m;
  105.                     for (int j = key.Length-1; j >= 0; j--)
  106.                     {
  107.                         if (crypted[i] - key[j] >= 0)
  108.                         {
  109.                             crypted[i] -= key[j];
  110.                             bin[j] = '1';
  111.                         }
  112.                         else { bin[j] = '0'; }
  113.                     }
  114.                     for (int k = 0; k < bin.Length; k++)
  115.                         nc += (bin[k] - 48) * (int)Math.Pow(2, bin.Length - k -1);
  116.  
  117.                     text.Append((char)(nc % 27 + 96));
  118.            
  119.             }
  120.  
  121.             return text.ToString();
  122.         }
  123.  
  124.         static int[] genKey(int m, int t, int[] key)
  125.         {
  126.             int[] outKey = new int[key.Length];
  127.             for (int i = 0; i < key.Length; i++)
  128.             {
  129.                 outKey[i] = (key[i]*t)%m;
  130.             }
  131.             return outKey;
  132.         }
  133.  
  134.         static int genMt(int m, int t)
  135.         {
  136.             for (int i = 0; i < m; i++)
  137.             {
  138.                 if ((t*i)%m == 1)
  139.                 {
  140.                     return i;
  141.                 }
  142.             }
  143.  
  144.             return -1;
  145.         }
  146.  
  147.         static string numToBin(int num)
  148.         {
  149.             char[] bin = new char[size];
  150.             int testNumber = 1;
  151.             for (int i = size-1; i >= 0; i--)
  152.             {
  153.                 bin[i] += (num & testNumber) > 0 ? '1' : '0';
  154.                 testNumber = testNumber << 1;
  155.             }
  156.  
  157.             return new string(bin);
  158.         }
  159.  
  160.  
  161.         /*static string encrypt(string s , int[] key)
  162.         {
  163.             StringBuilder ctext = new StringBuilder(s);
  164.             for (int i = 0; i < ctext.Length; i++)
  165.             {
  166.                 if (char.IsLetter(ctext[i]))
  167.                 {
  168.                     int nc = 0;
  169.                     bool upper = char.IsUpper(ctext[i]);
  170.                     if (upper) ctext[i] = (char)(ctext[i] + 32);
  171.                     string bin = numToBin(ctext[i] - 96);
  172.                     for (int j = 0; j < bin.Length; j++)
  173.                     {
  174.                         nc += key[j] * (bin[j] - 48);
  175.                     }
  176.                     ctext[i] = (char)(nc % 27 + 96);
  177.                     if (upper) ctext[i] = (char)(ctext[i] - 32);
  178.                 }
  179.             }
  180.  
  181.             return ctext.ToString();
  182.         }*/
  183.  
  184.  
  185.         /*
  186.         static string decrypt(string s, int[] key, int mt)
  187.         {
  188.             StringBuilder text = new StringBuilder(s);
  189.             for (int i = 0; i < text.Length; i++)
  190.             {
  191.                 if (char.IsLetter(text[i]))
  192.                 {
  193.                     int nc = 0;
  194.                     bool upper = char.IsUpper(text[i]);
  195.                     if (upper) text[i] = (char)(text[i] + 32);
  196.                     string bin = numToBin((text[i] - 96) * mt);
  197.                     for (int j = 0; j < bin.Length; j++)
  198.                     {
  199.                         nc += key[j] * (bin[j] - 48);
  200.                     }
  201.                     text[i] = (char)(nc % 27 + 96);
  202.                     if (upper) text[i] = (char)(text[i] - 32);
  203.                 }
  204.             }
  205.  
  206.             return text.ToString();
  207.         }*/
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment