← QCE Digital Solutions Unit 4

Order of compression, encryption and hashing

Compress first, then encrypt, and hash to check integrity. Encrypted data looks random so it cannot be compressed. On receiving, reverse the order: decrypt, then decompress.

Understanding Order of compression, encryption and hashing in QCE Digital Solutions Unit 4

What it does: Compressing while the data still has patterns is effective. The compressed bytes are then encrypted, so the encryption comes second. The sender hashes the original data and the receiver hashes the restored data. If the two hashes match, the data was not changed in transit. Compression works by finding patterns and repetition. Good encryption removes them, so the output looks random and barely shrinks. On receiving, reverse the sender's order. The last step applied (encryption) is the first one undone. Plaintext has patterns that compression can use. Compression does not change the key. Its job is to reduce size and transmission time.

Common mistakes: Before sending, the data must be compressed, not decompressed. Line 2 should be small = compress(data). Decompressing happens only at the receiver. Compression must happen before encryption. Encrypted data looks random, so it cannot be compressed. Line 2 should be small = compress(data).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 17–88) and word search (Modules 17–125).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A student wants to send a large file securely over the internet. Which order should the sender use?
Compress the file, then encrypt itEncrypt the file, then compress it
Matches
send = compress(encrypt(data))
↓
Encrypting before compressing. Encrypted data looks random, so it has no patterns for compression to remove and the file barely shrinks.
Applying
Meanings
Term
compress then encrypt
Meaning
Compress first, then encrypt, and hash to check integrity. Encrypted data looks random so it cannot be compressed. On receiving, reverse the order: decrypt, then decompress.
Practice
data ← READ file
Testing
Gaps
packed = (message)
Check Answer
Spots
1data = read("notes.txt")
2small = decompress(data)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Hippopotamus' badge — Savannah Titan, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A student wants to send a large file securely over the internet. Which order should the sender use?
A. Compress the file, then encrypt it ✓
B. Encrypt the file, then compress it
Encrypted data looks random, so there are no patterns left for compression to remove. Compress first, then encrypt.
2. Why does compressing AFTER encryption save almost no space?
A. Compression algorithms cannot read files that have a key
B. Encrypted data looks random, so it has no repeating patterns to shrink ✓
Compression works by finding patterns and repetition. Good encryption removes them, so the output looks random and barely shrinks.
3. A receiver gets a file that was compressed and then encrypted. What should the receiver do first?
A. Decrypt it, then decompress it ✓
B. Decompress it, then decrypt it
On receiving, reverse the sender's order. The last step applied (encryption) is the first one undone.
4. Which sequence correctly matches what the sender does and what the receiver does?
A. Sender: compress, encrypt. Receiver: decompress, decrypt
B. Sender: compress, encrypt. Receiver: decrypt, decompress ✓
The receiver undoes the steps in reverse order: decrypt first, then decompress.
5. What is the purpose of adding a hash to the process?
A. To check the data has not been changed or corrupted ✓
B. To make the file smaller before it is sent
A hash is a fixed-length fingerprint of the data. If the receiver's calculated hash matches the original, the data's integrity is confirmed. Compression is what reduces file size.

Match the Pairs

send = encrypt(compress(data)) → Compress the data first, then encrypt the compressed result before sending
data = decompress(decrypt(received)) → On receiving, reverse the order: decrypt first, then decompress
Encrypted data looks random, so compressing it afterwards saves almost no space. Compress before encrypting. → packed = compress(data) secret = encrypt(packed)
A hash of the original data lets the receiver check integrity once the data has been decrypted and decompressed. → digest = hash(data) same = hash(restored) == digest
send = compress(encrypt(data)) → Encrypting before compressing. Encrypted data looks random, so it has no patterns for compression to remove and the file barely shrinks.
data = decrypt(decompress(received)) → The receiving steps are in the wrong order. The data arrives encrypted, so it must be decrypted before it can be decompressed.

Key Term

compress then encrypt
Compress first, then encrypt, and hash to check integrity. Encrypted data looks random so it cannot be compressed. On receiving, reverse the order: decrypt, then decompress.

Fill in the Blanks

1. packed = compress(message) secret = encrypt(packed, key) send(secret)
Hint: Type the function for the step done first when sending, shrinking the data before it is locked.
2. packed = compress(message) secret = encrypt(packed, key) send(secret)
Hint: Type the function that scrambles the already shrunk data so an interceptor cannot read it.
3. packed = compress(message) check = hash(packed) secret = encrypt(packed, key)
Hint: Type the function that creates a fixed-length fingerprint used to verify integrity.
4. packed = decrypt(secret, key) message = decompress(packed) print(message)
Hint: Type the function that undoes the last step done when sending. It is the first step on receiving.
5. packed = decrypt(secret, key) message = decompress(packed) print(message)
Hint: Type the function that reverses the first sending step and restores the original size. It comes last when receiving.

Spot the Error

Question 1
A data = read("notes.txt")
B small = decompress(data)
C secret = encrypt(small, key)
D send(secret)
Line B — Before sending, the data must be compressed, not decompressed. Line 2 should be small = compress(data). Decompressing happens only at the receiver.
Question 2
A data = read("log.txt")
B small = compress(secret)
C secret = encrypt(small, key)
D save(secret, "log.bin")
Line B — Compression must happen before encryption. Encrypted data looks random, so it cannot be compressed. Line 2 should be small = compress(data).
Question 3
A data = read("form.csv")
B check = hash(key)
C small = compress(data)
D secret = encrypt(small, key)
E send(secret, check)
Line B — The hash is used to check the integrity of the data, so it must be made from the data, not the key. Line 2 should be check = hash(data).
Question 4
A data = read("score.txt")
B check = hash(data)
C small = compress(data)
D secret = decrypt(small, key)
E send(secret, check)
Line D — When sending, the compressed data must be encrypted, not decrypted. Line 4 should be secret = encrypt(small, key).
Question 5
A packed = decompress(secret)
B data = decompress(packed)
C show(data)
Line A — On receiving, the order is reversed: decrypt first, then decompress. The data arrives encrypted, so line 1 should be packed = decrypt(secret, key).

Student with a class code? Join your class →