CSCO-121 · Lesson 11 of 12 · 21 min
Cryptography fundamentals
▶ Video coming soon A video version of this lesson is on the way.
By the eleventh week, Jordan had used the words encryption, hashing, certificates and keys dozens of times without being entirely sure how they fitted together. Keisha noticed and cleared an afternoon. She drew three boxes on the whiteboard, labelled them symmetric, asymmetric and hash, and said that nearly everything in security that involves secrets is built from those three pieces plus a sensible way of looking after keys. "You do not need to do the maths," she said. "You need to know which piece does which job, and what goes wrong when people use the wrong one."
Cryptography is the practice of protecting information using mathematical techniques. It supports every property from lesson 2: encryption protects confidentiality, hashes and message authentication codes protect integrity, and digital signatures provide authenticity and non-repudiation. This lesson explains the building blocks at the level the exam expects, shows how TLS and other protocols combine them, and covers the practical side: storing passwords, managing keys and avoiding outdated algorithms.
Basic terms
Plaintext is readable data. Ciphertext is the scrambled output of encryption. A cipher, or algorithm, is the method; a key is the secret value that controls it. Modern security does not depend on keeping the algorithm secret. The best algorithms are published and have been studied by experts for years; their security rests entirely on the key. This principle, named after the nineteenth-century cryptographer Auguste Kerckhoffs, is why you should distrust any product that claims to use a secret, proprietary algorithm.
Key length matters because an attacker who knows the algorithm can, in principle, try every possible key. Each extra bit doubles the number of keys. A 128-bit key has about 3.4 × 10 to the power 38 possibilities, far beyond any foreseeable computer's ability to search, which is why AES with 128-bit or 256-bit keys is considered strong. Encoding, such as Base64, is not encryption: it changes the format of data so that it can travel through systems that expect text, and anyone can reverse it without a key. Seeing an encoded password in a configuration file is the same as seeing the password.
Symmetric encryption
Symmetric encryption uses the same key to encrypt and decrypt. It is fast and suitable for large amounts of data, which is why it protects stored files, disk volumes and the bulk of every TLS session. The Advanced Encryption Standard, AES, is the standard choice, with 128, 192 or 256-bit keys. ChaCha20 is a modern alternative common on phones. Older ciphers such as DES, with its 56-bit key, and 3DES are obsolete. Symmetric ciphers are used in modes; current protocols favour authenticated modes such as AES-GCM, which encrypt and detect tampering at the same time.
The weakness of symmetric encryption is key distribution. Both sides need the same secret key, and they need to get it to each other without anyone else seeing it. Among many parties the number of keys grows quickly: every pair of people needs its own key, so a group of n people needs n × (n − 1) / 2 keys. For nine people at Riverside Family Law that is already 36 keys, and for an internet of billions it is impossible. Asymmetric cryptography solves this problem.
Randomness
Keys, salts and session values must be unpredictable, which means they must come from a cryptographically secure random number generator, such as the secrets module in Python or the operating system's random source. Ordinary random functions used for games and simulations are predictable enough that keys made with them can be guessed. Weak randomness has caused real failures, including devices that generated the same keys as thousands of others.
Worked problem
Counting symmetric keys
If every pair of Riverside Family Law's 9 staff shared a separate secret key, how many keys would be needed? How many would 40 people need?
Show solution
- The number of pairs is n × (n − 1) / 2.
- For 9 people: 9 × 8 / 2 = 36 keys.
- For 40 people: 40 × 39 / 2 = 780 keys.
Answer: 36 keys for 9 people and 780 for 40, which is why symmetric keys are normally exchanged using asymmetric methods instead of being shared in advance.
Asymmetric encryption
Asymmetric, or public key, cryptography uses a pair of mathematically related keys. The public key can be shared with anyone; the private key must be kept secret by its owner. Data encrypted with the public key can be decrypted only with the matching private key, so anyone can send the owner a confidential message. Data signed with the private key can be verified with the public key, so anyone can check that the owner signed it. Common algorithms are RSA, based on the difficulty of factoring large numbers, and elliptic curve cryptography, ECC, which gives similar strength with much shorter keys and is common in modern systems.
Asymmetric algorithms are much slower than symmetric ones, so they are rarely used to encrypt bulk data. Instead, protocols use them to agree on or protect a symmetric key, then use that key for the data. This combination is called hybrid encryption. Key exchange methods such as Diffie-Hellman, usually in its elliptic curve form ECDHE, let two parties agree on a shared secret over a public network without ever sending the secret itself. TLS 1.3 uses ephemeral key exchange for every session, which gives forward secrecy: even if a server's long-term private key is stolen later, past sessions cannot be decrypted.
Hashing
A cryptographic hash function takes input of any size and produces a fixed-size output, the hash or digest. A good hash function is one-way, meaning you cannot work back from the hash to the input; small changes in input produce completely different hashes, as lesson 2 showed with the altered invoice; and it is collision resistant, meaning it is impractical to find two different inputs with the same hash. The SHA-2 family, especially SHA-256, and the newer SHA-3 family are current standards. MD5 and SHA-1 are broken for security purposes, because collisions can be produced deliberately, though you may still see them used as simple checksums for detecting accidental corruption.
Hashes have many uses: checking that a downloaded file matches the publisher's published hash, detecting changes to files in integrity monitoring, identifying malware samples, and forming part of digital signatures. A hash alone does not prove who created a message, because anyone can compute a hash. Adding a secret key gives a message authentication code; the common construction is HMAC, used in the TOTP codes in lesson 10 and inside many protocols. A receiver who shares the key can check both that the message is unchanged and that it came from someone who knows the key.
Storing passwords
Systems should never store passwords in plain text or with reversible encryption. They should store a hash, so that when a user signs in, the system hashes what was typed and compares it with the stored value. A plain fast hash such as SHA-256 is not enough, for two reasons. First, identical passwords produce identical hashes, so attackers can precompute hashes of common passwords, in what are sometimes called rainbow tables, and look them up. Second, fast hashes let attackers test billions of guesses per second against a stolen database.
The answer is a salt and a slow hash. A salt is a random value, different for each user, stored with the hash and mixed into it, so identical passwords produce different hashes and precomputed tables are useless. A slow, purpose-built password hashing function, such as Argon2, bcrypt, scrypt or PBKDF2 with a high iteration count, makes each guess expensive. A user signing in barely notices a fraction of a second; an attacker making billions of guesses is slowed enormously.
import hashlib, secrets
def store(password):
salt = secrets.token_bytes(16)
digest = hashlib.pbkdf2_hmac("sha256", password.encode(), salt, 200_000)
return salt, digest
def check(password, salt, digest):
trial = hashlib.pbkdf2_hmac("sha256", password.encode(), salt, 200_000)
return secrets.compare_digest(trial, digest)
s1, d1 = store("correct horse battery staple")
s2, d2 = store("correct horse battery staple")
print("same password, same stored hash?", d1 == d2)
print("right password accepted:", check("correct horse battery staple", s1, d1))
print("wrong password accepted:", check("Correct horse battery staple", s1, d1))same password, same stored hash? False right password accepted: True wrong password accepted: False
The program stores the same password twice. Because each copy has its own random salt, the stored hashes differ, so an attacker cannot tell that two users share a password. PBKDF2 with 200,000 iterations makes each check deliberately slow. The comparison uses a constant-time function so that timing differences do not leak information.
Digital signatures and certificates
A digital signature combines hashing and asymmetric cryptography. The signer hashes the document and then uses their private key to sign the hash. Anyone can verify the signature using the signer's public key: they hash the document themselves and check it against the signed value. If the document has changed by a single character, or if a different private key was used, verification fails. Signatures therefore provide integrity, authenticity and, because only the signer holds the private key, non-repudiation.
Signatures are everywhere: software updates are signed so that devices install only genuine code; email can be signed with S/MIME; documents can be signed electronically; and every TLS certificate is a public key signed by a certificate authority. The certificate is what links a public key to a real identity. Without it, an attacker could hand you their own public key and claim it belonged to your bank. Lesson 8 described how certificate chains and PKI provide that link.
| Goal | Building block | Examples |
|---|---|---|
| Confidentiality of bulk data | Symmetric encryption | AES-GCM, ChaCha20 |
| Agreeing on a key over a public network | Asymmetric key exchange | ECDHE in TLS 1.3 |
| Detecting changes | Hash | SHA-256, SHA-3 |
| Detecting changes by someone with a shared key | Message authentication code | HMAC-SHA256 |
| Proving who signed and that nothing changed | Digital signature | RSA or ECDSA signatures on updates and certificates |
| Storing passwords | Salted slow password hash | Argon2, bcrypt, scrypt, PBKDF2 |
Key management and the future
Cryptography is only as strong as the protection of its keys. Key management covers generating keys with good randomness, storing them safely, controlling who can use them, rotating them on a schedule or when compromise is suspected, backing them up where needed, and destroying them when no longer needed. Private keys for important certificates may live in a hardware security module or a cloud key service, designed so that the key can be used but not copied. On laptops, a trusted platform module chip protects disk encryption keys. Recovery keys for disk encryption must be escrowed somewhere safe, or a forgotten PIN can make a laptop's data unrecoverable, an availability failure caused by good confidentiality.
Cryptography also changes over time. Algorithms once considered strong, such as DES, MD5 and SHA-1, have been retired as computers grew faster and researchers found weaknesses. Large quantum computers, if they are built, would break today's RSA and elliptic curve algorithms. In response, standards bodies have published post-quantum algorithms; the US National Institute of Standards and Technology released its first post-quantum standards in 2024, and vendors are beginning to add them to products. For a support technician, the practical lesson is crypto agility: keep systems updated so that they can move to newer algorithms, and replace devices that are stuck on obsolete ones.
Common mistakes
MistakeCalling Base64 encryption.
InsteadEncoding is reversible by anyone; it provides no confidentiality.
MistakeStoring passwords with a fast unsalted hash.
InsteadUse a unique salt and a slow password hashing function such as Argon2 or bcrypt.
MistakeUsing MD5 or SHA-1 for security.
InsteadBoth are broken for collision resistance; use SHA-256 or SHA-3.
MistakeEncrypting large files with RSA directly.
InsteadAsymmetric algorithms protect keys; symmetric ciphers encrypt bulk data.
MistakeSharing a private key to make setup easier.
InsteadA private key must stay with its owner; share only the public key or certificate.
Summary and key terms
- Modern cryptography relies on published algorithms and secret keys; longer keys mean exponentially more work for attackers.
- Symmetric encryption (AES) is fast but needs a shared key; n parties need n × (n − 1) / 2 pairwise keys.
- Asymmetric cryptography (RSA, ECC) uses public and private keys for key exchange, encryption to an owner and signatures.
- Hybrid encryption uses asymmetric methods to agree on symmetric session keys; ephemeral exchange gives forward secrecy.
- Hashes (SHA-2, SHA-3) detect changes; HMAC adds a key; MD5 and SHA-1 are broken for security.
- Passwords are stored as salted, slow hashes using Argon2, bcrypt, scrypt or PBKDF2.
- Digital signatures provide integrity, authenticity and non-repudiation; certificates bind public keys to identities.
- Key management and crypto agility, including the move to post-quantum algorithms, keep cryptography effective.
- Plaintext
- Readable data before encryption.
- Ciphertext
- Scrambled data produced by encryption.
- Symmetric encryption
- Encryption using the same key to encrypt and decrypt.
- Asymmetric encryption
- Encryption using a public and private key pair.
- Hash function
- A one-way function producing a fixed-size digest of any input.
- Salt
- A random value added to each password before hashing.
- HMAC
- A message authentication code built from a hash and a secret key.
- Digital signature
- A value made with a private key that anyone can verify with the public key.
- Forward secrecy
- A property ensuring past sessions stay safe even if a long-term key is stolen later.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Why is AES used for bulk data instead of RSA?
Show answer
Symmetric encryption is much faster.
How many pairwise keys do 12 people need?
Show answer
12 × 11 / 2 = 66.
Which key encrypts a message so that only the owner can read it?
Show answer
The owner's public key.
Which key creates a digital signature?
Show answer
The signer's private key.
Why add a salt to a password hash?
Show answer
So identical passwords get different hashes and precomputed tables fail.
Name a password hashing function suitable for storing passwords.
Show answer
Argon2, bcrypt, scrypt or PBKDF2.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.