Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Numerics;
  6. using System.Text;
  7. using System.Security.Cryptography;
  8.  
  9. namespace methods
  10. {
  11.     class Program
  12.     {
  13.         public static BigInteger x, y, g, n, X_new, Y_new;
  14.         public static String g_str, n_str, X_str, Y_str;
  15.         public static int l1, l2;
  16.            
  17.         static BigInteger Pow(BigInteger a, BigInteger b)
  18.         {
  19.             BigInteger result = 1;
  20.             for (BigInteger i = 0; i < b; i++)
  21.                 result *= a;
  22.        
  23.             return result;
  24.         }
  25.  
  26.         static BigInteger GenerateNumberOfLength(int l)
  27.         {
  28.             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  29.             byte[] _result = new byte[BigInteger.Pow(2, l).ToByteArray().LongLength];
  30.             BigInteger result;
  31.             do
  32.             {
  33.                 rng.GetBytes(_result);
  34.                 result = new BigInteger(_result);
  35.             } while (result <= BigInteger.Pow(2, (l - 1)) || result >= BigInteger.Pow(2, l));
  36.            
  37.             return result;
  38.         }
  39.  
  40.         private static void FindNumberX(int l)
  41.         {
  42.             x = GenerateNumberOfLength(l);
  43.         }
  44.        
  45.         private static void FindNumberY(int l)
  46.         {
  47.             y = GenerateNumberOfLength(l);
  48.         }
  49.  
  50.         static void Main(string[] args)
  51.         {
  52.             using (FileStream In = File.OpenRead("g.txt"))
  53.             {
  54.                 byte[] array = new byte[In.Length];
  55.                 In.Read(array, 0, array.Length);
  56.                 g_str = System.Text.Encoding.Default.GetString(array);
  57.             }
  58.  
  59.             using (FileStream In1 = File.OpenRead("n.txt"))
  60.             {
  61.                 byte[] array1 = new byte[In1.Length];
  62.                 In1.Read(array1, 0, array1.Length);
  63.                 n_str = System.Text.Encoding.Default.GetString(array1);
  64.             }
  65.            
  66.             g = BigInteger.Parse(g_str);
  67.             n = BigInteger.Parse(n_str);
  68.            
  69.             Random rnd = new Random();
  70.             l1 = rnd.Next(3, 50);
  71.             l2 = rnd.Next(3, 50);
  72.  
  73.             FindNumberX(l1);
  74.             Console.WriteLine("А сгенерировал значение х");
  75.             Console.ReadKey();
  76.             FindNumberY(l2);
  77.             Console.WriteLine("Б сгенерировал значение у");
  78.             Console.ReadKey();
  79.            
  80.             StreamWriter Out3 = null, Out4 = null;
  81.             try
  82.             {
  83.                 Out3 = new StreamWriter(File.Open("x.txt", FileMode.Create));
  84.                 Out4 = new StreamWriter(File.Open("y.txt", FileMode.Create));
  85.             }
  86.             catch (IOException e)
  87.             {
  88.                 Console.WriteLine(e.Message);
  89.             }
  90.            
  91.             try
  92.             {
  93.                 Out3.WriteLine(x);
  94.                 Out4.WriteLine(y);
  95.             }
  96.             catch (IOException e)
  97.             {
  98.                 Console.WriteLine(e.Message);
  99.             }
  100.            
  101.             Out3.Close();
  102.             Out4.Close();
  103.            
  104.             BigInteger X = BigInteger.ModPow(g, x, n);
  105.             BigInteger Y = BigInteger.ModPow(g, y, n);
  106.            
  107.             StreamWriter Out = null, Out1 = null;
  108.             try
  109.             {
  110.                 Out = new StreamWriter(File.Open("XforB.txt", FileMode.Create));
  111.                 Out1 = new StreamWriter(File.Open("YforA.txt", FileMode.Create));
  112.             }
  113.             catch (IOException e)
  114.             {
  115.                 Console.WriteLine(e.Message);
  116.             }
  117.            
  118.             try
  119.             {
  120.                 Out.WriteLine(X);
  121.                 Console.WriteLine("А переслал Б значение Х");
  122.             }
  123.             catch (IOException e)
  124.             {
  125.                 Console.WriteLine(e.Message);
  126.             }
  127.  
  128.             Out.Close();
  129.             Console.ReadKey();
  130.  
  131.             try
  132.             {
  133.                 Out1.WriteLine(Y);
  134.                 Console.WriteLine("Б переслал А значение У");
  135.             }
  136.             catch (IOException e)
  137.             {
  138.                 Console.WriteLine(e.Message);
  139.             }
  140.  
  141.             Out1.Close();
  142.             Console.ReadKey();
  143.        
  144.             using (FileStream In2 = File.OpenRead("XforB.txt"))
  145.             {
  146.                 byte[] array = new byte[In2.Length];
  147.                 In2.Read(array, 0, array.Length);
  148.                 X_str = System.Text.Encoding.Default.GetString(array);
  149.             }
  150.            
  151.             using (FileStream In3 = File.OpenRead("YforA.txt"))
  152.             {
  153.                 byte[] array1 = new byte[In3.Length];
  154.                 In3.Read(array1, 0, array1.Length);
  155.                 Y_str = System.Text.Encoding.Default.GetString(array1);
  156.             }
  157.            
  158.             X_new = BigInteger.Parse(X_str);
  159.             Y_new = BigInteger.Parse(Y_str);
  160.            
  161.             BigInteger K_A = BigInteger.ModPow(Y_new, x, n);
  162.             BigInteger K_B = BigInteger.ModPow(X_new, y, n);
  163.            
  164.             if (K_A == K_B)
  165.                 Console.WriteLine("Сгенерирован корректный секретный ключ для связи А и Б");
  166.             else
  167.                 Console.WriteLine("На каком-то этапе произошла ошибка");
  168.             Console.Read();
  169.         }
  170.        
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement