Skip to content

HTTPS, encryption and secrets

How HTTPS keeps a connection private, what a certificate is, how data is protected while it moves and while it is stored, and how keys and passwords are kept safe.

  • Lesson 102 of 125
  • 12 min read
  • Depth
  • Free to read, no account
On this page
  1. Why interviewers ask this
  2. The idea in plain words
  3. Pause and predict
  4. Worked example
  5. Trade-offs
  6. Common misconceptions
  7. How to say it in the interview
  8. Check your understanding
  9. Answers
  10. Sources

What you need first

This lesson uses ideas from these earlier lessons. Read them first if any idea is new to you.

01Why interviewers ask this

The interviewer asks: "How do you keep user data safe? Where does the database password live?" A weak answer is "we encrypt everything". A strong answer separates data on the move from data in storage, and says who holds the keys.

02The idea in plain words

You send an open padlock to anyone. Only you hold its key. A stranger locks a box with it and sends it back, and only you can open it.

Encryption scrambles data so that only someone with the right key can read it. Here, "lock" means encrypt and "unlock" means decrypt. Symmetric encryption uses one key to lock and unlock, and is fast. Asymmetric encryption uses a pair: the public key is shared. What it locks, only the private key opens. It is slow, so it is used only to start a connection.

TLS (Transport Layer Security) is the lock in HTTPS (lesson 7). It keeps data private, shows if anyone changed it, and proves that you reached the real server. The handshake, the first messages that set up the lock, takes one round trip in TLS 1.3:

  1. The client says hello and starts a key exchange, a way for two sides to agree on a secret key over an open network.
  2. The server sends its certificate and its part. It signs the handshake with its private key to prove that it owns the certificate. Both sides have the same session key, which is never sent.
  3. The client checks the certificate.
  4. All data is locked with the fast session key.

A certificate says "this public key belongs to this host name". It has an end date. A certificate authority (CA), a company that browsers trust, signs it. The client checks the name, the date, and that the signature leads to a trusted CA. If one fails, the connection fails. Let's Encrypt, a free CA, issues 90-day certificates and advises renewal every 60 days, by a program. The longest life browsers allow: 200 days from 15 March 2026, 100 days from 15 March 2027, 47 days from 15 March 2029.

In transit means while data moves on a network. TLS covers it. At rest means while data sits in a disk, a database or a backup. Encrypt it with a strong method, such as AES (Advanced Encryption Standard) with a 256-bit key. This protects a stolen disk or backup. It does not stop someone who can ask the app, which unlocks the data for them.

Envelope encryption keeps keys safe. A data key locks the data. A master key locks the data keys. A key management service (KMS) holds the master key, does the locking, and never shows the key. Without data keys, a master key change would re-lock all the data. With them, you re-lock only the small data keys.

A secret is a password, API key or private key. Keep secrets out of code, git and logs. Store them in a secrets manager. Change them often and after any leak.

Stuck on something? The AI tutor has read this lesson. Ask it with a free account

03Pause and predict

Pause. Guess first.

You must change the master key. You have 2 TB of data and 5,000 data keys. Locking data runs at 100 MB per second. One re-lock of a data key takes 10 ms. Which is faster, and by how many times?

Write your guess before you read on. The answer is in the worked example.

04Worked example

A food app. Assumptions (made up): a round trip is 50 ms, DNS 30 ms, server work 20 ms.

Cost of the lock. The first visit without TLS: DNS 30 + connection 50 + request 50 + work 20 = 150 ms. TLS 1.3 adds one round trip: 150 + 50 = 200 ms.

Master key change. Without data keys, re-locking all the data: 2 TB = 2,000,000 MB. 2,000,000 / 100 = 20,000 seconds = 5.6 hours. With data keys, re-locking the keys: 5,000 x 10 ms = 50,000 ms = 50 seconds. That is 400 times faster (20,000 / 50). This answers the predict box.

05Trade-offs

TLS solves private, checked traffic. It costs a round trip and certificates to renew. Encryption at rest solves stolen disks and backups. It costs key management. I would use envelope encryption when the data is large.

06Common misconceptions

"HTTPS means the site is safe." It means the line is private and the server owns the name.

"I deleted the password from git, so it is safe." Git keeps its history. Change the password.

07How to say it in the interview

Say it like this

I use TLS in transit, with certificates renewed by a program, and I keep secrets in a secrets manager, never in code. At rest I use data keys, locked by a master key in a key service, and I change a leaked secret at once.

08Check your understanding

  1. What is the difference between data in transit and at rest? Which three things does a client check in a certificate?
  2. There are 5 TB of data and 1,000 data keys. Locking data runs at 200 MB per second. A re-lock takes 20 ms. How long does each way of changing the master key take?
  3. At 12:00 am every API request fails. The servers are healthy. The client error says the certificate has expired. What do you do now, and what stops it next time?

09Answers

Show the answers
  1. In transit: moving on a network. At rest: stored. The client checks the name, the date and the signature chain.
  2. Re-locking the data: 5 TB = 5,000,000 MB. 5,000,000 / 200 = 25,000 seconds = 6.9 hours. Re-locking: 1,000 x 20 ms = 20 seconds. That is 1,250 times faster.
  3. Install a new certificate. Then renew by a program, and alert when a certificate is near its end date and not yet renewed.

10Sources

Try it now, no sign-up

More practice, in the app (free account)

Ask the AI tutor about this lesson

The tutor has read this lesson. Ask in your own words, any time. Free account, 20 questions a day.

Reading never needs an account. The account is for the tutor, the quiz, your notes and your progress.

All lessons in Module 16: Security and privacy