Four expressions of key exchange in DH mode

1. Theoretical expression
1. Alice and Bob determine two large prime numbers p and g, which do not need to be kept secret
2. Alice chooses another large random number a and calculates A as follows: A=ga mod p
3. Alice sends A to Bob
4. Bob chooses another large random number b and calculates B as follows: B=gb mod p
5. Bob sends B to Alice
6. Calculate the secret key K1 as follows: K1=Ba mod p
7. Calculate the secret key K2 as follows: K2=Ab mod p
K1=K2, so Alice and Bob can use it for encryption and decryption
 
The way of digital expression
1. The communication party Alice and the communication party Bob agree that g=5, p=23, and g and p are public
2. Alice generates a random number a, a is kept secret, such as a=6
3. Alice calculates g^a%p and sends it to Bob, g^a%p=5^6%23=8
4. Bob generates a random number b, b is kept secret, such as b=15
5. Bob calculates g^b%p and sends it to Alice, g^b%p=5^15%23=19
6. After Alice receives g^b%p, she uses the secret a to calculate (g^b%p)^a%p=19^6%23=2
7. After Bob receives g^a%p, he uses the secret b to calculate (g^a%p)^b%p=8^15%23=2
8. In this way, the communicating parties Alice and Bob get the same key: 2
 
3. The practical way of expression
1. Server Key Exchange message
Server side: use DH algorithm to generate integers p, g, pubkey (ie A=ga mod p ), and put these three parameters in the Server Key Exchange message. In order to prevent the message from being maliciously tampered with, the Server Key exchange message also contains a signature for the public key parameters.
Client: After receiving this message, it will  record the three parameters of p, g, pubkey (ie A=ga mod p ) in the message. With these, it can be based on pubkey (ie A=ga mod p ) and private (ie b ) pre-master is calculated .
 
2. Client Key Exchange message
Client: Put the generated pubkey (ie B=gb mod p )  into the Client Key Exchange message and send it to the server.
Server side: After receiving this message, it will record the pubkey (ie B=gb mod p ) in the message, and calculate the pre-master  based on pubkey (ie B=gb mod p ) and private (ie a ) .
 
4. Graphical expression

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326404579&siteId=291194637