Compression, Encryption and Hashing — OCR A-Level Computer Science
Compression, Encryption and Hashing explained
Compression, Encryption and Hashing covers 4 specification points, including (a) Lossy vs Lossless compression.
Read the full explanation
and (b) Run length encoding and dictionary coding…. Open a point below for its explanation and revision guidance.
(a) Lossy vs Lossless compression.
Compression reduces the number of bits needed to store or transmit data. Lossy compression permanently discards data judged less important, so the original cannot be restored exactly. JPEG images and MP3 audio are common examples, where the discarded detail is chosen because people perceive it poorly. Lossless compression reduces file size without losing any data, so decompression returns an identical copy of the original. This suits text, program files and spreadsheets, where every value must stay intact. When answering a multiple-choice question, test each option by asking whether the original can be recovered exactly. If it cannot, the option describes lossy compression.
(b) Run length encoding and dictionary coding for lossless compression.
Run length encoding (RLE) and dictionary coding are two lossless methods, so the original data can be rebuilt exactly. RLE replaces each run of identical values with a count and the value: AAAABBBCCD becomes 4A3B2C1D. It suits data with long runs, but ABC becomes 1A1B1C, which is larger than the original. Dictionary coding stores each repeated pattern once in a table and replaces later occurrences with short codes that point to its entry. Decompression needs that dictionary to restore the original, so it must be stored or sent with the compressed data. Dictionary coding suits text with repeated words or phrases.
(c) Symmetric and asymmetric encryption.
Symmetric encryption uses one shared secret key to both encrypt and decrypt data, so sender and recipient must both hold the same key. It is fast, which suits bulk data, but the shared key must be exchanged securely beforehand, creating a key distribution problem. Asymmetric encryption uses a mathematically linked key pair: a public key, which can be shared openly, encrypts data, and a private key, held only by its owner, decrypts it. This avoids sharing a secret in advance, but it is slower, so practical systems often use asymmetric encryption to exchange a symmetric session key. In an exam, compare the two methods using a concrete scenario such as online banking.
(d) Different uses of hashing.
Hashing applies a one-way function to data of any size, producing a fixed-length output called a hash; a small change to the input gives a very different output, and the original cannot practically be recovered. Storing a hash of each password, rather than the password itself, means a stolen database does not directly reveal passwords; login hashes the entered password and compares the results. Hashes also verify integrity: a recipient who hashes a downloaded file and matches the published hash can be confident it was not altered. Digital signatures sign a message's hash, which is faster than signing the whole message. Name a use and explain which property makes hashing suitable for it.
Your focus
- Distinguish lossy from lossless compression by whether the original data can be restored exactly.
- Identify correct examples of lossy and lossless file formats, such as JPEG, MP3, PNG and ZIP.
- Justify the choice of compression type for a given use case by referring to whether data loss is acceptable.
Show all 12 objectives
- Encode a short string using run length encoding by counting consecutive identical characters.
- Describe how dictionary coding replaces repeated patterns with codes that refer to a stored dictionary.
- Explain why both methods are lossless and identify which method suits a given data pattern.
- Describe how symmetric encryption uses one shared key for encryption and decryption.
- Explain why asymmetric encryption relies on a linked public and private key pair.
- Compare symmetric and asymmetric encryption in terms of key management and speed.
- Describe how hashing is used to store passwords securely.
- Explain how a hash can verify the integrity of data.
- Explain how hashing is used within digital signatures.
Compression, Encryption and Hashing exam tips
Marking Points
- Defines lossy compression as permanently removing data, so the original cannot be restored exactly.
- Defines lossless compression as reducing file size without losing data, so decompression produces an identical copy.
- Gives JPEG or MP3 as an example of lossy compression and links it to data that people perceive poorly.
- Gives PNG or ZIP as an example of lossless compression and links it to files that must remain exact.
- Selects the option that matches reversibility of the process rather than file size alone.
- Explains that run length encoding stores each run as a count and a value, so four consecutive A characters become 4A.
- Works through a worked example such as AAAABBBCCD becoming 4A3B2C1D.
- Identifies that dictionary coding replaces repeated patterns with short codes that point to stored dictionary entries.
- States that both methods are lossless because the original can be rebuilt exactly during decompression.
- Recognises that run length encoding suits data with long runs, while dictionary coding suits repeated words or phrases.
- States that symmetric encryption uses one shared key for both encryption and decryption.
- Explains that asymmetric encryption uses a linked public and private key pair, with the public key encrypting and the private key decrypting.
- Identifies the key distribution problem as the main weakness of symmetric encryption.
- Compares speed by stating that symmetric encryption is faster and asymmetric encryption is slower.
- Justifies a choice of method for a given scenario by linking key management and speed to that scenario.
- Identifies password storage as a use, where stored hashes are compared with hashes of entered passwords.
- Explains that hashing verifies data integrity by comparing a hash of received data with a published hash.
- Describes digital signatures as signing a hash of the message rather than the whole message.
- Explains that the one-way and fixed-length properties make hashing suitable for these uses.
- Links each use to the property of hashing that makes it suitable.
Examiner Tips
- 💡Apply one test to every option: can the original be restored exactly? That single check separates the two methods.
- 💡Memorise one named example for each type so you can recognise it in a question stem.
- 💡When a question gives a use case, decide whether losing data would matter there before choosing lossy or lossless.
- 💡When a question gives a string, work through it letter by letter and count each run carefully before writing the encoded form.
- 💡Match the method to the pattern in the question: long runs suggest run length encoding, repeated phrases suggest dictionary coding.
- 💡Check that any answer about dictionary coding mentions that the dictionary must be available for decompression.
- 💡Name both key types in your answer and state which key performs each step.
- 💡When comparing, pair each advantage with a disadvantage and a reason rather than listing features separately.
- 💡Use the scenario in the question to justify your choice, for example by explaining why asymmetric encryption can be used to share a symmetric session key.
- 💡Give one specific use and explain it fully, rather than listing several uses without explanation.
- 💡Connect the property of hashing to the use, for example by explaining why one-way behaviour protects stored passwords.
- 💡Check that you describe what is hashed in each use, such as the password, the file or the message.
Common Mistakes
- Claims lossy compression can be reversed to recover the exact original; correct this by stating that discarded data cannot be recovered.
- Assumes lossless compression always gives the smallest file; correct this by recognising that lossless files are usually larger than lossy versions of the same content.
- Treats lossy compression as acceptable for executable programs; correct this by noting that a single changed bit can stop a program working.
- Describes JPEG as lossless; correct this by recording JPEG as a lossy format.
- Describes run length encoding as working best on random data; correct this by noting that random data has few runs, so the encoded file may be larger.
- Suggests the dictionary is unnecessary during decompression; correct this by stating that the decompressor needs the dictionary entries to restore the original.
- Claims run length encoding removes repeated values; correct this by noting that every run is kept in full through its count, so nothing is lost.
- Describes dictionary coding as lossy because data is replaced by codes; correct this by noting that the codes map back to the original exactly.
- Describes asymmetric encryption as using one key for both steps; correct this by stating that two different keys in a linked pair are used.
- Says the public key decrypts the message; correct this by stating that the public key encrypts and the private key decrypts in confidentiality use.
- Claims asymmetric encryption is always more secure than symmetric encryption; correct this by explaining that the difference lies in key management and speed, not an absolute level of security.
- Says hashes can be decrypted to recover the original password; correct this by stating that hashing is one-way, so the entered password is hashed and the results compared.
- Treats hashing as a form of encryption that hides data; correct this by stating that a hash does not keep data confidential and is used for integrity and verification.
- Claims a hashed password cannot be attacked; correct this by noting that weak passwords can be guessed by hashing candidate passwords and comparing the results.