Data compression and encryption

A-Level Computer Science · Floating Point, Compression and Software Development

Lossless and lossy

Lossless compression reconstructs the original exactly — required for text, program code, spreadsheets and anything where a changed byte matters. Lossy compression discards information the human eye or ear is unlikely to notice, achieving much smaller files, and is used for photographs, audio and video. The choice is decided by one question: does the original have to come back exactly?

Run length encoding

Replaces runs of identical values with a count and the value: AAAAABBB becomes 5A3B. It works well on images with large flat areas and badly on data with few repeats — on random data it can make the file larger, and saying that shows real understanding.

Dictionary compression

Builds a dictionary of repeated sequences and stores an index instead of the sequence. Effective on text, where words repeat. The dictionary itself must be stored or reconstructible, which is part of the cost.

Symmetric and asymmetric encryption

Symmetric uses one key for both encryption and decryption: fast, but the key must reach the other party securely, and that is the hard problem. Asymmetric uses a public key to encrypt and a private key to decrypt, solving key distribution at the cost of speed. In practice asymmetric is used to exchange a symmetric session key, then symmetric does the bulk work.

Digital signatures and certificates

A digital signature reverses the roles: the sender encrypts a hash of the message with their private key, and anyone can decrypt it with the public key — which proves it came from that sender and that the message has not changed. A digital certificate from a certification authority binds a public key to an identity, which is what stops an attacker publishing their own key under someone else's name.

Hashing is not encryption

A hash is one-way: it cannot be reversed to recover the input. That is exactly why passwords are stored hashed — a stolen database does not reveal the passwords themselves.

Worked example: choose a compression method and explain an encrypted transaction

(a) A system stores three types of file: medical records as text, X-ray images, and recordings of consultation calls. Choose the type of compression for each and explain.

(b) Describe the security steps when a user sends a bank card number through a website that uses HTTPS.

Solution

(a) Medical records as text — LOSSLESS compression, using dictionary compression. One wrong character in a drug dose could have serious consequences, so the original must be reconstructed exactly. Dictionary compression is effective here because medical terms are repeated very often.

X-ray images — LOSSLESS compression, even though they are images. This is where the question sets a trap: images normally use lossy compression, but diagnostic images do not — a faint detail discarded by the algorithm could be the very sign of a disease. The deciding rule is not 'what type of file is this' but 'must the original come back exactly'.

Recordings of consultation calls — LOSSY compression is acceptable. The purpose is to listen and understand the content again, not to analyze the signal, so discarding frequencies outside the range of clear hearing is reasonable and saves a great deal of storage.

(b) Security steps of an HTTPS transaction

$1.$ The browser connects to the server and requests a digital certificate.

$2.$ The server sends the certificate, which contains its public key and the signature of a certificate authority. The browser checks that signature to confirm the public key really belongs to this website — this step stops an attacker from inserting their own key in the middle.

$3.$ The browser generates a random symmetric session key, encrypts it with the server's public key and sends it. Only the server, which has the matching private key, can decrypt it.

$4.$ From this point on, all data — including the card number — is encrypted with the symmetric session key, because it is much faster than asymmetric encryption.

Why the design is a hybrid like this: asymmetric encryption solves the key distribution problem but is too slow for large volumes of data; symmetric encryption is fast but cannot hand over a key securely by itself. Combining the two gets the advantages of both — and this is exactly the answer the question wants.

Common mistake

Saying asymmetric encryption is 'more secure' and so should be used for everything. It solves the KEY DISTRIBUTION problem rather than being inherently stronger, and it is much slower — which is why in practice it is used only to exchange a symmetric key and then the system switches to symmetric.

Key takeaway

Choose the type of compression with the question 'must the original come back exactly', not by file type. Run-length encoding can make a file bigger. Asymmetric to exchange keys, symmetric to do the work. And hashing is one-way, so it is completely different from encryption.

← Floating point representation · All A-Level Computer Science lessons · Practice A-Level Computer Science questions (free, instant scoring) · Software development and testing strategies →