r/programming • • Jan 16 '18

Cryptography: Diffie-Hellman key exchange explained intuitively using colors

https://youtu.be/YEBfamv-_do?t=2m18s
2.5k Upvotes

170 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jan 16 '18

Thanks for this, but I'm still lost. Here is what I did. Using this website, I followed your directions:

They agree on a public color and exchange that - Eve sees this

 AliceBob
PUBLICFFED00FFED00

They mix that public color with their secret private color and they exchange that, Eve sees this but doesn't know the mixed color (the private one)

 AliceBob
PUBLICFFED00FFED00
PRIVATEFF000000B500
MIXFF770080D100

They each take this color and then mix their private color with it

 AliceBob
PUBLICFFED00FFED00
PRIVATEFF000000B500
MIXFF770080D100
FINAL MIXFF4F0055C800

They both end up with two totally different colors. What am I doing wrong?

Thanks!

2

u/fjortisar Jan 16 '18

Alice would have Bob's MIX and Bob would have Alice's.

The final color is PRIVATE + MIX

However, this doesn't work either. I suspect it's because you're finding midpoints between two hex values (from a midpoint of two others), instead of actually mixing colors.

If you have some real paints, try that. But in essence you are correct. In the end Alice and Bob mix the same 3 colors, even though each of them only really know what 2 of the base colors are (public and their own private).

2

u/Sirflankalot Jan 16 '18

I think the problem is that your tool always does a 50/50 mix. Ultimately you want the resulting color to be 33/33/33 between Public/A Private/B Private. If you take the result from a 50/50 mix of Public and A Private and mix it with B private you will end up with a 25/25/50 mix, which will be the wrong balance. You can show it works by clicking on all three colors at the same just in a different order. It's not as rewarding, but that's what you're doing.

2

u/ReturningTarzan Jan 16 '18 edited Jan 16 '18

It's cause you're averaging colors instead of mixing them. That's not commutative. I.e. mix(mix(A,B),C) != mix(A,mix(B,C))

Try to define colors as vectors instead and the mixing as addition:

 AliceBob
SharedS=0,1,1S=0,1,1
PrivateA=3,1,0B=1,0,4
Shared+privateS+A=3,2,1S+B=1,1,5
Private+other party's (shared+private)A+(S+B)=4,2,5B+(S+A)=4,2,5

The bold items are information sent over the network: S, S+A and S+B. The only way to construct S+A+B from that is (S+A)+(S+B)-S, which involves subtraction, which is analogous to un-mixing colors, which is the part that's supposed to be impossible.

What actually makes it impossible in the real key exchange is the modular arithmetic, using large numbers and wrapping the result around if it overflows some smaller number, without recording how many times it overflowed. Try using one-dimensional colors and mixing by multiplication instead, along with a modulus of 99 so the mixing can't simply be reversed by division:

 AliceBob
SharedS=12S=12
PrivateA=71B=15
Shared*private MOD 99S*A=60S*B=81
(Privateother party's (sharedprivate MOD 99)) MOD 99A*(S*B)=9B*(S*A)=9

Similarly to the above, to derive the final key without any of the private keys you'd need to do (S*A)*(S*B)/S. But to do that you need to guess what part of the product of either S*A or S*B was discarded by the modulus. With large enough numbers that's a lot of bruteforcing.

Now, multiplication isn't actually that hard to reverse for a bunch of reasons, which is why the actual algorithm uses exponentiation instead and likes prime numbers and all that, but the idea of "mixing" with a commutative one-way function is the same.