impera Open Impera

A levels Computer Science · Data representation

Encryption

1Why encrypt?

Encryption turns readable plaintext into unreadable ciphertext using an algorithm (a cipher) and a key. Only someone with the right key can decrypt it.

Encryption does not stop data being intercepted; it stops the interceptor understanding it. It protects passwords, online banking, messages and stored files.

Check yourself

What does encryption prevent?

  • Viruses

  • An interceptor understanding the data

  • The data being intercepted

  • The data being deleted

Show the answer

An interceptor understanding the data

Unreadable, not unreachable.

2The Caesar cipher

The Caesar cipher shifts every letter a fixed number of places along the alphabet. The shift is the key.

Worked example

Shift of 3

  1. H → K, E → H, L → O, L → O, O → R.
  2. HELLO becomes KHOOR.

It is very easy to break:

  • there are only 25 possible keys, so all can be tried (brute force);
  • frequency analysis: the most common letter in English is E, so the most common letter in the ciphertext is probably E.
Check yourself

Encrypt CAT with a Caesar shift of 2.

  • CAT

  • AYR

  • DBU

  • ECV

Show the answer

ECV

Each letter +2.

3The Vernam cipher

The Vernam cipher (one-time pad) combines each bit of the plaintext with a bit of the key using XOR.

Worked example

XOR with a key

  1. Plaintext 'A' = 0100 0001. Key = 0011 0101.
  2. XOR (1 if the bits differ): 0111 0100.
  3. Decrypt: 0111 0100 XOR 0011 0101 = 0100 0001 = 'A'. The same key undoes it.

The Vernam cipher has perfect security, provided the key is:

  • truly random;
  • at least as long as the message;
  • used only once, then destroyed.

Every possible plaintext of that length is equally likely, so even unlimited computing power cannot break it. Its weakness is sharing such long keys securely.

Check yourself

What makes the Vernam cipher unbreakable?

  • A truly random key, as long as the message, used only once

  • A very short key that is easy to remember and share

  • Using a Caesar shift on the text before encrypting it

  • Repeating the key for every message

Show the answer

A truly random key, as long as the message, used only once

One-time pad.

4Symmetric and asymmetric encryption

SymmetricAsymmetric (public key)
the same key encrypts and decryptsa public key encrypts; only the matching private key decrypts
fastslower
the key must be shared secretly firstthe public key can be given to anyone
example: AESexample: RSA

In practice they are combined: asymmetric encryption is used to swap a symmetric key securely, then the fast symmetric key encrypts the rest of the conversation (this is how HTTPS works).

Check yourself

What is the main problem with symmetric encryption?

  • It cannot encrypt text, only numbers

  • It needs two different keys, a public and a private one

  • It is very slow compared with asymmetric encryption

  • The key has to be shared securely before use

Show the answer

The key has to be shared securely before use

Key distribution.

5Public keys and digital signatures

Public-key encryption. Anyone can lock a message with Bob's public key, but only Bob's private key can unlock it.

A digital signature proves who sent a message and that it has not been changed:

  1. The sender makes a hash (a short fingerprint) of the message.
  2. The sender encrypts the hash with their private key: this is the signature.
  3. The receiver decrypts the signature with the sender's public key, hashes the message themselves, and compares.
  4. If the hashes match, the message is genuine and unchanged.

A digital certificate, issued by a trusted certificate authority, links a public key to its owner, so you know a website's public key really belongs to that website.

Check yourself

Which key does the sender use to create a digital signature?

  • The receiver's private key

  • The receiver's public key

  • A shared symmetric key

  • Their own private key

Show the answer

Their own private key

Only the sender has it.

6Hashing

A hash function turns data of any length into a fixed-length value. It is one-way: you cannot work back from the hash to the data.

  • Passwords are stored as hashes. When you log in, your typed password is hashed and compared, so the real password is never stored.
  • A salt (random data added before hashing) means two users with the same password get different hashes, which defeats pre-computed tables of common passwords.
  • Hashes also check that downloaded files have not been changed.

Hashing is not encryption: encryption can be reversed with the key, but a hash cannot be reversed.

Check yourself

Why are passwords stored as hashes rather than encrypted?

  • Hashes can be decrypted by the website when the user logs in

  • Encryption of passwords is not allowed under data protection law

  • Hashes are longer, so they are harder to guess than encrypted passwords

  • A hash cannot be reversed, so a stolen database does not reveal the passwords

Show the answer

A hash cannot be reversed, so a stolen database does not reveal the passwords

One-way.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera