Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TI_Lab1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Выбирите язык 1 - русский; 0 - английский: ");
- int lang = int.Parse(Console.ReadLine());
- Viziner v = new Viziner();
- v.Crypt(lang);
- v.DeCrypt();
- Cryptanalysis c = new Cryptanalysis();
- int keylen = c.Kasiski();
- string key = "";
- key = c.FreqAnalysis(keylen).ToString();
- Console.ReadKey();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace TI_Lab1
- {
- class Viziner
- {
- public string sym;
- char[] eng = {
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
- 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
- 'u', 'v', 'w', 'x', 'y', 'z'};
- char[] rus = {
- 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'з', 'ж', 'и',
- 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т',
- 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь',
- 'э', 'ю', 'я' };
- public Viziner()
- {
- sym = "";
- }
- public void setVizinerSym(string temp)
- {
- sym = temp;
- }
- public string getVizinerSym()
- {
- return sym;
- }
- public int CheckLang(char sm)
- {
- if (rus.Contains(sm) == true) return 0;//русский
- else if (eng.Contains(sm) == true) return 1; //английский
- else return 2;
- }
- public int Numb(char sym, char[] alphabet, int size)
- {
- for (int i = 0; i < size; i++)
- {
- if (alphabet[i] == sym)
- return i;
- }
- return 0;
- }
- public int Crypt(int lang)
- {
- int size_rus = rus.Length, size_eng = eng.Length, i = 0, k = 0;
- string text = "";
- if (lang == 0) text = System.IO.File.ReadAllText(@"text_eng.txt", Encoding.Default);
- if (lang == 1) text = System.IO.File.ReadAllText(@"text_rus.txt", Encoding.Default);
- char[] tmp = text.ToCharArray();
- if ((CheckLang(tmp[0]) == 0) || (CheckLang(tmp[1]) == 0) || (CheckLang(tmp[2]) == 0))
- {
- Console.WriteLine("Введите ключ на русском языке: ");
- string str = System.Console.ReadLine();
- char[] key = str.ToCharArray();
- for (int e = 0; e < text.Length; e++)
- {
- if ((rus.Contains(tmp[e]) == true))
- {
- k = (Numb(tmp[e], rus, size_rus) + Numb(key[i], rus, size_rus)) % size_rus;
- sym += rus[k];
- i = (i + 1) % key.Length;
- }
- else sym += tmp[e];
- }
- }
- else
- {
- Console.WriteLine("Введите ключ на английском языке: ");
- string str = System.Console.ReadLine();
- char[] key = str.ToCharArray();
- for (int e = 0; e < text.Length; e++)
- {
- if (eng.Contains(tmp[e]) == true)
- {
- k = (Numb(tmp[e], eng, size_eng) + Numb(key[i], eng, size_eng)) % size_eng;
- sym += eng[k];
- i = (i + 1) % key.Length;
- }
- else sym += tmp[e];
- }
- }
- Console.WriteLine(sym);
- Console.WriteLine();
- System.IO.File.WriteAllText(@"crypt.txt", sym, Encoding.Default);
- Console.WriteLine("Зашифровано");
- Console.WriteLine();
- sym = sym.Remove(0);
- return 1;
- }
- public int DeCrypt()
- {
- int size_rus = rus.Length, size_eng = eng.Length, i = 0, k = 0;
- string text = System.IO.File.ReadAllText(@"crypt.txt", Encoding.Default);
- char[] tmp = text.ToCharArray();
- if ((CheckLang(tmp[0]) == 0) || (CheckLang(tmp[1]) == 0) || (CheckLang(tmp[2]) == 0))
- {
- Console.WriteLine("Введите ключ на русском языке: ");
- string str = System.Console.ReadLine();
- char[] key = str.ToCharArray();
- for (int e = 0; e < text.Length; e++)
- {
- if ((rus.Contains(tmp[e]) == true))
- {
- k = (size_rus + Numb(tmp[e], rus, size_rus) - Numb(key[i], rus, size_rus)) % size_rus;
- sym += rus[k];
- i = (i + 1) % key.Length;
- }
- else sym += tmp[e];
- }
- }
- else
- {
- Console.WriteLine("Введите ключ на английском языке: ");
- string str = System.Console.ReadLine();
- char[] key = str.ToCharArray();
- for (int e = 0; e < text.Length; e++)
- {
- if (eng.Contains(tmp[e]) == true)
- {
- k = (size_eng + Numb(tmp[e], eng, size_eng) - Numb(key[i], eng, size_eng)) % size_eng;
- sym += eng[k];
- i = (i + 1) % key.Length;
- }
- else sym += tmp[e];
- }
- }
- System.IO.File.WriteAllText(@"resultf.txt", sym, Encoding.Default);
- Console.WriteLine(sym);
- Console.WriteLine();
- Console.WriteLine("Расшифровано");
- Console.WriteLine();
- sym = sym.Remove(0);
- return 1;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TI_Lab1
- {
- class Cryptanalysis
- {
- public string sym;
- char[] eng = {
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
- 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
- 'u', 'v', 'w', 'x', 'y', 'z'};
- char[] rus = {
- 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'з', 'ж', 'и',
- 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т',
- 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь',
- 'э', 'ю', 'я'
- };
- public Cryptanalysis()
- {
- sym = "";
- }
- public void setCryptanalysisSym(string temp)
- {
- sym = temp;
- }
- public string getCryptanalysisSym()
- {
- return sym;
- }
- public int Numb(char sym, char[] alphabet, int size)
- {
- for (int i = 0; i < size; i++)
- {
- if (alphabet[i] == sym)
- return i;
- }
- return 0;
- }
- int nod(int a, int b)
- {
- int i;
- for (i = a; i > 1; --i)
- if ((a % i == 0) && (b % i == 0))
- return i;
- return 1;
- }
- public int Kasiski()
- {
- int nl = 0, n = 50000;
- string text = System.IO.File.ReadAllText(@"crypt.txt", Encoding.Default);
- int[] nods = new int[n];
- char[] tmp = text.ToCharArray();
- for (int e = 0; e < text.Length; e++)
- {
- if ((rus.Contains(tmp[e]) == true) || (eng.Contains(tmp[e]) == true))
- sym += tmp[e];
- }
- string str1 = "", str2 = "";
- int[] l = new int[n];
- Console.WriteLine("-----------------------------------ЖДИТЕ------------------------------");
- for (int i = 0; i < sym.Length - 2; ++i)
- {
- str1 = sym[i].ToString() + sym[i + 1].ToString() + sym[i + 2].ToString();
- for (int j = i + 1; j < sym.Length - 2; ++j)
- {
- str2 = sym[j].ToString() + sym[j + 1].ToString() + sym[j + 2].ToString();
- if (String.Equals(str1, str2) == true)
- l[nl++] = j - i;
- }
- for (int z = 0; z < nl; ++z)
- for (int q = z + 1; q < nl; ++q)
- {
- nods[nod(l[z], l[q])]++;
- }
- for (int j = 0; j < nl; j++)
- {
- l[j] = 0;
- }
- nl = 0;
- // str2 = "";
- // str1 = "";
- }
- int keylen = 0;
- for (int i = 2; i < n; ++i)
- if (nods[keylen] < nods[i])
- keylen = i;
- Console.WriteLine("Длина ключа = {0}", keylen);
- return keylen;
- }
- public char[,] Matrix(int keylen)
- {
- int n = sym.Length / keylen + 1;
- char[] tmp = sym.ToCharArray();
- char[,] text = new char[n, keylen];
- for (int i = 0; i < sym.Length; i++)
- {
- text[i / keylen, i % keylen] = tmp[i];
- }
- return text;
- }
- public int CheckLang(char sm)
- {
- if (rus.Contains(sm) == true) return 0;//русский
- else if (eng.Contains(sm) == true) return 1; //английский
- else return 2;
- }
- public char[] FreqAnalysis(int keylen)
- {
- int n = sym.Length / keylen + 1;
- int freq_rus_O = 16, freq_eng_E = 5;
- char[] key = new char[keylen];
- int j = 0;
- char[,] text = Matrix(keylen);
- int e = 0;
- int[] alph_freq = new int[]
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0 };
- int size = 0;
- if (CheckLang(sym[0]) == 0) size = rus.Length;
- if (CheckLang(sym[0]) == 1) size = eng.Length;
- if (size == rus.Length)
- {
- for (int k = 0; k < keylen; k++)
- {
- for (int a = 0; a < size; a++)
- {
- for (int i = 0; i < n; i++)
- {
- if (rus[a] == text[i, j])
- alph_freq[a]++;
- }
- }
- int max = 0;
- int maxi = 0;
- for (int a = 0; a < size; a++)
- {
- if (max <= alph_freq[a])
- {
- max = alph_freq[a];
- maxi = a + 1;
- }
- }
- e = (maxi - freq_rus_O + size) % size;
- key[k] = rus[e];
- for (int a = 0; a < size; a++)
- alph_freq[a] = 0;
- e = 0;
- j = (j + 1) % keylen;
- }
- }
- if (size == eng.Length)
- {
- for (int k = 0; k < keylen; k++)
- {
- for (int a = 0; a < size; a++)
- {
- for (int i = 0; i < n; i++)
- {
- if (eng[a] == text[i, j])
- alph_freq[a]++;
- }
- }
- int max = 0;
- int maxi = 0;
- for (int a = 0; a < size; a++)
- {
- if (max <= alph_freq[a])
- {
- max = alph_freq[a];
- maxi = a + 1;
- }
- }
- e = (maxi - freq_eng_E + size) % size;
- key[k] = eng[e];
- for (int a = 0; a < size; a++)
- alph_freq[a] = 0;
- e = 0;
- j = (j + 1) % keylen;
- }
- }
- Console.WriteLine();
- Console.Write("Ключ --------->");
- for (int i = 0; i < keylen; i++)
- Console.Write(key[i]);
- return key;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement