30% off your first 3 months

Get the deal

How encryption keeps messages private

by DuckyHelperNov 12, 20264 min read
Computer science

TL;DR:

  • Encryption scrambles a message with a key so that only someone with the right key can read it.

  • A Caesar cipher shifts every letter by a fixed amount. It has only 25 useful keys, so anyone can try them all.

  • Public-key encryption uses two linked keys: a public one anyone can use to lock a message, and a private one that only the owner uses to unlock it.

When you send a message over the internet it passes through many machines you do not control. Encryption is what makes it unreadable on the way. The oldest ideas are simple enough to do by hand, and they show both how encryption works and why a weak one fails.

The idea in plain words

Plaintext is the readable message. Ciphertext is the scrambled version. A cipher is the method of scrambling, and a key is the secret value that controls it. A good cipher is public, and only the key is secret. This lets people study the method and find weaknesses, which makes it safer.

Worked example: a Caesar cipher

The Caesar cipher replaces each letter with the one a fixed number of places later in the alphabet. With the key 3, A becomes D, B becomes E, and so on, wrapping from Z back to A.

Encrypt HELLO with key 3:

  1. H is the 8th letter. Shift by 3 to the 11th: K.

  2. E is the 5th. Shift to the 8th: H.

  3. L is the 12th. Shift to the 15th: O. The second L gives O again.

  4. O is the 15th. Shift to the 18th: R.

The ciphertext is KHOOR. To decrypt, shift each letter back by 3: K→H, H→E, O→L, O→L, R→O. Here is the same thing in Python.

def caesar(text, shift):
    out = ""
    for ch in text:
        if ch.isalpha():
            base = ord("A") if ch.isupper() else ord("a")
            out += chr((ord(ch) - base + shift) % 26 + base)
        else:
            out += ch
    return out

print(caesar("HELLO", 3))
print(caesar("KHOOR", -3))

The output is KHOOR then HELLO. The % 26 wraps around the alphabet: for X (position 23, counting from 0) with shift 3, (23 + 3) % 26 = 0, which is A.

Why simple ciphers break

  • Few keys. There are only 25 useful shifts. Anyone can try them all in seconds and see which gives readable text.

  • Letter patterns. Every E becomes the same letter. In English text some letters are far more common than others, so the most common letter in the ciphertext likely stands for E. That is called frequency analysis, and it breaks any cipher that always maps a letter to the same letter.

  • Shared secret. Both people must already know the key. How do you send the key without someone seeing it?

Modern ciphers fix the first two problems by using enormous keys and scrambling many times. The third problem needs a different idea.

Public and private keys

In public-key cryptography, each person has two linked keys. The public key can be shared with the world. A message locked with it can only be unlocked with the matching private key, which never leaves its owner. Think of a padlock: anyone can snap it shut, but only the key holder can open it.

So to send you a secret, I use your public padlock. I never need your private key, and no secret key has to cross the network. A well-known system called RSA does this with arithmetic that is easy in one direction and very hard to reverse.

Here is a toy RSA with tiny numbers. Never use numbers this small, or this code, for anything real. Take primes 3 and 11. Then n = 3 × 11 = 33. Pick public exponent e = 3 and private exponent d = 7, which works because 3 × 7 = 21, and 21 leaves remainder 1 when divided by 20 (that is (3 − 1) × (11 − 1)). The public key is (33, 3), and the private key is d = 7.

  1. Encrypt the message m = 4: 4³ = 64, and 64 mod 33 = 31. The ciphertext is 31.

  2. Decrypt: 31⁷ mod 33. Since 31 ≡ −2 (mod 33), we get (−2)⁷ = −128, and −128 + 132 = 4, with 132 = 4 × 33. The result is 4, the original message.

An attacker who sees 33, 3 and 31 can factor 33 into 3 and 11 instantly. With primes hundreds of digits long, factoring takes so long that it is impractical. That gap is the whole point.

How to check your answer

  • Decrypt your ciphertext. You should get the original text back exactly.

  • Check wrap-around: Y shifted by 3 should be B.

  • Check that spaces and punctuation are left alone.

  • In the toy RSA, check that e × d leaves remainder 1 when divided by 20.

Common mistakes

  • Thinking a long-looking ciphertext is secure. Security comes from the key size and the method.

  • Hiding the method and calling it secure. A good system stays safe even when the method is known.

  • Using a home-made cipher to protect real information. Use well-tested tools.

  • Sharing a private key. If it leaks, the lock is useless.

Where you will use it next

HTTPS, passwords and secure messaging all build on these ideas. Underneath, a cipher is built from the logic in boolean logic and truth tables, and the dictionary-style lookups behind password checks are covered in lists, dictionaries and sets.

Frequently asked questions

Is a Caesar cipher ever secure?

No. It is a good teaching tool and a puzzle, and nothing more.

Does encryption hide who I am talking to?

Not by itself. It protects the contents of the message, not the fact that a message was sent.

Get started

DuckyHelper can
do more

It sees your screen and works in any app you use. Try it for 10 minutes, no card.

Get it for Mac, Windows or Linux

Share

Questions and jobs

Do anything

Ask it a question or hand it a job, in any app.

Doing jobs for you is in early access on Mac. Asking works on Mac, Windows and Linux.