Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 53 ยท Cryptography and Authentication

Cryptography Essentials

Learn the cryptography every security tester needs: hashing, symmetric and asymmetric encryption, signatures, TLS in outline and the common misuses such as ECB and static IVs.

Cryptography protects data in transit, data at rest and the proof of who sent what. Used correctly it is among the strongest defenses we have. Used incorrectly it gives a false sense of safety, and security reviews find broken cryptography all the time: not because the algorithms are weak, but because developers pick the wrong mode, reuse a value that must be unique, or invent their own scheme.

You will not learn to design cryptographic algorithms here, and you should never try to in a real product. The goal is to know the building blocks, what each one guarantees, and the mistakes that testers look for. Everything in this lesson runs with Node's built-in node:crypto module in the stand-in examples; those use real, standard algorithms, but the data, keys and parameters are toy values chosen to make the output repeatable. Real systems get keys from a key management service, never from a source file. The next lesson, Password Attacks and Defenses, applies these ideas to storing passwords.

Four building blocks

Hash A fingerprint. One way.
Symmetric One shared key locks and unlocks.
Asymmetric Public key locks, private key unlocks.
Signature Proves who wrote it and that it is unchanged.

Each answers a different question. Do not mix them up.

1 / 2

Hashing

A cryptographic hash function turns any input into a fixed-length fingerprint. SHA-256 always yields 256 bits. A good hash has three properties: it is deterministic (same input, same output), one-way (you cannot work backwards to the input), and collision resistant (it is infeasible to find two inputs with the same output). Even a one-character change produces a completely different hash.

Hashes verify integrity (did this download change?) and are a building block of signatures and password storage. A plain hash is not encryption: nothing can be "decrypted", and it provides no secrecy by itself, because attackers can guess inputs and compare. MD5 and SHA-1 are broken for collision resistance and must not be used for security. Use SHA-256 or better (SHA-2, SHA-3).

To prove that a message came from someone who knows a secret key, use an HMAC (hash-based message authentication code). It mixes a key into the hash so only holders of the key can produce or verify the tag.

Node.js Hashes and HMAC with node:crypto

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice timingSafeEqual: comparing secrets with a normal == can leak how many leading bytes matched through tiny timing differences. Use a constant-time comparison for tags and tokens.

Symmetric encryption

Symmetric encryption uses one shared secret key for both locking and unlocking. AES is the standard cipher. A cipher alone only transforms one fixed-size block; a mode of operation says how to handle longer data, and the mode matters enormously.

  • ECB encrypts every block independently. Equal plaintext blocks become equal ciphertext blocks, so patterns survive. Never use it.
  • CBC chains blocks and needs a random IV (initialization vector), but it provides no integrity: an attacker can modify ciphertext, and misuse leads to padding-oracle attacks.
  • GCM (and ChaCha20-Poly1305) are authenticated encryption: they encrypt and also detect tampering. This is the modern default.

Rules for the IV or nonce: it need not be secret, but it must be unique for every message under the same key (random 12 bytes for GCM). Reusing a nonce with GCM is catastrophic, because it can expose the key stream and let attackers forge messages.

The stand-in below shows the ECB weakness and the benefit of authenticated encryption. It uses a made-up fixed key, which is fine for a demo and unacceptable in production.

Node.js ECB leaks patterns, GCM detects tampering

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The first output proves what the textbook says about ECB: the three blocks look the same, so structure (think of an encrypted image whose outline is still visible) leaks. The second part shows why authenticated modes are standard: a single flipped bit makes decryption fail instead of silently producing altered data.

Asymmetric encryption and signatures

Asymmetric (public-key) cryptography uses a key pair: a public key you can share and a private key you keep secret. It solves the key-distribution problem, because strangers can use your public key without meeting you first. Two main uses:

  • Key exchange: two parties agree on a shared secret over an open channel (Diffieโ€“Hellman and its elliptic-curve version). The shared secret then keys fast symmetric encryption.
  • Digital signatures: the private key signs a message, and anyone with the public key can verify it. A signature proves the message came from the key holder and was not altered (authenticity and integrity, not secrecy). RSA, ECDSA and Ed25519 are common.

Asymmetric operations are much slower than symmetric ones, so real protocols use them only to set up keys and sign things, then switch to AES or ChaCha20 for the bulk data.

Node.js Sign and verify with Ed25519

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Here the key pair is generated freshly each time, so the signature bytes differ between runs, but the three verification results are always the same.

TLS in outline

TLS is the protocol behind HTTPS. It combines all the building blocks:

sequenceDiagram
    participant C as Browser
    participant S as Server
    C->>S: ClientHello (supported ciphers, key share)
    S->>C: ServerHello + certificate + key share
    Note over C: Verify certificate chain and name
    C->>S: Finished (keys derived from key exchange)
    S->>C: Finished
    Note over C,S: Encrypted application data (symmetric)

TLS: authenticate the server with a certificate, agree on keys, then encrypt with a fast symmetric cipher.

The server proves its identity with a certificate, a public key signed by a certificate authority that your system trusts. The browser checks that the certificate is valid, unexpired and issued for the host name. Real TLS lives in your operating system and in the server, and cannot run on this site; the stand-ins above use the same primitives it is built from. Testers care about TLS versions and settings: old protocol versions (SSL, TLS 1.0 and 1.1) and weak cipher suites should be disabled, certificates must validate, and HTTP should redirect to HTTPS with HSTS.

Common mistakes testers look for

  • Rolling your own algorithm or protocol, or inventing a "custom encryption" that is really a substitution of some kind.
  • ECB mode, static or reused IVs and nonces, and unauthenticated encryption (CBC without a MAC).
  • Encryption without authentication: ciphertext that can be modified undetected.
  • Hard-coded keys in source code or images, and keys stored next to the data they protect.
  • Weak randomness: using Math.random() or timestamps for tokens, rather than a cryptographic random generator.
  • Using a plain hash for passwords (see the next lesson).
  • Disabled certificate validation ("just for testing") that reaches production.
  • Treating encoding as encryption: Base64 is a format, not a secret.
  • Non-constant-time comparison of tags and tokens.

A one-line rule covers most of it: use a well-reviewed high-level library, choose authenticated encryption, generate fresh random nonces, and keep keys in a managed store.

Identifying hashes, ciphers and steganography

Analysts often find a mysterious string in a file, a database dump or a CTF-style lab exercise and need to decide what it is before doing anything with it. Three quick questions help: how long is it, what characters does it use, and how random does it look?

Hash lengths and formats

A hex-encoded hash has a length that points to the algorithm:

Hex length Likely algorithm
32 MD5 (or NTLM, MD4)
40 SHA-1
64 SHA-256 (or SHA3-256)
128 SHA-512

Some formats announce themselves. Password hashes in Unix-style files start with a prefix such as $2b$ (bcrypt), $argon2id$ (Argon2) or $6$ (SHA-512 crypt). Length alone only narrows the field: two algorithms can share a length, so identification is a hypothesis, and a hash identification tool lists candidates, not certainties. Never assume that a string of 32 hex characters is a password hash; it could be a file checksum, a session ID or a UUID without dashes.

Recognizing encodings and ciphers

  • Base64 uses letters, digits, + and /, often ends with = padding and has a length that is a multiple of four. Base64URL swaps in - and _. It is an encoding, not encryption.
  • Hex uses only 0-9a-f. URL encoding shows %20 sequences.
  • Classical ciphers such as Caesar shifts or substitution preserve letter frequencies, so frequency analysis breaks them.
  • Modern ciphertext looks uniformly random; no frequency pattern remains, and its entropy is close to the maximum of 8 bits per byte.

Entropy is a quick triage tool: plain text scores around 4 to 5 bits per byte, compressed or encrypted data above 7.5. That tells you "encrypted or compressed" without telling you which. The same measure helps with static malware analysis.

Steganography: hiding data in plain sight

Encryption hides what a message says; steganography hides that a message exists. A common technique for images is least-significant-bit (LSB) embedding: each pixel color value is a byte, and changing its lowest bit alters the color so slightly that the eye cannot see it. A sender replaces those low bits with the bits of the secret message. A receiver who knows the scheme reads the low bits back.

Detection relies on statistics: normal images have correlated low bits, while embedded data makes them look random; comparing the original with a suspect copy shows differences; metadata and appended data after a file's end marker are also checked. Uses range from watermarking and covert communication to malware hiding payloads in images. Defenders cannot block every image, but they can watch for unusual outbound data, strange file sizes and tools used to embed.

The stand-in below classifies sample strings by length and character set and then hides and recovers a short message in the low bits of a synthetic "image" (an array of pixel values), all in memory.

Node.js Identify strings and hide data in low bits

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The largest change is 1 out of 255, which is why LSB messages are invisible to the human eye and why detection needs statistics. Treat this as a teaching model: real tools must also handle image compression, which can destroy low bits, and file formats with headers.

Rainbow tables in context

A rainbow table trades memory for time: instead of storing every hash, it stores compressed chains that are recomputed during lookup. They defeat fast, unsalted hashes and fail against salted ones, which is why password storage must always use a unique salt and a slow hash. See password security for the full design.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which statement about a cryptographic hash such as SHA-256 is true?

    A hash is one-way and always has the same output length. It is not encryption and involves no key, so there is nothing to decrypt.
  2. 2. Why should AES-ECB not be used?

    ECB encrypts each block independently, so structure in the data remains visible in the ciphertext. Use an authenticated mode such as GCM.
  3. 3. What does a digital signature provide?

    Signing with a private key lets anyone verify with the public key that the message came from the key holder and has not changed. It does not hide the content.
  4. 4. What is the rule for the nonce used with AES-GCM?

    The nonce is not secret, but reusing it with the same key breaks GCM's security and can expose data and allow forgeries.
  5. 5. Why use timingSafeEqual to compare an HMAC tag?

    Ordinary comparisons stop at the first differing byte, so response time can reveal how much of a guess was right. A constant-time comparison avoids that.

Summary

  • Hashes fingerprint data; HMACs add a key so only key holders can create the tag.
  • Symmetric ciphers (AES) share one key; use authenticated modes like GCM and never ECB.
  • Nonces and IVs need not be secret but must be fresh for each message.
  • Asymmetric cryptography handles key exchange and signatures; real protocols use it to set up fast symmetric encryption.
  • TLS authenticates the server with a certificate, agrees on keys and then encrypts the traffic.
  • Most real failures are misuse: custom schemes, reused nonces, hard-coded keys, weak randomness and disabled validation.

Next: Password Attacks and Defenses