A Logo

Feel free to include my content in your page via my
RSS feed

Help Irongeek.com pay for
bandwidth and research equipment:

Search Irongeek.com:

Irongeek Button
Social-engineer-training Button

Help Irongeek.com pay for bandwidth and research equipment:


Diffie-Hellman key exchange calculator

Dirty Diffie-Hellman
(Like dirty Santa, but geekier)

Crappy PHP script for a simple Diffie-Hellman key exchange calculator. I guess I could have used Javascript instead of PHP, but I had rounding errors.

Set these two for everyone

Alice Bob
a: b:
a = 5
A = ga mod p = 105 mod 541 = 456
b = 7
B = gb mod p = 107 mod 541 = 156
Alice and Bob exchange A and B in view of Carl
keya = B a mod p = 1565 mod 541 = 193
keyb = A B mod p = 4567 mod 541 = 193

     Hi all, the point of this game is to meet new people, and to learn about the Diffie-Hellman key exchange. Did you ever wonder how two parties can negotiate a cryptographic key in the presence of an observer, without the observer figuring out the key? My guess is not, but bear with me. This will be a simplified version of the Diffie-Hellman key exchange (in real life, better constants and larger variables should be chosen) , in the form of a game. Enter as many times as you like.

Fixed numbers: g=10, p=541

Contestant steps:

1. Find someone you do not know, and introduce yourself.

2. One of you is Alice (a), and one is Bob (b). If genders don't match that's ok, one of you can be Alan and the other Barb for all I care.

3. Both of you choose a number between 1 and 100, but don't tell the other person this number.

4. Alice, compute A = ga mod p = 10a mod 541.
Bob, compute B = gb mod p = 10b mod 541.
Feel free to rip out your calculator or smart phone, or just use this calculator:

5. Alice and Bob, exchange A and B verbally in the presences of Carl (Or as Chux0r points out, perhaps Christmas "Eve").

6. Alice, compute SecretKeyA = Ba mod p = Ba mod 541. Notice the superscript is the lower case variable you chose.
Bob, compute SecretKeyB = Ab mod p = Ab mod 541. Notice the superscript is the lower case variable you chose.

7. If you did it right, SecretKeyA should be the same as SecretKeyB. Write your names, the A and B values, and the shared SecretKey outcome on a piece of paper and turn it in for the drawing.


The officiator will draw a piece of paper and announce the two people, the values of their A and B, and then wait 20 sec. If someone else can announce Alice and Bob's shared SecretKey in the 20 secs, they win instead.

15 most recent posts on Irongeek.com:

If you would like to republish one of the articles from this site on your webpage or print journal please contact IronGeek.

Copyright 2016, IronGeek
Louisville / Kentuckiana Information Security Enthusiast