# HTTPS & TLS

> How the web secures communication using Transport Layer Security (TLS) to encrypt HTTP requests and responses.

Subject: Internet | Lesson 13 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/https-and-tls
Previous: [HTTP Headers](https://codewithanimation.in/internet/http-headers.md) | Next: [What is an API?](https://codewithanimation.in/internet/what-is-an-api.md)

## What it is

**HTTPS** (Hypertext Transfer Protocol Secure) is an extension of HTTP. It secures plain-text HTTP messages by encrypting them using a cryptographic protocol called **TLS** (Transport Layer Security). It ensures privacy, data integrity, and server authentication.

## Why it matters

Without HTTPS, anyone sitting between you and the server (like your ISP or a hacker on public Wi-Fi) can read everything you send, including passwords and cookies. HTTPS is also mandatory for modern browser features (Geolocation, Service Workers, camera access) and heavily boosts SEO.

## How it works

Before any HTTP data is sent, the client and server perform a **TLS Handshake**. They use asymmetric encryption (public/private keys and digital certificates) to securely agree on a shared, symmetric 'session key'. All subsequent HTTP traffic is encrypted using this fast session key.

## Common mistake: Common mistake

**Mixed Content Errors**. Beginners often build a secure `https://` website, but then try to load an image, script, or `fetch()` an API over an insecure `http://` connection. Modern browsers will strictly block this mixed content, breaking the site.

## Tip: Pro tip

Think of plain HTTP as sending a **Postcard** through the mail-anyone who touches it can read it. Think of HTTPS as putting that postcard inside an impenetrable **Steel Safe** where only the final recipient has the combination.

## Try it: Try it

Try sending a password in the Packet Interceptor below. Switch between HTTP and HTTPS to see what a 'Man in the Middle' hacker can actually read.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/https-and-tls

## Check yourself

### Question 1

What underlying protocol provides the encryption and security for HTTPS?

- A. TLS (Transport Layer Security)
- B. TCP (Transmission Control Protocol)
- C. AES (Advanced Encryption Standard)
- D. SSL (Secure Sockets Layer)

Correct answer: A

HTTPS relies entirely on the modern Transport Layer Security (TLS) protocol to encrypt communications.

- Why not B: TCP provides reliable data delivery, but it does not encrypt the data.
- Why not C: AES is a specific encryption algorithm used *within* TLS, but TLS is the protocol itself.
- Why not D: SSL is the predecessor to TLS and has been deprecated for over a decade due to security vulnerabilities.

### Question 2

What happens if a secure `https://` website attempts to `fetch()` data from an insecure `http://` API?

- A. The browser will throw a 'Mixed Content' error and block the request.
- B. The browser will automatically upgrade the API to HTTPS.
- C. The request succeeds, but the data is unencrypted.
- D. The server will reject the request with a 403 Forbidden.

Correct answer: A

This is known as a Mixed Content error. Browsers strictly block insecure active content (like API calls or scripts) on secure pages because an attacker could modify the insecure data to hijack the secure session.

- Why not B: While some browsers attempt upgrades for images, `fetch` requests are strictly blocked to prevent data leaks.
- Why not C: Browsers strictly forbid mixing secure and insecure active content to protect the user.
- Why not D: The request never even reaches the server; it is blocked locally by the browser.

### Question 3

During the TLS Handshake, how does your browser know that it is actually talking to `google.com` and not an imposter?

- A. It verifies the server's Digital Certificate against trusted Certificate Authorities.
- B. It checks the IP address against a global database.
- C. It sends a ping request that only Google can answer.
- D. The server sends a secret password that is hardcoded into the browser.

Correct answer: A

Servers must present a Digital Certificate signed by a trusted third party (Certificate Authority). The browser comes pre-installed with a list of trusted CAs and uses cryptography to verify the signature.

- Why not B: IP addresses change frequently and do not guarantee identity, as an attacker can spoof them.
- Why not C: Ping (ICMP) checks connectivity, not cryptographic identity.
- Why not D: Browsers do not store hardcoded passwords for every website.

## Remember this

- HTTPS is just HTTP wrapped inside a secure TLS tunnel.
- TLS Handshake establishes the secure connection before any data is sent.
- Browsers block 'Mixed Content' (loading HTTP assets on an HTTPS page).
- Digital Certificates prove the server is actually who it claims to be (e.g., Google).
