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.67 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.             StreamReader In = null, In1 = null;
  53.             try
  54.             {
  55.                 In = new StreamReader(File.Open("g.txt", FileMode.Create));
  56.                 byte[] array = new byte[In.Length];
  57.                 In.Read(array, 0, array.Length);
  58.                 g_str = System.Text.Encoding.Default.GetString(array);
  59.                
  60.                 In1 = new StreamReader(File.Open("n.txt", FileMode.Create));
  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.             catch (IOException e)
  66.             {
  67.                 Console.WriteLine(e.Message);
  68.             }
  69.            
  70.             In.Close();
  71.             In1.Close();
  72.            
  73.             g = BigInteger.Parse(g_str);
  74.             n = BigInteger.Parse(n_str);
  75.            
  76.             Random rnd = new Random();
  77.             l1 = rnd.Next();
  78.             l2 = rnd.Next();
  79.            
  80.             FindNumberX(l1);
  81.             Console.WriteLine("А сгенерировал значение х");
  82.             FindNumberY(l2);
  83.             Console.WriteLine("Б сгенерировал значение у");
  84.            
  85.             StreamWriter Out3 = null, Out4 = null;
  86.             try
  87.             {
  88.                 Out3 = new StreamWriter(File.Open("x.txt", FileMode.Create));
  89.                 Out4 = new StreamWriter(File.Open("y.txt", FileMode.Create));
  90.             }
  91.             catch (IOException e)
  92.             {
  93.                 Console.WriteLine(e.Message);
  94.             }
  95.            
  96.             try
  97.             {
  98.                 Out3.WriteLine(x);
  99.                 Out4.WriteLine(y);
  100.             }
  101.             catch (IOException e)
  102.             {
  103.                 Console.WriteLine(e.Message);
  104.             }
  105.            
  106.             Out3.Close();
  107.             Out4.Close();
  108.            
  109.             BigInteger X = BigInteger.ModPow(g, x, n);
  110.             BigInteger Y = BigInteger.ModPow(g, y, n);
  111.            
  112.             StreamWriter Out = null, Out1 = null;
  113.             try
  114.             {
  115.                 Out = new StreamWriter(File.Open("X.txt", FileMode.Create));
  116.                 Out1 = new StreamWriter(File.Open("Y.txt", FileMode.Create));
  117.             }
  118.             catch (IOException e)
  119.             {
  120.                 Console.WriteLine(e.Message);
  121.             }
  122.            
  123.             try
  124.             {
  125.                 Out.WriteLine(X);
  126.                 Console.WriteLine("А переслал Б значение Х");
  127.                 Out1.WriteLine(Y);
  128.                 Console.WriteLine("Б переслал А значение У");
  129.             }
  130.             catch (IOException e)
  131.             {
  132.                 Console.WriteLine(e.Message);
  133.             }
  134.            
  135.             Out.Close();
  136.             Out1.Close();
  137.            
  138.             StreamReader In2 = null, In3 = null;
  139.             try
  140.             {
  141.                 In2 = new StreamReader(File.Open("X.txt", FileMode.Create));
  142.                 byte[] array = new byte[In2.Length];
  143.                 In2.Read(array, 0, array.Length);
  144.                 X_str = System.Text.Encoding.Default.GetString(array);
  145.                
  146.                 In3 = new StreamReader(File.Open("Y.txt", FileMode.Create));
  147.                 byte[] array1 = new byte[In3.Length];
  148.                 In3.Read(array1, 0, array1.Length);
  149.                 Y_str = System.Text.Encoding.Default.GetString(array1);
  150.             }
  151.             catch (IOException e)
  152.             {
  153.                 Console.WriteLine(e.Message);
  154.             }
  155.            
  156.             In2.Close();
  157.             In3.Close();
  158.            
  159.             X_new = BigInteger.Parse(X_str);
  160.             Y_new = BigInteger.Parse(Y_str);
  161.            
  162.             BigInteger K_A = BigInteger.ModPow(Y_new, x, n);
  163.             BigInteger K_B = BigInteger.ModPow(X_new, y, n);
  164.            
  165.             if (K_A == K_B)
  166.                 Console.WriteLine("Сгенерирован корректный секретный ключ для связи А и Б");
  167.             else
  168.                 Console.WriteLine("На каком-то этапе произошла ошибка");
  169.             Console.Read();
  170.         }
  171.        
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement