Diffie-Hellman is one of the oldest public-key cryptosystems. It is often called a key-agreement protocol, because it allows the establishment of a common secret key, that can be used to encrypt and decrypt messages.
Generate DH public key parameters
Generate the DH public key parameters and save them to a file (this may take a while to complete depending on the key size - you need to be patient).
Using the DH public parameters that were created in the previous step, it is now necessary to create the public and private keys of both entities that need to communicate (again Alice and Bob).
So, both Alice and Bob, need to have the dhparams.pem file created in the previous step.
Now, Alice can do the following to create its private key:
And you can look at the content of the files... using hexdump utility.
hexdump -C alice_common_key.dh
Here are the contents of the key:
00000000 c7 5f cf 8d 1e 00 6f 54 7b cc 7d 0c 85 d0 99 15 |�_�...oT{�}..�..|
00000010 78 47 bc 32 fa 09 67 73 15 b5 9e 42 e6 e0 13 3c |xG�2�.gs.�.B��.<|
00000020 d7 d3 1c 17 a1 ef 45 1f 4b 2f 65 b0 d0 87 f9 d0 |��..��E.K/e��.��|
00000030 2e 99 fe 66 91 fe e3 c1 01 2a b0 e2 d5 e1 a7 4e |..�f.���.*����N|
00000040 b8 8c 97 e1 c4 ec 29 58 41 06 77 50 f4 95 23 fb |�..���)XA.wP�.#�|
00000050 13 29 01 43 22 05 8a db 38 1a f8 21 30 8c f8 f7 |.).C"..�8.�!0.��|
00000060 22 25 7b 88 89 7a 05 0c b5 fa 30 ce 61 2c c6 e4 |"%{..z..��0�a,��|
00000070 24 ca 23 e2 78 05 13 b3 2e 13 a9 2c e4 19 d7 eb |$�#�x..�..�,�.��|
00000080 be 8a 61 b4 d1 20 df ed 9e a2 31 89 4b b0 d5 6b |�.a�� ��.�1.K��k|
00000090 ac d8 8a 01 46 1a cc b9 fc aa e0 68 6d 82 35 52 |��..F.̹��hm.5R|
000000a0 45 c5 10 6d bd 07 ac a1 a9 4f 3f 23 12 e3 d8 4e |E�.m�.���O?#.��N|
000000b0 0e e9 18 04 68 da b4 93 5d e0 86 c0 ad 1f d6 fc |.�..hڴ.]�.��.��|
000000c0 d7 e1 70 64 77 96 6b 83 57 77 47 5a 60 1d 45 92 |��pdw.k.WwGZ`.E.|
000000d0 70 46 67 4d 3a f8 64 d7 ad f9 db a4 0d 92 cf e6 |pFgM:�d�ۤ..��|
000000e0 d0 43 86 be b6 2e 65 08 95 dc 7d d9 6d 0c 03 b9 |�C.��.e..�}�m..�|
000000f0 cb 2a 46 5e 6a 78 36 2f 51 ff ba b5 7a 17 45 33 |�*F^jx6/Q���z.E3|
00000100
As an alternative for the creation of keys we could use the following examples. In these examples, we create the common DH key and after that we create an SHA-256 hash of the key. This way, we end-up with a raw 256-bit key that can be used directly. This key is store on the alice_common_key.dh and bob_common_key.dh.
This key can be used as secret key to cipher and decipher data, as we can see in the next section.
Encrypt and decrypt with DH
Now that we have a common key between Alice and Bob, this key can be used to do symmetric cryptography between the two parties communicating. Consider the plain.txt and cipher.txt below, two examples of files that are going to be encrypted or decrypted.