TLS Handshake Explained — Tech Blog

By hackeraju

A concise, copy-paste-ready blog page that explains the TLS handshake and embeds runnable code examples (for demonstration). Includes a small visual diagram and copy buttons for each snippet so your readers can try examples quickly.

What is TLS (short)

TLS (Transport Layer Security) is the modern protocol that secures HTTP connections (you know it as HTTPS). The handshake is the initial exchange where client and server agree on versions, cipher suites, and derive shared secrets for encryption.

Handshake diagram

Quick analogy

Imagine two people meeting: they say hello, show ID, verify identity with a trusted third party, agree a secret handshake, and then speak in private. That sequence is the TLS handshake.

Code examples

Click the Copy button to copy each snippet. These are for demonstration — do not use the DH demo for production cryptography.

1) Observe a real TLS connection (Python)

import socket, ssl

hostname = 'www.google.com'
port = 443

context = ssl.create_default_context()

with socket.create_connection((hostname, port)) as sock:
    with context.wrap_socket(sock, server_hostname=hostname) as secure_sock:
        print(f"Connected to {hostname} with {secure_sock.version()}")
        print("Cipher used:", secure_sock.cipher())
        print("Server certificate:", secure_sock.getpeercert())

2) Simple Diffie-Hellman demo (educational)

# Educational DH demo — not secure for production
import random

P = 23  # small prime for demo
G = 5

a = random.randint(1, P-1)
A = pow(G, a, P)

b = random.randint(1, P-1)
B = pow(G, b, P)

client_secret = pow(B, a, P)
server_secret = pow(A, b, P)

print("Client public key:", A)
print("Server public key:", B)
print("Shared secret (client):", client_secret)
print("Shared secret (server):", server_secret)

3) Use OpenSSL to inspect handshake

openssl s_client -connect www.google.com:443

Tip: Show these snippets in your blog and explain the output line-by-line for readers who are new to networking.

How to use this HTML

  1. Copy the full HTML file into index.html.
  2. Create a style.css file for CSS and a script.js file for JavaScript.
  3. Open index.html in your browser to preview the page.
  4. Host it on GitHub Pages, Netlify, or Vercel to publish quickly.