From the course: Symmetric Cryptography Essential Training

Unlock the full course today

Join today to access over 24,100 courses taught by industry experts.

Solution: Implementing Diffie–Hellman

Solution: Implementing Diffie–Hellman

(upbeat music) - This is the chapter six solution. As usual, I'm using C++ for this solution, but yours might look similar in some other language. I'm doing this all in main, but I can see other folks writing functions to do different parts of this. At the top, I have a bunch of variables initialized, including the required "m" and "g". And then right afterwards I do a bit of magic with C++'s true random number generator to generate numbers between three and twelve. Any other sort of random number generation works here for this problem, or just hard code the values if you like. Now we're just doing the math using the "pow" function from the "cmath" library, raising things to powers. Unfortunately, this function returns a floating point number, which we have to cast back into an integer, otherwise the modular operator won't work. And we do the comparison at the end and we celebrate. Now, let's run the code and see what this looks like. I'm going to run it once, and we find out that…

Contents