Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Numerics;
- using System.Text;
- using System.Security.Cryptography;
- namespace methods
- {
- class Program
- {
- public static BigInteger x, y, g, n, X_new, Y_new;
- public static String g_str, n_str, X_str, Y_str;
- public static int l1, l2;
- static BigInteger Pow(BigInteger a, BigInteger b)
- {
- BigInteger result = 1;
- for (BigInteger i = 0; i < b; i++)
- result *= a;
- return result;
- }
- static BigInteger GenerateNumberOfLength(int l)
- {
- RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
- byte[] _result = new byte[BigInteger.Pow(2, l).ToByteArray().LongLength];
- BigInteger result;
- do
- {
- rng.GetBytes(_result);
- result = new BigInteger(_result);
- } while (result <= BigInteger.Pow(2, (l - 1)) || result >= BigInteger.Pow(2, l));
- return result;
- }
- private static void FindNumberX(int l)
- {
- x = GenerateNumberOfLength(l);
- }
- private static void FindNumberY(int l)
- {
- y = GenerateNumberOfLength(l);
- }
- static void Main(string[] args)
- {
- using (FileStream In = File.OpenRead("g.txt"))
- {
- byte[] array = new byte[In.Length];
- In.Read(array, 0, array.Length);
- g_str = System.Text.Encoding.Default.GetString(array);
- }
- using (FileStream In1 = File.OpenRead("n.txt"))
- {
- byte[] array1 = new byte[In1.Length];
- In1.Read(array1, 0, array1.Length);
- n_str = System.Text.Encoding.Default.GetString(array1);
- }
- g = BigInteger.Parse(g_str);
- n = BigInteger.Parse(n_str);
- Random rnd = new Random();
- l1 = rnd.Next(3, 50);
- l2 = rnd.Next(3, 50);
- FindNumberX(l1);
- Console.WriteLine("А сгенерировал значение х");
- Console.ReadKey();
- FindNumberY(l2);
- Console.WriteLine("Б сгенерировал значение у");
- Console.ReadKey();
- StreamWriter Out3 = null, Out4 = null;
- try
- {
- Out3 = new StreamWriter(File.Open("x.txt", FileMode.Create));
- Out4 = new StreamWriter(File.Open("y.txt", FileMode.Create));
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- try
- {
- Out3.WriteLine(x);
- Out4.WriteLine(y);
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- Out3.Close();
- Out4.Close();
- BigInteger X = BigInteger.ModPow(g, x, n);
- BigInteger Y = BigInteger.ModPow(g, y, n);
- StreamWriter Out = null, Out1 = null;
- try
- {
- Out = new StreamWriter(File.Open("XforB.txt", FileMode.Create));
- Out1 = new StreamWriter(File.Open("YforA.txt", FileMode.Create));
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- try
- {
- Out.WriteLine(X);
- Console.WriteLine("А переслал Б значение Х");
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- Out.Close();
- Console.ReadKey();
- try
- {
- Out1.WriteLine(Y);
- Console.WriteLine("Б переслал А значение У");
- }
- catch (IOException e)
- {
- Console.WriteLine(e.Message);
- }
- Out1.Close();
- Console.ReadKey();
- using (FileStream In2 = File.OpenRead("XforB.txt"))
- {
- byte[] array = new byte[In2.Length];
- In2.Read(array, 0, array.Length);
- X_str = System.Text.Encoding.Default.GetString(array);
- }
- using (FileStream In3 = File.OpenRead("YforA.txt"))
- {
- byte[] array1 = new byte[In3.Length];
- In3.Read(array1, 0, array1.Length);
- Y_str = System.Text.Encoding.Default.GetString(array1);
- }
- X_new = BigInteger.Parse(X_str);
- Y_new = BigInteger.Parse(Y_str);
- BigInteger K_A = BigInteger.ModPow(Y_new, x, n);
- BigInteger K_B = BigInteger.ModPow(X_new, y, n);
- if (K_A == K_B)
- Console.WriteLine("Сгенерирован корректный секретный ключ для связи А и Б");
- else
- Console.WriteLine("На каком-то этапе произошла ошибка");
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement