· 6 years ago · Oct 06, 2019, 02:02 PM
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace DiffieHellmanProtocol
8{
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 // Console.WriteLine("DIFFIE HELMLMAN ");
14 Protocol Program = new Protocol();
15 Program.GetG();
16 }
17 class Protocol
18 {
19 private int RandomCommonNumberP;
20 private int RandomCommonNumberG;
21 private double OpenKeyFromA;
22 private double OpenKeyFromB;
23 private int SecretKeyFromA;
24 private int SecretKeyFromB;
25 public void GetG()
26 {
27 start:
28 // Console.WriteLine("VALUE OF P:");
29 int entering = 23;
30
31 RandomCommonNumberP = entering;
32
33 int G = 5;
34 RandomCommonNumberG = G;
35
36 SideASecretKeyGenerate();
37
38
39 }
40 private void SideASecretKeyGenerate()
41 {
42 Console.WriteLine("value of A");
43 int secretKey = Convert.ToInt32(Console.ReadLine());
44 SecretKeyFromA = secretKey;
45 double openKey = Math.Pow(RandomCommonNumberG, secretKey) % RandomCommonNumberP;
46 OpenKeyFromA = openKey;
47 Console.WriteLine();
48 SideBSecretKeyGenerate();
49 }
50 private void SideBSecretKeyGenerate()
51 {
52 Console.WriteLine("value for B");
53 int secretKey = Convert.ToInt32(Console.ReadLine());
54 SecretKeyFromB = secretKey;
55 double openKey = Math.Pow(RandomCommonNumberG, secretKey)%RandomCommonNumberP;
56 OpenKeyFromB = openKey;
57 Console.WriteLine();
58 execution();
59 }
60 private void execution()
61 {
62 Console.ForegroundColor = ConsoleColor.Green;
63 Console.WriteLine(OpenKeyFromA+" "+OpenKeyFromB);
64 Console.ForegroundColor = ConsoleColor.DarkYellow;
65 Console.WriteLine(SecretKeyFromA+" "+SecretKeyFromB);
66 Console.ResetColor();
67
68 Console.WriteLine("ПРОВЕРКА "+"A-"+OpenKeyFromA+" b-"+SecretKeyFromB+" P-"+RandomCommonNumberP+" K="+Math.Pow(OpenKeyFromA,SecretKeyFromB)%RandomCommonNumberP);
69 //СТРОЧКОЙ ВЫШЕ КЛЮЧ ПОЛУЧЕННЫЙ НА СТОРОНЕ B И ОН НЕ ВЕРНЫЙ ,В ОСТАЛЬНОМ ВСЁ ПРАВИЛЬНО,ХОТЯ НЕ ФАКТ ПРОВЕРИТЬ ОТКРЫТЫЙ КЛЮЧ ОТ А
70 Console.WriteLine("ПРОВЕРКА " + "A-" + OpenKeyFromB + " b-" + SecretKeyFromA + " P-" + RandomCommonNumberP + " K=" + Math.Pow(OpenKeyFromB, SecretKeyFromA) % RandomCommonNumberP);
71 //Console.WriteLine(Math.Pow(OpenKeyFromA, SecretKeyFromB)%RandomCommonNumberP+" "+ Math.Pow(OpenKeyFromB, SecretKeyFromA)%RandomCommonNumberP);
72 Console.ReadLine();
73
74 Console.WriteLine("Мы получили открытые ключи от стороны A "+OpenKeyFromA+" и стороны B "+ OpenKeyFromB+"\n Собеседники обменялись этими ключами");
75 Console.ForegroundColor = ConsoleColor.DarkGreen;
76 Console.WriteLine("Сторона A вы приняли открытый ключ от стороны B и ,преобразовав его по формуле K=B ^ a % p" +
77 ",где a ваш секретный ключ,B открытый ключ от стороны B , получили \n "+Math.Pow(OpenKeyFromB,SecretKeyFromA) % RandomCommonNumberP +"-Общий ключ шифрования");
78 Console.ForegroundColor = ConsoleColor.DarkMagenta;
79
80 Console.WriteLine("Сторона B вы приняли открытый ключ от стороны A и ,преобразовав его по формуле K= A ^ b % p " +
81 ",где b ваш секретный ключ ,A открытый ключ от стороны A , получили \n "+Math.Pow(OpenKeyFromA,SecretKeyFromB) % RandomCommonNumberP+"-Общий ключ шифрования");
82 Console.ReadKey();
83 }
84
85 }
86
87
88
89 }
90}