{"id":166,"date":"2026-11-13T12:33:19","date_gmt":"2026-11-13T04:33:19","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-diffie-hellman-agrees-on-a-shared-secret\/"},"modified":"2026-11-13T21:51:40","modified_gmt":"2026-11-13T13:51:40","slug":"how-diffie-hellman-agrees-on-a-shared-secret","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-diffie-hellman-agrees-on-a-shared-secret\/","title":{"rendered":"How Diffie-Hellman agrees on a shared secret"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Diffie-Hellman<\/strong> lets two parties derive the same secret from a public generator. They agree on a group. Each party samples a secret exponent, sends the generator raised to that exponent, and raises the value it receives to its own exponent. Both compute g raised to the product of the exponents. An observer who sees the two transmitted powers is not given an efficient way to recover that product power. The exchange does not name the peer. Authentication is a separate signature or a pre-shared key.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Two machines that have never shared a secret need one, and the path between them is readable. Sending the secret itself exposes it. Diffie-Hellman sends powers instead. In a multiplicative group modulo a prime, or in an elliptic-curve group, exponentiation is easy and the inverse discrete logarithm is the hard problem the construction relies on. Both parties can finish the arithmetic. A listener who sees only the public parameters and the two powers cannot run the same last step.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The listener is not the only other party on a network. Nothing in the four values says who chose the exponent. A caller who needs the secret to be shared with one named peer has to bind the transmitted powers to identities. The binding is not part of the arithmetic.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">If both parties use the same group and the same generator, Alice&#8217;s result B^a and Bob&#8217;s result A^b are the same group element g^{ab}. The computation is two exponentiations per party, polynomial in the bit length of the group. The public values are the group parameters, A, and B. The secrets a and b stay local.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The equality is algebraic and says nothing about who sent A. A deployment that must reject a substituted power checks a signature on the power, or uses a pre-shared key in an authenticated key-exchange construction. The raw group element is then passed through a key-derivation function before it is used as key bytes. A received element is accepted only when it lies in the intended subgroup, because the protocol is defined for that subgroup.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose a public group and a generator g of a large prime-order subgroup. Named groups are the usual choice.<\/li>\n<li>Alice samples a secret exponent a and sends A = g^a. Bob samples b and sends B = g^b.<\/li>\n<li>Each side checks that the received element is in the intended subgroup.<\/li>\n<li>Alice computes B^a. Bob computes A^b. Both results equal g^{ab}.<\/li>\n<li>Feed the shared element through a key-derivation function to obtain key bytes. Discard a and b when the session no longer needs them.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The construction yields a shared group element under the hardness of the Diffie-Hellman problem in that group. It does not identify the peer. RSA is a different public-key operation, built from exponents modulo a composite. Diffie-Hellman is the shared-exponent agreement.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the received element skips the subgroup check<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Raise any received bit string to the local exponent and call the result the session secret<\/td>\n<td>The protocol is defined for an element of the chosen subgroup. A value outside that subgroup is not an input the agreement computes on.<\/td>\n<\/tr>\n<tr>\n<td>Send the exponent instead of the power<\/td>\n<td>The exponent is the secret. The power is the value the other party can combine. They are not interchangeable.<\/td>\n<\/tr>\n<tr>\n<td>Use the raw group element as a symmetric key<\/td>\n<td>Key bytes come from a key-derivation function applied to the element. The element itself has structure the cipher key is not supposed to carry.<\/td>\n<\/tr>\n<tr>\n<td>Treat a matching secret as proof of the peer&#8217;s identity<\/td>\n<td>Anyone who can send a power can finish the arithmetic with the receiver. Identity comes from a signature or a pre-shared key bound to the exchange.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A public exponent modulo a composite is RSA. An authenticated handshake adds a signature or a pre-shared key around this exchange. Diffie-Hellman is the unauthenticated agreement on g^{ab}.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment agrees on g raised to the product of two secret exponents. The public prime is 23 and the generator is 5. Secrets 6 and 15 produce the shared residue <code>2<\/code>.<\/p>\n\n<pre><code class=\"language-python\">p, g = 23, 5\na, b = 6, 15\nA, B = pow(g, a, p), pow(g, b, p)\nshared_a, shared_b = pow(B, a, p), pow(A, b, p)\nassert shared_a == shared_b == pow(g, a * b, p)\nprint(shared_a)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>TLS<\/strong> and <strong>SSH<\/strong> agree on a session key with Diffie-Hellman, historically in the multiplicative group and now mostly as elliptic-curve Diffie-Hellman or X25519. IKE uses the same agreement. RSA encrypts a key with a public exponent; it does not perform this handshake.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the bare exchange when the peer must be identified. Use an authenticated key-exchange construction. Keep the exponent arithmetic as the agreement step inside that construction, on a group whose discrete log is the one the deployment chose.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Diffie-Hellman returns the two powers, the shared value g to the product of the exponents, and the subgroup check. <strong>Diffie-Hellman agrees on g raised to the product of two secret exponents by exchanging g to each exponent, and the exchange by itself does not identify the peer.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Diffie-Hellman lets two parties derive the same secret from a public generator. They agree on a group. Each party samples a secret exponent, sends the generator raised to that exponent, and raises the value it receives to its own exponent. Both compute g raised to the product&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":565,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions\/565"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}