Skip to content
BoKSA

16 – Cryptography: Symmetric and Public-Key Encryption, Hashes and Digital Signatures

16 – Cryptography: Symmetric and Public-Key Encryption, Hashes and Digital Signatures

Course: ICT Basic course · Level: introductory (concepts, not mathematics) Book: Kurose & Ross, 9th ed., chapter 8.2 (principles of cryptography), 8.3 (message integrity and digital signatures), 8.4 (end-point authentication)

Learning goals

After this article you can:

  • explain the terms plaintext, ciphertext, key, algorithm and Kerckhoffs' principle;
  • explain symmetric encryption with examples (Caesar/substitution ciphers → AES, ChaCha20) and its key distribution problem;
  • explain public-key (asymmetric) cryptography (RSA, ECC) and Diffie-Hellman key exchange;
  • explain hash functions, MACs and digital signatures and what each one protects;
  • explain why we need certificates and Certificate Authorities;
  • name which algorithms are current, outdated or broken, and what post-quantum cryptography is.

1. Basic terms

1
2
3
4
5
6
           key K_A                                key K_B
             │                                      │
 plaintext ──►[encryption algorithm]── ciphertext ──►[decryption algorithm]──► plaintext
     m                                  K_A(m)                                  m = K_B(K_A(m))
                                          ▲
                                        Trudy
  • Plaintext: the original message. Ciphertext: the encrypted message.
  • Algorithm: the method (public!). Key: the secret that controls the algorithm.
  • Kerckhoffs' principle (1883): a system must be secure even if everything about it is public except the key. So: never trust "secret" home-made algorithms ("security through obscurity").

Types of attacks: ciphertext-only (Trudy only has encrypted text: brute force, statistical analysis), known-plaintext, chosen-plaintext.


2. Symmetric-key cryptography

Alice and Bob use the same secret key K_S to encrypt and decrypt.

2.1 Classic ciphers (history)

Caesar cipher: shift every letter k positions (k = 3: a→d, b→e…). Only 25 possible keys → trivial to break.

Monoalphabetic substitution cipher: replace each letter by another letter according to a fixed table:

1
2
3
4
plaintext:  abcdefghijklmnopqrstuvwxyz
ciphertext: mnbvcxzasdfghjklpoiuytrewq

"bob. i love you. alice"  →  "nkn. s gktc wky. mgsbc"

26! ≈ 4×10²⁶ possible keys, but easy to break with frequency analysis (in English 'e' and 't' are the most common letters; in Dutch 'e' and 'n'). Polyalphabetic ciphers (Vigenère) and machines such as Enigma (WWII) improved this, but were broken too.

2.2 Modern symmetric algorithms

Algorithm Status
DES (56-bit key, 1977) broken, can be brute-forced in hours. Don't use
3DES deprecated (NIST disallowed it after 2023)
RC4 (stream cipher) broken, forbidden in TLS
AES (Advanced Encryption Standard, 2001) – block cipher, 128-bit blocks, keys of 128/192/256 bits current standard, hardware-accelerated in almost every CPU
ChaCha20 (stream cipher) current, fast in software (phones), used in TLS 1.3, WireGuard, SSH

Modern block ciphers are used in a mode of operation. Today that's an authenticated encryption (AEAD) mode such as AES-GCM or ChaCha20-Poly1305, which gives confidentiality and integrity together. (ECB mode is insecure: identical blocks give identical ciphertext.)

How strong is 128 bits? 2¹²⁸ ≈ 3.4×10³⁸ keys. Even with all computers on Earth, brute force would take far longer than the age of the universe (see the 3Blue1Brown video).

2.3 The problem: key distribution

How do Alice and Bob agree on a secret key if they've never met and can only communicate over the (insecure) Internet? With n people who all want to communicate privately, you need n(n−1)/2 keys. → Public-key cryptography solves this.


3. Public-key (asymmetric) cryptography

Diffie & Hellman (1976), Rivest, Shamir & Adleman – RSA (1977).

Every person has a key pair: - a public key K⁺: everyone may know it; - a private key K⁻: only the owner knows it.

What one key encrypts, only the other key can decrypt: K⁻(K⁺(m)) = m and K⁺(K⁻(m)) = m.

1
2
3
 Encryption (confidentiality):          Signing (authenticity, integrity):
 Alice encrypts with Bob's PUBLIC key   Bob signs with his PRIVATE key
 → only Bob (PRIVATE key) can decrypt   → everyone can verify with Bob's PUBLIC key
  • RSA: security based on the difficulty of factoring large numbers (the product of two large primes). Keys of at least 2048 bits, preferably 3072+.
  • ECC (Elliptic Curve Cryptography): same security with much smaller keys (256-bit ECC ≈ 3072-bit RSA). Used in ECDSA, Ed25519 (signatures) and X25519/ECDHE (key exchange). Standard in TLS 1.3, SSH, Signal, WhatsApp.

Public-key crypto is slow (100–1000× slower than AES). So in practice we use a hybrid: public-key methods agree on a session key, then the data is encrypted with fast symmetric encryption (AES/ChaCha20). This is exactly what TLS does (article 17).

Diffie-Hellman key exchange

Two parties can agree on a shared secret over a public channel without ever sending the secret itself. The famous paint analogy: both mix a public colour with their own secret colour, exchange the mixtures, and add their own secret colour again; both end up with the same colour, but an eavesdropper can't "unmix" the paint. Ephemeral (new for every session) Diffie-Hellman (ECDHE) gives forward secrecy: stealing the server's long-term key later doesn't decrypt old recorded sessions.


4. Integrity: hashes, MACs and digital signatures

4.1 Cryptographic hash functions

A hash function H(m) turns a message of any length into a fixed-length "fingerprint" (digest):

  • the same input always gives the same output;
  • one-way: you can't compute m from H(m);
  • collision-resistant: it is practically impossible to find two messages with the same hash;
  • a tiny change in m gives a completely different hash (avalanche effect).
1
2
$ echo "Infrastructure" | sha256sum
$ echo "infrastructure" | sha256sum      # one letter different → totally different hash
Hash Status
MD5 (128 bits) broken (collisions in seconds). Only OK as a non-security checksum
SHA-1 (160 bits) broken (practical collision "SHAttered", 2017). Being removed everywhere
SHA-256 / SHA-384 / SHA-512 (SHA-2) current
SHA-3, BLAKE2/BLAKE3 current

Passwords are stored with special slow hash functions with a salt: Argon2id, bcrypt, scrypt, PBKDF2. Never with plain MD5/SHA (see "How NOT to store passwords").

Note: the Internet checksum and CRC (article 13) are not cryptographic hashes.

4.2 Message Authentication Code (MAC)

A hash alone doesn't help against an attacker: Trudy can change m and compute a new H(m). Solution: include a shared secret s:

MAC = H(m + s). In practice: HMAC-SHA256 (RFC 2104), or the built-in tag of AES-GCM/Poly1305.

Bob recomputes the MAC with the same secret: if it matches, the message is from someone who knows s and unchanged.

4.3 Digital signatures

  1. Bob computes the hash H(m) of the document.
  2. Bob encrypts the hash with his private key: signature = K_B⁻(H(m)).
  3. Alice decrypts the signature with Bob's public key and compares the result with her own H(m).

If they match: the message was signed by Bob (authenticity), it's unchanged (integrity), and Bob can't deny signing it (non-repudiation).

Used for: TLS certificates, software updates and app stores, code signing, e-mail (S/MIME, PGP, DKIM), DNSSEC, Git commits, legally valid e-signatures (eIDAS), and blockchain transactions.


5. Who owns this public key? Certificates and CAs

Problem: Trudy can send Alice her own public key, claiming it's Bob's (man-in-the-middle).

Solution: a Certificate Authority (CA) checks the identity and digitally signs a certificate that binds a public key to a name (e.g. www.hva.nl). Format: X.509.

  • Browsers and operating systems contain a list of trusted root CAs.
  • Chain of trust: root CA → intermediate CA → server certificate.
  • Certificate Transparency: all publicly trusted certificates are logged in public logs, so wrongly issued certificates can be detected (https://crt.sh/).

The DigiNotar case (2011): a hacked Dutch CA issued fake certificates for Google, used to spy on Iranian users. DigiNotar was removed from all browsers and went bankrupt, and the Dutch government had to replace many certificates quickly.

Details on certificates in TLS: article 17.


6. Post-quantum cryptography (new)

A large enough quantum computer could break RSA, Diffie-Hellman and ECC (Shor's algorithm). It doesn't exist yet, but attackers can "harvest now, decrypt later": record encrypted traffic today and decrypt it in the future.

  • Symmetric crypto and hashes are much less affected (use AES-256 to be safe).
  • In August 2024, NIST published the first post-quantum standards: ML-KEM (FIPS 203, key exchange, formerly "Kyber"), ML-DSA (FIPS 204, signatures, formerly "Dilithium") and SLH-DSA (FIPS 205, hash-based signatures).
  • Hybrid key exchange (X25519 + ML-KEM-768) is already enabled by default in Chrome, Firefox, Edge, Safari, Cloudflare and OpenSSH, so a large part of today's TLS traffic is already protected against future quantum attacks.
  • Organisations are advised (also by the NCSC and AIVD) to make a crypto inventory and plan the migration (crypto agility).

7. Try it yourself

  1. Encrypt and decrypt a file with AES using OpenSSL:
    1
    2
    3
    echo "secret test results" > msg.txt
    openssl enc -aes-256-cbc -pbkdf2 -salt -in msg.txt -out msg.enc
    openssl enc -d -aes-256-cbc -pbkdf2 -in msg.enc
    
  2. Create a key pair and sign/verify a file:
    1
    2
    3
    4
    openssl genpkey -algorithm ed25519 -out priv.pem
    openssl pkey -in priv.pem -pubout -out pub.pem
    openssl pkeyutl -sign -inkey priv.pem -rawin -in msg.txt -out msg.sig
    openssl pkeyutl -verify -pubin -inkey pub.pem -rawin -in msg.txt -sigfile msg.sig
    
    Change one character in msg.txt and verify again. What happens?
  3. Create an SSH key (ssh-keygen -t ed25519). Which file is the public key, and which is the private key?
  4. Break a substitution cipher using letter frequencies (try the online tool at https://www.dcode.fr/monoalphabetic-substitution).
  5. Look up all certificates ever issued for hva.nl: https://crt.sh/?q=hva.nl.

Check your understanding

  1. What is Kerckhoffs' principle?
  2. Why is a monoalphabetic substitution cipher weak, even with 26! keys?
  3. What is the key distribution problem, and how does public-key cryptography solve it?
  4. Why do we combine public-key and symmetric encryption?
  5. What is the difference between a hash, a MAC and a digital signature?
  6. With which key does Bob sign, and with which key does Alice verify?
  7. Why are MD5 and SHA-1 no longer used for security?
  8. What does "harvest now, decrypt later" mean?

References

Book

  • Kurose & Ross, 9th ed., chapter 8.2–8.4

Videos

Articles and standards


Cyber Security

Why is this important for cyber security?

  • Cryptography is the toolbox of security. HTTPS, VPNs, Wi-Fi (WPA3), SSH, password storage, e-mail authentication (DKIM), DNSSEC, software updates, banking apps and messaging (Signal/WhatsApp) all depend on it.
  • But most failures are not in the mathematics, they are in how crypto is used: weak or reused passwords, keys stored in source code or Git repositories, outdated algorithms (MD5, SHA-1, DES, RC4), home-made crypto, expired certificates, bad random number generators, or ECB mode.
  • Private keys are crown jewels. Whoever has a private key can impersonate a server, sign malware or decrypt data.
  • Quantum computers are a future threat to today's public-key algorithms (harvest now, decrypt later).
  • Encryption is also used by attackers: ransomware encrypts victims' files, and malware hides its communication inside TLS.

What can you do to make it safe?

  • Never invent your own crypto. Use well-known libraries (libsodium, OpenSSL/BoringSSL, the standard libraries of your language) and high-level APIs.
  • Use current algorithms: AES-GCM or ChaCha20-Poly1305, SHA-256+, Ed25519/ECDSA or RSA ≥ 3072, X25519 (+ ML-KEM), Argon2id/bcrypt for passwords.
  • Protect keys: never put them in source code (use a secrets manager or vault, or .env files that are excluded from Git), restrict file permissions (chmod 600), use hardware protection where possible (TPM, HSM, YubiKey, passkeys), and rotate keys.
  • Use strong randomness: secrets in Python, crypto.getRandomValues() in JavaScript, never random() for keys or tokens.
  • Encrypt data at rest (BitLocker, FileVault, LUKS) and in transit (TLS, VPN, SSH).
  • Plan for change (crypto agility): know where you use which algorithm, so you can replace it (e.g. for post-quantum).
  • Mind the law: the GDPR requires "appropriate technical measures"; encryption of personal data is one of them, and it can mean a lost laptop isn't a reportable data breach.

Extra references