Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    Encryption in Practice

    Encryption in transit — data moving over a network. The answer is TLS everywhere: browser to server, and server to

    Chapter 4 of 135 min read

    The theory (symmetric vs asymmetric, key exchange, signatures) is in Networking 3: DNS, HTTP and HTTPS Ch 10. This chapter covers how you use it as an engineer.

    The Plain Picture#

    A bank has three problems: money moving in armoured vans (protect it on the road), money in the vault (protect it at rest), and the vault key itself (who holds the key?). The last one is the hardest.

    The Real Names#

    • Encryption in transit — data moving over a network. The answer is TLS everywhere: browser to server, and server to database, and service to service inside your network.

    • Encryption at rest — data stored on disk: databases, backups, files in object storage (AWS (coming soon), Google Cloud (coming soon)). Cloud providers can do this with one setting.

    • AES-GCM — the standard symmetric cipher mode to use. It provides secrecy and integrity: tampered ciphertext fails to decrypt. It needs a unique nonce (a "number used once") for every encryption with the same key — reusing a nonce with the same key breaks its security.

    • Key management — where keys live, who can use them, how they're rotated. Cloud KMS (Key Management Service — AWS KMS, Google Cloud KMS) keeps master keys inside secure hardware that never lets them out.

    • Envelope encryption — encrypt the data with a data key, then encrypt the data key with a master key held in KMS, and store the encrypted data key next to the data. To decrypt, ask KMS to unwrap the data key. The master key never leaves KMS, and every use of it is logged.

      data  ──AES-GCM with data key──▶  ciphertext        stored together
      data key ──KMS master key──────▶  encrypted data key  ─────────────▶  [ciphertext | encrypted data key]

    HMAC — proving a message came from someone who knows the secret#

    HMAC (Hash-based Message Authentication Code) combines a hash with a shared secret key: only someone who knows the key can produce the right HMAC for a message. It's how services verify webhooks — messages another service sends to your API.

    When a payment provider tells StudyBuddy "payment succeeded", it sends the body plus a signature header:

    Output
    body:      {"event":"quiz.completed","score":200}
    signature: HMAC-SHA256(key="webhook-secret", body) =
               5ce16b6f793367511f484fb4b9990e19a76a035fd8d710a8303d20521567bb86

    StudyBuddy recomputes the HMAC with its copy of the secret and compares (in constant time, Ch 3). If an attacker sends a fake "payment succeeded", they can't produce a valid signature without the secret.

    Gotchas

    • Never invent your own encryption scheme. Use well-reviewed libraries and high-level APIs (Python's cryptography package, Java's standard crypto with AES-GCM). Home-made crypto is almost always breakable.

    • "Encrypted at rest" doesn't protect against your own app being hacked. If the app can read the data, so can an attacker who controls the app. At-rest encryption protects against stolen disks and backups.

    • Keys in the source code are the most common failure of all (Ch 11).

    Check yourself

    4 questions
    1

    What's the difference between encryption in transit and at rest?

    Show answer

    In transit protects data moving over networks (TLS); at rest protects stored data (disks, backups, databases).

    2

    What is envelope encryption, and why use it?

    Show answer

    Data is encrypted with a data key, which is itself encrypted by a master key kept in KMS — the master key never leaves secure hardware, access is controlled and logged, and rotation is easier.

    3

    What does an HMAC prove, and where is it used?

    Show answer

    That the message was produced by someone holding the shared secret and wasn't altered; used for webhook signatures and signed tokens.

    4

    What must never be reused with AES-GCM?

    Show answer

    A nonce with the same key.