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 GOST
- {
- class Program
- {
- public static int[][] mat = new int[8][];
- public static string To8(string s)
- {
- while (s.Length < 16)
- s = "0" + s ;
- return s;
- }
- public static string To4(string s)
- {
- while (s.Length < 4)
- s = "0" + s;
- return s;
- }
- public static string To32(string s)
- {
- while (s.Length < 32)
- s = "0" + s;
- return s;
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Введите исходное сообщение");
- string s = Console.ReadLine();
- char[] source = s.ToArray();
- Console.WriteLine("UNICODE");
- foreach (var item in source)
- {
- Console.WriteLine((int)item);
- }
- Console.WriteLine("HEX");
- foreach (var item in source)
- {
- Console.WriteLine(Convert.ToString(item, 2));
- }
- Console.WriteLine("Введите ключ");
- s = Console.ReadLine();
- char[] key = s.ToArray();
- Console.WriteLine("UNICODE");
- foreach (var item in key)
- {
- Console.WriteLine((int)item);
- }
- Console.WriteLine("HEX");
- foreach (var item in key)
- {
- Console.WriteLine(Convert.ToString(item, 2));
- }
- Console.ReadKey();
- string m1 = To8(Convert.ToString(source[2], 2));
- string m2 = To8(Convert.ToString(source[3], 2));
- string m3 = To8(Convert.ToString(key[0], 2));
- string m4 = To8(Convert.ToString(key[1], 2));
- Console.WriteLine(m1 + " " + m2 + " " + m3 + " " + m4);
- Console.ReadKey();
- string m12 = string.Format("{0}{1}", To8(m1), To8(m2));
- string m34 = string.Format("{0}{1}", To8(m3), To8(m4));
- Console.WriteLine("source + source | key + key // DigitFormat");
- Console.WriteLine(m12 + " " + m34);
- long mm12 = Convert.ToInt64(m12, 2);
- long mm34 = Convert.ToInt64(m34, 2);
- Console.WriteLine(mm12 + " " + mm34);
- string sum = Convert.ToString(Convert.ToInt32(m12, 2) + Convert.ToInt32(m34, 2), 2);
- Console.WriteLine(sum + " HEXFormat");
- Console.ReadKey();
- sum = To32(sum);
- List<int> R0 = new List<int>();
- mat[0] = new int[] { 4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3 };
- mat[1] = new int[] { 14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9 };
- mat[2] = new int[] { 5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11 };
- mat[3] = new int[] { 7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3 };
- mat[4] = new int[] { 6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2 };
- mat[5] = new int[] { 4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14 };
- mat[6] = new int[] { 13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12 };
- mat[7] = new int[] { 1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12 };
- for (int i = 0; i < sum.Length; i += 4)
- {
- Console.WriteLine(Convert.ToInt32(sum.Substring(i, 4), 2));
- R0.Add(mat[7 - i / 4][Convert.ToInt32(sum.Substring(i, 4), 2)]);
- }
- string sR0 = "";
- foreach (var item in R0)
- {
- sR0 += To4(Convert.ToString(item, 2));
- }
- Console.WriteLine("Преобразование по таблице: " + sR0);
- sR0 = sR0.Substring(11, sR0.Length-11) + sR0.Substring(0, 11);
- Console.WriteLine("Сдвиг на 11 бит");
- Console.ReadKey();
- int L0 = Convert.ToInt32(To8(Convert.ToString(source[0], 2)) + To8(Convert.ToString(source[1], 2)), 2);
- long R1 = Convert.ToInt64(sR0, 2);
- long answer = L0 ^ R1;
- Console.WriteLine("ОТВЕТ:" + To32(Convert.ToString(answer, 2)));
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement