Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.99 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 TI_Lab1
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Write("Выбирите язык 1 - русский; 0 - английский:    ");
  14.             int lang = int.Parse(Console.ReadLine());
  15.  
  16.             Viziner v = new Viziner();
  17.            
  18.             v.Crypt(lang);
  19.             v.DeCrypt();
  20.             Cryptanalysis c = new Cryptanalysis();
  21.             int keylen = c.Kasiski();
  22.             string key = "";
  23.             key = c.FreqAnalysis(keylen).ToString();
  24.             Console.ReadKey();
  25.         }
  26.     }
  27. }
  28.  
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Text;
  33. using System.Threading.Tasks;
  34. using System.IO;
  35.  
  36. namespace TI_Lab1
  37. {
  38.     class Viziner
  39.     {
  40.         public string sym;
  41.  
  42.         char[] eng = {
  43.     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  44.     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  45.     'u', 'v', 'w', 'x', 'y', 'z'};
  46.  
  47.         char[] rus = {
  48.     'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'з', 'ж', 'и',
  49.     'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т',
  50.     'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь',
  51.     'э', 'ю', 'я' };
  52.  
  53.         public Viziner()
  54.         {
  55.             sym = "";
  56.         }
  57.  
  58.         public void setVizinerSym(string temp)
  59.         {
  60.             sym = temp;
  61.         }
  62.  
  63.  
  64.  
  65.         public string getVizinerSym()
  66.         {
  67.             return sym;
  68.         }
  69.  
  70.         public int CheckLang(char sm)
  71.         {
  72.  
  73.             if (rus.Contains(sm) == true) return 0;//русский
  74.             else if (eng.Contains(sm) == true) return 1; //английский
  75.             else return 2;
  76.         }
  77.  
  78.         public int Numb(char sym, char[] alphabet, int size)
  79.         {
  80.             for (int i = 0; i < size; i++)
  81.             {
  82.                 if (alphabet[i] == sym)
  83.                     return i;
  84.             }
  85.             return 0;
  86.         }
  87.  
  88.  
  89.         public int Crypt(int lang)
  90.         {
  91.             int size_rus = rus.Length, size_eng = eng.Length, i = 0, k = 0;
  92.             string text = "";
  93.             if (lang == 0) text = System.IO.File.ReadAllText(@"text_eng.txt", Encoding.Default);
  94.             if (lang == 1) text = System.IO.File.ReadAllText(@"text_rus.txt", Encoding.Default);
  95.             char[] tmp = text.ToCharArray();
  96.  
  97.             if ((CheckLang(tmp[0]) == 0) || (CheckLang(tmp[1]) == 0) || (CheckLang(tmp[2]) == 0))
  98.             {
  99.                 Console.WriteLine("Введите ключ на русском языке: ");
  100.                 string str = System.Console.ReadLine();
  101.                 char[] key = str.ToCharArray();
  102.  
  103.                 for (int e = 0; e < text.Length; e++)
  104.                 {
  105.                     if ((rus.Contains(tmp[e]) == true))
  106.                     {
  107.                         k = (Numb(tmp[e], rus, size_rus) + Numb(key[i], rus, size_rus)) % size_rus;
  108.                         sym += rus[k];
  109.                         i = (i + 1) % key.Length;
  110.                     }
  111.                     else sym += tmp[e];
  112.                 }
  113.             }
  114.             else
  115.             {
  116.                 Console.WriteLine("Введите ключ на английском языке: ");
  117.                 string str = System.Console.ReadLine();
  118.                 char[] key = str.ToCharArray();
  119.                 for (int e = 0; e < text.Length; e++)
  120.                 {
  121.                     if (eng.Contains(tmp[e]) == true)
  122.                     {
  123.                         k = (Numb(tmp[e], eng, size_eng) + Numb(key[i], eng, size_eng)) % size_eng;
  124.                         sym += eng[k];
  125.                         i = (i + 1) % key.Length;
  126.                     }
  127.                     else sym += tmp[e];
  128.  
  129.                 }
  130.             }
  131.             Console.WriteLine(sym);
  132.             Console.WriteLine();
  133.             System.IO.File.WriteAllText(@"crypt.txt", sym, Encoding.Default);
  134.  
  135.             Console.WriteLine("Зашифровано");
  136.             Console.WriteLine();
  137.             sym = sym.Remove(0);
  138.             return 1;
  139.         }
  140.  
  141.         public int DeCrypt()
  142.         {
  143.             int size_rus = rus.Length, size_eng = eng.Length, i = 0, k = 0;
  144.             string text = System.IO.File.ReadAllText(@"crypt.txt", Encoding.Default);
  145.             char[] tmp = text.ToCharArray();
  146.  
  147.             if ((CheckLang(tmp[0]) == 0) || (CheckLang(tmp[1]) == 0) || (CheckLang(tmp[2]) == 0))
  148.             {
  149.                 Console.WriteLine("Введите ключ на русском языке: ");
  150.                 string str = System.Console.ReadLine();
  151.                 char[] key = str.ToCharArray();
  152.  
  153.                 for (int e = 0; e < text.Length; e++)
  154.                 {
  155.                     if ((rus.Contains(tmp[e]) == true))
  156.                     {
  157.                         k = (size_rus + Numb(tmp[e], rus, size_rus) - Numb(key[i], rus, size_rus)) % size_rus;
  158.                         sym += rus[k];
  159.                         i = (i + 1) % key.Length;
  160.                     }
  161.                     else sym += tmp[e];
  162.                 }
  163.             }
  164.             else
  165.             {
  166.                 Console.WriteLine("Введите ключ на английском языке: ");
  167.                 string str = System.Console.ReadLine();
  168.                 char[] key = str.ToCharArray();
  169.                 for (int e = 0; e < text.Length; e++)
  170.                 {
  171.                     if (eng.Contains(tmp[e]) == true)
  172.                     {
  173.                         k = (size_eng + Numb(tmp[e], eng, size_eng) - Numb(key[i], eng, size_eng)) % size_eng;
  174.                         sym += eng[k];
  175.                         i = (i + 1) % key.Length;
  176.                     }
  177.                     else sym += tmp[e];
  178.                 }
  179.             }
  180.  
  181.  
  182.             System.IO.File.WriteAllText(@"resultf.txt", sym, Encoding.Default);
  183.             Console.WriteLine(sym);
  184.             Console.WriteLine();
  185.  
  186.             Console.WriteLine("Расшифровано");
  187.             Console.WriteLine();
  188.             sym = sym.Remove(0);
  189.             return 1;
  190.         }
  191.  
  192.     }
  193. }
  194.  
  195. using System;
  196. using System.Collections.Generic;
  197. using System.Linq;
  198. using System.Text;
  199. using System.Threading.Tasks;
  200.  
  201. namespace TI_Lab1
  202. {
  203.     class Cryptanalysis
  204.     {
  205.         public string sym;
  206.  
  207.         char[] eng = {
  208.     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  209.     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  210.     'u', 'v', 'w', 'x', 'y', 'z'};
  211.  
  212.         char[] rus = {
  213.     'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'з', 'ж', 'и',
  214.     'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т',
  215.     'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь',
  216.     'э', 'ю', 'я'
  217.                      };
  218.  
  219.  
  220.  
  221.         public Cryptanalysis()
  222.         {
  223.             sym = "";
  224.         }
  225.  
  226.         public void setCryptanalysisSym(string temp)
  227.         {
  228.             sym = temp;
  229.         }
  230.  
  231.  
  232.  
  233.         public string getCryptanalysisSym()
  234.         {
  235.             return sym;
  236.         }
  237.  
  238.         public int Numb(char sym, char[] alphabet, int size)
  239.         {
  240.             for (int i = 0; i < size; i++)
  241.             {
  242.                 if (alphabet[i] == sym)
  243.                     return i;
  244.             }
  245.             return 0;
  246.         }
  247.  
  248.         int nod(int a, int b)
  249.         {
  250.             int i;
  251.             for (i = a; i > 1; --i)
  252.                 if ((a % i == 0) && (b % i == 0))
  253.                     return i;
  254.             return 1;
  255.         }
  256.  
  257.         public int Kasiski()
  258.         {
  259.             int nl = 0, n = 50000;
  260.             string text = System.IO.File.ReadAllText(@"crypt.txt", Encoding.Default);
  261.             int[] nods = new int[n];
  262.             char[] tmp = text.ToCharArray();
  263.             for (int e = 0; e < text.Length; e++)
  264.             {
  265.                 if ((rus.Contains(tmp[e]) == true) || (eng.Contains(tmp[e]) == true))
  266.                     sym += tmp[e];
  267.  
  268.             }
  269.  
  270.             string str1 = "", str2 = "";
  271.             int[] l = new int[n];
  272.  
  273.             Console.WriteLine("-----------------------------------ЖДИТЕ------------------------------");
  274.             for (int i = 0; i < sym.Length - 2; ++i)
  275.             {
  276.                 str1 = sym[i].ToString() + sym[i + 1].ToString() + sym[i + 2].ToString();
  277.  
  278.                 for (int j = i + 1; j < sym.Length - 2; ++j)
  279.                 {
  280.                     str2 = sym[j].ToString() + sym[j + 1].ToString() + sym[j + 2].ToString();
  281.                     if (String.Equals(str1, str2) == true)
  282.                         l[nl++] = j - i;
  283.  
  284.                 }
  285.                 for (int z = 0; z < nl; ++z)
  286.                     for (int q = z + 1; q < nl; ++q)
  287.                     {
  288.                         nods[nod(l[z], l[q])]++;
  289.  
  290.                     }
  291.                 for (int j = 0; j < nl; j++)
  292.                 {
  293.                     l[j] = 0;
  294.                 }
  295.                 nl = 0;
  296.                 //  str2 = "";
  297.                 //  str1 = "";
  298.             }
  299.  
  300.  
  301.             int keylen = 0;
  302.             for (int i = 2; i < n; ++i)
  303.                 if (nods[keylen] < nods[i])
  304.                     keylen = i;
  305.  
  306.             Console.WriteLine("Длина ключа = {0}", keylen);
  307.  
  308.             return keylen;
  309.         }
  310.  
  311.         public char[,] Matrix(int keylen)
  312.         {
  313.             int n = sym.Length / keylen + 1;
  314.             char[] tmp = sym.ToCharArray();
  315.             char[,] text = new char[n, keylen];
  316.             for (int i = 0; i < sym.Length; i++)
  317.             {
  318.                 text[i / keylen, i % keylen] = tmp[i];
  319.             }
  320.             return text;
  321.         }
  322.  
  323.         public int CheckLang(char sm)
  324.         {
  325.  
  326.             if (rus.Contains(sm) == true) return 0;//русский
  327.             else if (eng.Contains(sm) == true) return 1; //английский
  328.             else return 2;
  329.         }
  330.         public char[] FreqAnalysis(int keylen)
  331.         {
  332.  
  333.             int n = sym.Length / keylen + 1;
  334.             int freq_rus_O = 16, freq_eng_E = 5;
  335.             char[] key = new char[keylen];
  336.             int j = 0;
  337.  
  338.             char[,] text = Matrix(keylen);
  339.             int e = 0;
  340.             int[] alph_freq = new int[]
  341.                                            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  342.                                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  343.                                              0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  344.                                              0, 0, 0, 0 };
  345.  
  346.             int size = 0;
  347.             if (CheckLang(sym[0]) == 0) size = rus.Length;
  348.             if (CheckLang(sym[0]) == 1) size = eng.Length;
  349.  
  350.             if (size == rus.Length)
  351.             {
  352.  
  353.                 for (int k = 0; k < keylen; k++)
  354.                 {
  355.                     for (int a = 0; a < size; a++)
  356.                     {
  357.                         for (int i = 0; i < n; i++)
  358.                         {
  359.  
  360.                             if (rus[a] == text[i, j])
  361.                                 alph_freq[a]++;
  362.                         }
  363.                     }
  364.  
  365.                     int max = 0;
  366.                     int maxi = 0;
  367.                     for (int a = 0; a < size; a++)
  368.                     {
  369.                         if (max <= alph_freq[a])
  370.                         {
  371.                             max = alph_freq[a];
  372.                             maxi = a + 1;
  373.                         }
  374.                     }
  375.                     e = (maxi - freq_rus_O + size) % size;
  376.                     key[k] = rus[e];
  377.                     for (int a = 0; a < size; a++)
  378.                         alph_freq[a] = 0;
  379.  
  380.  
  381.                     e = 0;
  382.                     j = (j + 1) % keylen;
  383.                 }
  384.             }
  385.             if (size == eng.Length)
  386.             {
  387.                 for (int k = 0; k < keylen; k++)
  388.                 {
  389.                     for (int a = 0; a < size; a++)
  390.                     {
  391.                         for (int i = 0; i < n; i++)
  392.                         {
  393.  
  394.                             if (eng[a] == text[i, j])
  395.                                 alph_freq[a]++;
  396.                         }
  397.                     }
  398.  
  399.                     int max = 0;
  400.                     int maxi = 0;
  401.                     for (int a = 0; a < size; a++)
  402.                     {
  403.                         if (max <= alph_freq[a])
  404.                         {
  405.                             max = alph_freq[a];
  406.                             maxi = a + 1;
  407.                         }
  408.                     }
  409.                     e = (maxi - freq_eng_E + size) % size;
  410.                     key[k] = eng[e];
  411.                     for (int a = 0; a < size; a++)
  412.                         alph_freq[a] = 0;
  413.  
  414.                     e = 0;
  415.                     j = (j + 1) % keylen;
  416.                 }
  417.             }
  418.             Console.WriteLine();
  419.             Console.Write("Ключ --------->");
  420.             for (int i = 0; i < keylen; i++)
  421.                 Console.Write(key[i]);
  422.             return key;
  423.         }
  424.     }
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement