Order of compression, encryption and hashing

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Order of compression, encryption and hashing

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
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
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

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

Definitions

compress then encrypt

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
packed = ________(message) secret = encrypt(packed, key) send(secret)
2.
packed = compress(message) secret = _______(packed, key) send(secret)
3.
packed = compress(message) check = ____(packed) secret = encrypt(packed, key)
4.
packed = _______(secret, key) message = decompress(packed) print(message)
5.
packed = decrypt(secret, key) message = __________(packed) print(message)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A data = read("notes.txt")
B small = decompress(data)
C secret = encrypt(small, key)
D send(secret)
Question 2
A data = read("log.txt")
B small = compress(secret)
C secret = encrypt(small, key)
D save(secret, "log.bin")
Question 3
A data = read("form.csv")
B check = hash(key)
C small = compress(data)
D secret = encrypt(small, key)
E send(secret, check)
Question 4
A data = read("score.txt")
B check = hash(data)
C small = compress(data)
D secret = decrypt(small, key)
E send(secret, check)
Question 5
A packed = decompress(secret)
B data = decompress(packed)
C show(data)

Answer Key

Order of compression, encryption and hashing
Section A: Quick Questions
Q1
A — Compress the file, then encrypt it
Q2
B — Encrypted data looks random, so it has no repeating patterns to shrink
Q3
A — Decrypt it, then decompress it
Q4
B — Sender: compress, encrypt. Receiver: decrypt, decompress
Q5
A — To check the data has not been changed or corrupted
Section B: Match the Pairs
Q1
send = encrypt(compress(data)) → Compress the data first, then encrypt the compressed result before sending
Q2
data = decompress(decrypt(received)) → On receiving, reverse the order: decrypt first, then decompress
Q3
Encrypted data looks random, so compressing it afterwards saves almost no space. Compress before encrypting. → packed = compress(data) secret = encrypt(packed)
Q4
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
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
packed = compress(message) secret = encrypt(packed, key) send(secret)
Q2
packed = compress(message) secret = encrypt(packed, key) send(secret)
Q3
packed = compress(message) check = hash(packed) secret = encrypt(packed, key)
Q4
packed = decrypt(secret, key) message = decompress(packed) print(message)
Q5
packed = decrypt(secret, key) message = decompress(packed) print(message)
Section E: Spot the Error
Q1
B — Before sending, the data must be compressed, not decompressed. Line 2 should be small = compress(data). Decompressing happens only at the receiver.
Q2
B — Compression must happen before encryption. Encrypted data looks random, so it cannot be compressed. Line 2 should be small = compress(data).
Q3
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).
Q4
D — When sending, the compressed data must be encrypted, not decrypted. Line 4 should be secret = encrypt(small, key).
Q5
A — On receiving, the order is reversed: decrypt first, then decompress. The data arrives encrypted, so line 1 should be packed = decrypt(secret, key).