Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Data Compression and Encryption

4.5.6.9 Data compression·4.5.6.10 Encryption

Aligned to the AQA 7517 specification

Level
Intermediate
Reading time
6 min
Published
13 June 2026
Updated
1 July 2026
On this page
  1. 1.Why Compress Data?
  2. 2.Lossless Compression: Run-Length Encoding
  3. 3.Lossy Compression
  4. 4.Encryption: Core Concepts
  5. 5.The Caesar Cipher
  6. 6.The Vernam Cipher (One-Time Pad)
  7. 7.Common Exam Mistakes

Key takeaways

  • Lossless compression (such as RLE and dictionary-based LZW) lets the original data be reconstructed perfectly, while lossy compression permanently discards some data to achieve much higher compression ratios.
  • Run-length encoding replaces consecutive repeated values with a (value, count) pair and is most effective on long runs of the same value, performing poorly on complex images with many colour changes.
  • Encryption transforms plaintext into ciphertext using a cipher and a key; the Caesar cipher shifts each letter by a fixed amount and has only 25 keys, making it breakable by exhaustive key search and frequency analysis.
  • The Vernam cipher (one-time pad) XORs each plaintext bit with a key bit and is provably perfectly secure only if the key is truly random, at least as long as the message, used exactly once, and kept completely secret.
  • AES and RSA are only computationally secure, relying on computational hardness rather than mathematical proof, so unlike the correctly used Vernam cipher they are not proven unbreakable.

Why Compress Data?

Uncompressed data files are large. Compression reduces the number of bits needed to represent data, reducing both:

  • Storage requirements (fit more data on a disk)
  • Transmission time (send files faster over a network)

A 1920×1080 uncompressed 24-bit image takes about 6 MB. The same image as a JPEG may be 200–500 KB — a 10–30× reduction.

Two fundamentally different approaches:

LosslessLossy
Data recovered?Yes — perfectlyNo — some data permanently lost
Compression ratioModerate (2–5×)High (10–100×)
Suitable forText, executables, source code, medical imagesPhotos (JPEG), audio (MP3), video (H.264)
Reversible?YesNo

Lossless Compression: Run-Length Encoding

Run-length encoding (RLE) replaces consecutive repeated values with a (value, count) pair.

Example — compressing a bitmap row:

Original (15 values): A A A A B B B C C D D D D D D
RLE:                   (A,4) (B,3) (C,2) (D,6)  = 8 values

8 stored values vs 15 original — nearly 50% reduction.

RLE is most effective when the data contains long runs of the same value. It performs poorly on complex images with many colour changes (it may even increase the file size).

Dictionary-based compression (e.g. LZW, used in PNG and GIF): builds a dictionary of repeated patterns found in the data. Longer repeated substrings are replaced with short dictionary codes. Effective on text and structured data.

Both RLE and dictionary-based compression are lossless — the original data can be perfectly reconstructed.

Lossy Compression

Lossy compression permanently discards some data to achieve much higher compression ratios.

How it works (JPEG example):

  1. The image is divided into 8×8 blocks of pixels
  2. Each block is transformed into frequency components (DCT)
  3. High-frequency detail (fine textures) — imperceptible to the human eye — is discarded
  4. Remaining values are encoded efficiently

The discarded data is gone. Repeatedly saving a JPEG increases the visible degradation ("compression artefacts").

Use cases:

  • JPEG: photographs — lossy is acceptable because photos contain more detail than the eye can discern
  • MP3: audio — removes sounds outside human hearing range and quieter sounds masked by louder ones
  • Video codecs (H.264, H.265): store only changes between frames; discard imperceptible detail

Why not use lossless for everything? Lossless compression ratios are insufficient for video and audio — a 2-hour film in lossless format would be hundreds of gigabytes.

Encryption: Core Concepts

Encryption transforms plaintext (readable data) into ciphertext (unreadable data) using a cipher (algorithm) and a key. Only authorised parties with the key can decrypt it.

TermMeaning
PlaintextThe original readable message
CiphertextThe encrypted (unreadable) output
CipherThe algorithm used to encrypt and decrypt
KeyA value that controls the cipher's operation
EncryptionPlaintext → Ciphertext (using key)
DecryptionCiphertext → Plaintext (using key)

The same plaintext encrypted with different keys produces different ciphertext. Without the key, the ciphertext should be computationally infeasible to reverse.

Want more lessons like this one?

Generate lessons on anything you study. Free account, no card needed.

Start generating

The Caesar Cipher

The Caesar cipher is a substitution cipher that shifts each letter of the plaintext by a fixed number of positions in the alphabet.

Key = shift amount (1–25).

Example — encrypt HELLO with key = 3 (shift right by 3):

PlaintextHELLO
Shift +3KHOOR
CiphertextKHOOR

Decryption: shift back by 3.

Weaknesses:

  • Only 25 possible keys — an attacker can try all 25 in seconds (exhaustive key search)
  • Vulnerable to frequency analysis: in English text, 'E' is most common; if 'X' is most common in the ciphertext, the key is likely 23

The Caesar cipher provides essentially no security by modern standards.

The Vernam Cipher (One-Time Pad)

The Vernam cipher (one-time pad) XORs each bit of the plaintext with the corresponding bit of a secret key.

Conditions for perfect security:

  1. The key must be truly random (not pseudo-random)
  2. The key must be at least as long as the message
  3. The key must be used exactly once and never reused
  4. The key must be kept completely secret

Worked example:

Plaintext:  1011 0011
Key:        0110 1100
Ciphertext: 1101 1111  (XOR each bit)

Decrypt:
Ciphertext: 1101 1111
Key:        0110 1100
Plaintext:  1011 0011  ✓

Why it is provably perfectly secure: if the key is truly random, every possible plaintext that produces the given ciphertext is equally likely. No amount of computation can determine the plaintext without the key.

Practical limitations: distributing a key as long as the message securely is difficult. One-time pads are used in high-security diplomatic communications but are impractical for general use.

Computational security: all other ciphers (AES, RSA, etc.) rely on computational hardness — breaking them requires more computation than is currently feasible, but they are not mathematically proven unbreakable (unlike the Vernam cipher).

Common Exam Mistakes

1. Claiming lossy compression can be reversed

Lossy compression permanently discards data. The original file cannot be recovered from a lossy-compressed version. "Decompressing" a JPEG gives the approximation stored in the file, not the original photograph.

2. Stating the Caesar cipher is secure because the alphabet is large

The number of keys (25) is tiny — not the alphabet size. An attacker tests all 25 possible shifts in seconds. Security depends on key space size, not alphabet size.

3. Confusing the Vernam cipher with general XOR encryption

XOR with a short repeated key is not a Vernam cipher and is easily broken. The Vernam cipher requires a key as long as the message, used only once. Short or repeated keys destroy the perfect security guarantee.

4. Overstating the security of AES/RSA

AES and RSA are computationally secure — no practical attack is known with current technology. They are not mathematically proven unbreakable. Only the Vernam cipher (with correct use) is provably perfectly secure.

Key terms

Lossless compression
Compression where the original data can be reconstructed perfectly, giving moderate (2–5×) compression ratios.
Lossy compression
Compression that permanently discards some data to achieve much higher (10–100×) compression ratios; the original cannot be recovered.
Run-length encoding (RLE)
A lossless method that replaces consecutive repeated values with a (value, count) pair, most effective on long runs of the same value.
Dictionary-based compression
A lossless method (e.g. LZW, used in PNG and GIF) that builds a dictionary of repeated patterns and replaces longer repeated substrings with short codes.
Plaintext
The original readable message before encryption.
Ciphertext
The encrypted, unreadable output of a cipher.
Cipher
The algorithm used to encrypt and decrypt data.
Key
A value that controls the cipher's operation; the same plaintext with different keys produces different ciphertext.
Caesar cipher
A substitution cipher that shifts each letter of the plaintext by a fixed number of positions in the alphabet, with the key being the shift amount (1–25).
Frequency analysis
An attack that exploits known letter frequencies in a language, such as 'E' being most common in English, to deduce a cipher's key.
Vernam cipher (one-time pad)
A cipher that XORs each plaintext bit with a key bit and is provably perfectly secure if the key is truly random, at least as long as the message, used once, and kept secret.
Computational security
Security that relies on breaking a cipher requiring more computation than is currently feasible, rather than being mathematically proven unbreakable, as with AES and RSA.

Frequently asked questions

Lossless compression lets the original data be recovered perfectly and gives moderate ratios (2–5×), suiting text, executables, source code, and medical images. Lossy compression permanently discards some data to reach much higher ratios (10–100×) and suits photos (JPEG), audio (MP3), and video (H.264). A lossy file cannot be restored to the original.

Because it has only 25 possible keys, an attacker can try all 25 shifts in seconds by exhaustive key search. It is also vulnerable to frequency analysis: in English, 'E' is most common, so if 'X' is most common in the ciphertext the key is likely 23. Security depends on key space size, not alphabet size.

Because it XORs the plaintext with a truly random key at least as long as the message, used once and kept secret, every possible plaintext that produces the given ciphertext is equally likely, so no amount of computation can determine the plaintext without the key. XOR with a short or repeated key is not a Vernam cipher and is easily broken.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Images, Sound and Data Representation

Next

Software, Operating Systems and System Software

Related lessons

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.