Skip to content

Commit 189082a

Browse files
save file
1 parent 1f26de5 commit 189082a

File tree

1 file changed

+155
-7
lines changed

1 file changed

+155
-7
lines changed

blog/25-12-19/diffie-hellman/diffie-hellman.html

Lines changed: 155 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787

8888

8989
.blog-hdr
90-
{color:blue}
90+
{color:blue;font-weight:bold}
9191

9292
.blog-text
9393
{margin:1.5rem auto;padding:1.25rem 1.75rem;background-color:#fcfcfc;/*#f9f9f9*/font-family:system-ui, sans-serif;font-size:1rem;line-height:2;
@@ -116,6 +116,9 @@
116116
.link-txt
117117
{}
118118

119+
ol li, ul li
120+
{margin:10px}
121+
119122
input
120123
{font-size:16px;padding:5px 7px;box-sizing:border-box;}
121124
input[type=button]
@@ -141,25 +144,170 @@ <h1 class=title>
141144
<div class=description>
142145

143146
<p>
144-
In this post, we’ll walk through a practical demonstration of the Diffie–Hellman key exchange algorithm implemented in JavaScript. Diffie–Hellman is a cornerstone of modern cryptography, allowing two parties to establish a shared secret over an insecure channel without ever transmitting the secret itself.
147+
In this post, we’ll walk through a practical demonstration of the Diffie–Hellman key exchange algorithm implemented in JavaScript.
148+
Diffie–Hellman is a cornerstone of modern cryptography, allowing two parties to establish a shared secret over an insecure channel
149+
without ever transmitting the secret itself.
150+
</p>
151+
152+
<p>
153+
We’ll explore how the algorithm works step by step — from generating public and private keys, to exchanging values, to deriving
154+
the same shared secret on both sides. Along the way, you’ll see how simple modular arithmetic underpins secure communication,
155+
and how JavaScript can be used to illustrate these concepts in code.
156+
</p>
157+
158+
<p>
159+
By the end, you’ll understand not only the theory behind Diffie–Hellman, but also how to implement it in practice,
160+
making abstract cryptographic ideas tangible and accessible.
161+
</p>
162+
163+
</div>
164+
165+
166+
167+
<div class=blog-text>
168+
169+
<div class=blog-hdr>
170+
Diffie–Hellman Key Exchange
171+
</div>
172+
173+
<p>
174+
The Diffie–Hellman algorithm is a method that allows two people to create a shared secret even if they’re talking over a completely
175+
insecure channel. That’s the magic of it: anyone can listen in on the conversation, but nobody except the two participants can figure
176+
out the secret they end up sharing.
177+
</p>
178+
179+
<ol>
180+
<li>
181+
<b>
182+
Both sides agree on two public numbers.
183+
</b>
184+
<br>
185+
These numbers don’t need to be secret — everyone in the world can see them.
186+
</li>
187+
<li>
188+
<b>
189+
Each person picks a private number.
190+
</b>
191+
<br>
192+
This is the only part that must stay hidden.
193+
</li>
194+
<li>
195+
<b>
196+
They each combine their private number with the public numbers
197+
</b>
198+
<br>
199+
Using a special kind of math (modular exponentiation). The result is a public value that they send to each other.
200+
</li>
201+
<li>
202+
<b>
203+
They each take the other person’s public value and combine it with their own private number.
204+
</b>
205+
<br>
206+
Because of the math involved, both sides end up with the exact same final number, even though they never sent that
207+
number over the network.
208+
</li>
209+
<li>
210+
<b>
211+
Anyone listening in sees only the public values
212+
</b>
213+
<br>
214+
but cannot reverse them to find the private numbers — that’s what makes the system secure.
215+
</li>
216+
</ol>
217+
218+
<p>
219+
The final shared number becomes a shared secret key, which can then be used to encrypt communication.
145220
</p>
221+
</div>
222+
223+
224+
<div class=blog-text>
225+
226+
<div class=blog-hdr>
227+
What Diffie–Hellman is used for
228+
</div>
229+
230+
<ul>
231+
<li>
232+
<b>
233+
Establishing secure connections
234+
</b>
235+
(HTTPS, SSH, VPNs, TLS).
236+
</li>
237+
<li>
238+
<b>
239+
Creating encryption keys on the fly
240+
</b>
241+
without pre‑sharing passwords.
242+
</li>
243+
<li>
244+
<b>
245+
Protecting against eavesdropping
246+
</b>
247+
, even on open networks.
248+
</li>
249+
<li>
250+
<b>
251+
Forward secrecy
252+
</b>
253+
, even if someone steals your long‑term keys later, they still can’t decrypt past conversations.
254+
</li>
255+
</ul>
146256

147257
<p>
148-
We’ll explore how the algorithm works step by step — from generating public and private keys, to exchanging values, to deriving the same shared secret on both sides. Along the way, you’ll see how simple modular arithmetic underpins secure communication, and how JavaScript can be used to illustrate these concepts in code.
258+
It’s one of the foundational building blocks of modern secure communication.
149259
</p>
150260

261+
</div>
262+
263+
264+
265+
<div class=blog-text>
266+
267+
<div class=blog-hdr>
268+
Why it’s secure
269+
</div>
270+
151271
<p>
152-
By the end, you’ll understand not only the theory behind Diffie–Hellman, but also how to implement it in practice, making abstract cryptographic ideas tangible and accessible.
272+
Diffie–Hellman relies on the difficulty of the discrete logarithm problem — a math problem that’s easy to compute in one direction
273+
but practically impossible to reverse. Even with powerful computers, guessing the private numbers from the public ones would take
274+
longer than the age of the universe.
153275
</p>
154276

155277
</div>
156278

157279

158-
<snippet-console component fullsize src='ex/diffie-hellman.js'></snippet-console>
280+
281+
<div class=blog-text>
282+
283+
<div class=blog-hdr>
284+
Other useful notes
285+
</div>
286+
287+
<ul>
288+
<li>
289+
Diffie–Hellman itself does not encrypt anything — it only creates the shared key.
290+
</li>
291+
<li>
292+
It’s often combined with other algorithms (like AES) to actually encrypt data.
293+
</li>
294+
<li>
295+
There are modern variants like Elliptic Curve Diffie–Hellman (ECDH), which provide the same security with much
296+
smaller numbers and faster performance.
297+
</li>
298+
<li>
299+
It was invented in 1976 and is considered one of the most important breakthroughs in cryptography.
300+
</li>
301+
</ul>
302+
303+
</div>
304+
305+
306+
<snippet-console id=ex1 component fullsize src='ex/diffie-hellman.js'></snippet-console>
159307

160308

161309

162-
<div>
310+
<div class=blog-text>
163311
<p>
164312
once a shared secret has been established this can be used to encrypt communications between alice and bob
165313
</p>
@@ -168,7 +316,7 @@ <h1 class=title>
168316
</p>
169317
</div>
170318

171-
<snippet-console component fullsize src='ex/tiny-encrypt.js'></snippet-console>
319+
<snippet-console id=ex2 component fullsize src='ex/tiny-encrypt.js'></snippet-console>
172320

173321

174322

0 commit comments

Comments
 (0)