Chrysolites Open Courses

Cybersecurity Topics: Threats, Defences and Careers · Course book

Chapter 5: Cryptography Basics

Pairs with Lesson 5: Cryptography Basics: Ciphers, Keys and RSA

Every time you send a message, log in to a website or pay with a card, cryptography is working out of sight to keep the information private and to prove that it has not been changed. Cryptography is the science of protecting information by transforming it so that only intended parties can read or verify it. This chapter explains the basic vocabulary of plaintext, ciphertext and keys, starting with a classical cipher that can be broken with pencil and paper to show why modern methods are needed. It then classifies cryptography into symmetric encryption, asymmetric encryption and hashing, works through a miniature example of the RSA algorithm with numbers small enough to check by hand, and explains how the three families combine in real systems such as secure websites and digital signatures.

The vocabulary of cryptography

The original, readable message is called the plaintext. Applying an encryption algorithm, or cipher, together with a secret value called a key, produces ciphertext, which should look like meaningless data to anyone without the right key. Decryption reverses the process. A fundamental principle, set out in the nineteenth century by Auguste Kerckhoffs, is that a system should remain secure even if everything about it except the key is public. Modern algorithms are published openly and studied by researchers around the world; their security rests entirely on keeping keys secret and making them too numerous to guess.

The Caesar cipher, said to have been used by Julius Caesar, shows the ideas in their simplest form. Each letter is shifted a fixed number of places along the alphabet. With a shift of 3, A becomes D, B becomes E and so on, wrapping round so that X becomes A. The plaintext MEET AT NOON becomes PHHW DW QRRQ. The key is the shift, and there are only 25 useful shifts, so an attacker can simply try them all in a minute, an approach called a brute-force or exhaustive key search. Even more complicated substitution ciphers fall to frequency analysis, because common letters such as E and T remain common in the ciphertext. Modern cryptography is designed so that neither approach works.

Three families: symmetric, asymmetric and hashing

Modern cryptographic tools fall into three families, each solving a different problem.

FamilyKeysMain purposeWell-known examplesStrengths and limits
Symmetric encryptionOne shared secret key encrypts and decryptsConfidentiality of bulk dataAES, ChaCha20Very fast; but both sides must already share the key
Asymmetric (public-key) encryptionA public key and a matching private keyKey exchange, digital signaturesRSA, elliptic-curve methodsSolves key sharing; much slower than symmetric
Hash functionsNo key (keyed versions exist)Integrity checks, password storageSHA-256, SHA-3One-way and fixed-length; cannot be reversed

Symmetric encryption uses the same key to encrypt and decrypt. The Advanced Encryption Standard (AES), adopted by the United States government in 2001 after an open international competition, is now used almost everywhere, from Wi-Fi to disk encryption. It replaced the older Data Encryption Standard (DES), whose 56-bit key had become too short. The difficulty with symmetric encryption is the key distribution problem: if two people have never met, how do they agree on a secret key without an eavesdropper learning it?

Asymmetric encryption, also called public-key cryptography, solves that problem with pairs of mathematically linked keys. The public key can be shared with anyone; the private key is kept secret. Data encrypted with the public key can be decrypted only with the private key. Anyone can therefore send you a secret message using your public key, but only you can read it. The same mathematics run in reverse allows digital signatures: something processed with your private key can be checked by anyone with your public key, proving that it came from you and was not altered.

A hash function takes input of any length and produces a fixed-length output, called a hash or digest, such as the 256 bits of SHA-256. A good cryptographic hash is one-way, so the input cannot be worked out from the output; it changes completely if even one character of the input changes; and it makes it practically impossible to find two inputs with the same hash. Hashes are used to check that downloaded files are intact, to store passwords safely and as a building block of digital signatures.

Symmetric Alice Bob same secret keylocks and unlocks fast, but the key mustbe shared safely first Asymmetric Alice Bob lock with Bob's public keyunlock with Bob's private key no shared secret needed,but much slower
Figure 5.1 — Symmetric encryption uses one shared key; asymmetric encryption uses a public key to lock and a separate private key to unlock.

How large must a key be?

Because good algorithms have no known shortcuts, an attacker without the key must try keys one after another. Each extra bit of key length doubles the number of possible keys. That makes the difference between key sizes dramatic.

Worked example. Imagine an attacker who can test a million million (1012) keys per second. A 56-bit DES key has 256 ≈ 7.2 × 1016 possibilities, so trying them all takes 7.2 × 1016 ÷ 1012 ≈ 72,000 seconds, about 20 hours. An 80-bit key would take about 38,000 years. A 128-bit AES key has 2128 ≈ 3.4 × 1038 possibilities, which would take about 3.4 × 1026 seconds, roughly 1019 years, close to a billion times the age of the universe.

This is why 56-bit DES was retired, while 128-bit and 256-bit AES are considered secure for the foreseeable future. Public-key algorithms need much longer keys for similar strength, because their mathematics allows cleverer attacks than simple guessing; an RSA key of 2,048 bits is roughly comparable to a 112-bit symmetric key, and 3,072 bits to 128 bits. Researchers are also preparing for large quantum computers, which could break today's RSA and elliptic-curve methods, and new "post-quantum" algorithms have already been standardised to replace them.

RSA in miniature

RSA, published in 1977 by Ron Rivest, Adi Shamir and Leonard Adleman, was one of the first practical public-key systems. Its security relies on the fact that multiplying two large prime numbers is easy, but working out the two primes from their product is extremely hard when the numbers are hundreds of digits long. The miniature example below uses tiny numbers so that every step can be checked; real keys use primes more than 300 digits long.

Worked example. Key generation. Choose two primes, p = 5 and q = 11. Their product is n = 55, which will be public. Compute (p − 1)(q − 1) = 4 × 10 = 40. Choose a public exponent e = 3, which shares no factor with 40. Find the private exponent d such that e × d leaves a remainder of 1 when divided by 40: d = 27 works, because 3 × 27 = 81 = 2 × 40 + 1. The public key is (n = 55, e = 3); the private key is d = 27.

Encryption. To send the message m = 9, compute c = me mod n = 93 mod 55 = 729 mod 55. Since 55 × 13 = 715, the remainder is 729 − 715 = 14. The ciphertext is 14.

Decryption. The receiver computes 1427 mod 55, which equals 9, recovering the original message. Anyone who knew only n = 55 would need to factor it into 5 × 11 to find d, trivial here but practically impossible for a 2,048-bit number.

In practice RSA is rarely used to encrypt messages directly, because it is slow and needs careful padding of the message to be secure. Instead it, or another public-key method, is used to agree on or protect a symmetric key, and to sign data.

Putting the pieces together

Real systems combine all three families, playing to each one's strengths. When your browser connects to a secure website using TLS, the website presents a certificate: its public key, signed by a trusted certificate authority, whose signature your browser can check with a public key it already holds. Your browser and the website then use public-key methods to agree on a fresh symmetric key without ever sending it across the network, and from that point all traffic is encrypted with fast symmetric encryption such as AES. Hashes inside the protocol detect any tampering with the messages. The next chapter looks in detail at how two parties can agree on a shared secret in public.

A digital signature works similarly. To sign a document, software hashes it and processes the short hash with the signer's private key. Anyone can check the signature using the signer's public key: if the document has been altered by even one character, its hash will differ and the check will fail. Signatures protect software updates, so that your device installs only code genuinely produced by its maker, and they underpin the email-authentication method DKIM. Cryptography is powerful, but it protects only what it is applied to: a strong cipher is no help if the key is stored in plain text next to the data, or if a user is tricked into handing over their password.

Check your understanding

  1. Encrypt the word CIPHER with a Caesar shift of 4, then explain how an attacker would break it.
  2. Explain the key distribution problem and how public-key cryptography solves it.
  3. Give one use each for symmetric encryption, asymmetric encryption and hashing.
  4. At 1012 keys per second, roughly how long would it take to try every 64-bit key? Show your working.
  5. Using the miniature RSA keys from the worked example (n = 55, e = 3), encrypt the message m = 4.
  6. Explain why a website uses both public-key and symmetric encryption during a single secure connection.

Install Chrysolites Open Courses

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.