简体   繁体   中英

How to use Diffie-Hellman key exchange to secure data transfer between a client and server?

I am a beginner programmer. I have been asked to secure data transfer between a client and a server by using a Diffie-Hellman key exchange. I have searched a lot on this issue, but I have just found some example codes that find big integers p and g .

The problem is I don't know how to use these numbers to secure a transfer of information. I would like to transfer "strings" over the connection between the client and the server, not integers. How can these numbers be useful to me? I'm approaching the deadline for this project for the course I`m taking and could really use some help.

I'd suggest reading and undertanding this: http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange

And here is a PHP code snippet that will help...

GENERATOR=2 and PRIME (300 digit prime) are constants

        // generate server secret
        $privateKey = 0;

        for ($i=0; $i<100; $i++) {
            if ($i==0) {
                $privateKey = mt_rand(1, 9);
            } else {
                $privateKey .= mt_rand(0, 9);
            }
        }

        // output server public key
        echo gmp_strval(gmp_powm(GENERATOR, $privateKey, PRIME));

        // calculate server secret key
        $secretKey = md5(
            gmp_strval(
                gmp_powm($_POST['public_key'], $privateKey, PRIME)
            )
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM