KS5H446 1.3.1 28 min read 10 question quiz 21 exam marks

Compression, Encryption and Hashing

Run length and dictionary encoding, symmetric and asymmetric encryption, and what a hash is actually for.

Compression

Lossy and lossless

Lossy permanently discards data judged least perceptible, giving very large reductions but no way back to the original. Used for images, audio and video.

Lossless reduces size by encoding the data more efficiently, and the original is restored exactly. Essential for text, program files, spreadsheets and anything where every byte matters.

Run length encoding

Consecutive identical values are replaced by the value and a count.

AAAAABBBCCCCCCCC becomes 5A 3B 8C

  • Extremely effective on data with long runs, such as simple graphics with large blocks of one colour
  • Can make a file larger if there are few runs, because each single value now needs a count as well
  • Lossless

Dictionary encoding

Repeated sequences are replaced with references to an index, and the dictionary is stored with the compressed data so it can be reversed.

the cat sat on the mat, the cat sat Dictionary: 1=the 2=cat 3=sat 1 2 3 on 1 mat, 1 2 3

  • Very effective on text, which is full of repeated words
  • The dictionary itself takes space, so short files may not benefit
  • Lossless, and the basis of ZIP and PNG
Choosing between them

Run length encoding suits data with long runs of identical values. Dictionary encoding suits data with repeated sequences that are not adjacent, such as natural language.

Lossless and lossy compression compared with the original fileCompression, with the length of each bar at the bottom standing for the size of the file. The original holds every byte of the data. Lossless compression finds repetition and records it more briefly: run length encoding replaces a run of identical values with the value and a count, so six As followed by three Bs becomes 6A 3B, and Huffman coding gives the most common symbols the shortest codes. Nothing is discarded, so unpacking returns exactly the original file, bit for bit, though the saving is modest. Lossy compression throws data away permanently, such as detail the eye is unlikely to miss, and so the file becomes much smaller but the original can never be recovered. Text and program code must be lossless because one wrong byte breaks them. Photos and music can usually be lossy.OriginalLosslessLossy12 MBevery byte of the data,nothing thrown awayfinds repetition and says it shorterAAAAAABBB6A3B6 MBunpack it: identical1 MBdetail thrown awayand never coming backOriginal12 MBLossless6 MBLossy1 MB1234
  1. The original file: every byte of the data, nothing thrown away.
  2. Lossless finds repetition and records it more briefly.
  3. Unpack it and you get back exactly what you started with.
  4. Lossy throws data away permanently, and gets much smaller.
Ask what happens if one byte changes. If the answer is that it breaks, the compression has to be lossless.
Lossless against lossy compression: a picture before and after, and when to use eachTwo ways to compress a file. With lossless compression a 5 megabyte picture is compressed to 3.2 megabytes using something like ZIP, PNG or FLAC, and when it is restored it is identical to the original, because nothing is lost. With lossy compression the same picture is compressed to 0.5 megabytes using something like JPEG, MP3 or MP4, and when it is restored it is not identical, because detail has been thrown away for good. Lossless suits text, programs, spreadsheets and medical scans, anything that must be exact. Lossy suits photos, music and video, where small losses go unnoticed and the files shrink a lot.Lossless1original5.0 MBcompressed3.2 MBrestoredidenticalZIP, PNG, FLACnothing is lost: every bit comesbackLossy2original5.0 MBcompressed0.5 MBrestorednot identicalJPEG, MP3, MP4detail is thrown away for goodWhich to use?3Losslesstext, programs, spreadsheets, medicalscans: anything that must be exactLossyphotos, music, video: small losses gounnoticed and the files shrink a lot
  1. Lossless: the file gets smaller, and decompressing gives back exactly the original.
  2. Lossy: the file gets much smaller, but detail is thrown away for good, so it cannot be restored exactly.
  3. Choose lossless when the data must be exact, and lossy when small losses do not matter.
Lossy files can never be restored to the original. That is the trade for a much smaller file.

Encryption and hashing

Symmetric encryption

The same key encrypts and decrypts.

  • Fast, so suitable for large volumes of data
  • The problem is key distribution: the key must reach the recipient securely, and if it is intercepted the encryption is worthless
  • With n people all communicating, the number of keys required grows as n(n-1)/2

Asymmetric encryption, public key

Two mathematically related keys. The public key may be distributed freely. The private key is never shared.

  • Anything encrypted with the public key can only be decrypted with the private key, so anyone can send you a secure message
  • Anything encrypted with the private key can be decrypted with the public key, which proves it came from the holder of the private key. This is a digital signature
  • Solves key distribution completely
  • Considerably slower than symmetric encryption

How they are used together

In practice HTTPS uses both. Asymmetric encryption is used at the start of the connection to exchange a symmetric session key securely, and all the actual data is then encrypted symmetrically because it is far faster. This takes the security of asymmetric key exchange and the speed of symmetric encryption.

Digital signatures and certificates

  1. The sender produces a hash of the message.
  2. The hash is encrypted with the sender's private key, producing the signature.
  3. The recipient decrypts it with the sender's public key and independently hashes the message.
  4. If the two hashes match, the message is unaltered and came from the holder of that private key.

A digital certificate issued by a certificate authority binds a public key to an identity, which is what stops an attacker simply publishing a public key claiming to be your bank.

Hashing

A hash function takes an input of any size and produces a fixed size output.

Properties of a good hash function:

  • Deterministic. The same input always gives the same output.
  • Fast to compute.
  • One way. It must be computationally infeasible to recover the input from the hash.
  • Avalanche effect. A tiny change to the input produces a completely different hash.
  • Collision resistant. It should be extremely difficult to find two inputs with the same hash.

Uses

  • Password storage. Store the hash rather than the password. A stolen database does not reveal the passwords. A salt, a random value added before hashing, ensures two identical passwords produce different hashes and defeats precomputed rainbow tables.
  • Hash tables. The hash of a key determines the storage index, giving average constant time lookup.
  • Integrity checking. Rehash a downloaded file and compare with the published hash to confirm nothing changed.
  • Digital signatures, as above.
Hashing is not encryption

Encryption is two way and reversible with a key. Hashing is one way by design. You never decrypt a hash, you hash the new input and compare.

Encryption: plaintext locked with a key, and hashing, which cannot be undoneEncryption, drawn as a journey. You begin with plaintext, the message as anybody could read it, here the word HELLO. An encryption algorithm combined with a key turns it into ciphertext, which looks like nonsense. The algorithm is usually public and well known; the secrecy lives entirely in the key, and the same algorithm with a different key produces completely different ciphertext. The ciphertext is what actually travels across the network, so an attacker who intercepts it, shown as a hooded figure with a magnifying glass, sees only nonsense. The right key reverses the process and the receiver gets the plaintext back; the wrong key gives nothing useful. Hashing is different. A hash function turns any input, such as a password, into a fixed length string, and it is one way: there is no way back from the hash to the original.HELLOplaintextkey + algorithmX#9@Qciphertextjust nonsense!HELLOthe right key unlocks itHashing is differentpassword123hash function5f4dcc3b5aa7...no wayback12345
  1. You start with plaintext: the message as anyone could read it.
  2. A key and an algorithm turn it into ciphertext.
  3. The ciphertext is what travels, and it is what an attacker sees.
  4. The right key reverses it. The wrong key gives nothing useful.
  5. Hashing is different: it is one way, and there is no way back.
The algorithm is public. The key is the secret. That one sentence answers most encryption questions.
A hash function turns any input into a fixed length hash, and how that is used to check a passwordA hash function turns any input into a fixed length value called a hash. The input hello gives one hash, the input Hello with a capital H gives a completely different one, and a whole book gives a hash of exactly the same length. The hashes shown are real SHA-256 values, cut short. A hash function has four properties: the same input always gives the same hash, it is one way so it cannot be reversed, a tiny change in the input gives a very different hash, and the hash is a fixed length however big the input is. A system can check a password without storing it: it stores only the hash, hashes what the user types, and lets them in if the two hashes match.A hash function1hellohashfunction2cf24dba5fb0a3...Hellohashfunction185f8db32271fe...a whole bookhashfunction82396ec9191a22...SHA-256 shown, cut short: the real hash is 64 hex digitsFour properties2Same input, same hashevery timeOne wayit cannot be reversedA tiny changegives a very different hashFixed lengthhowever big the inputChecking a password without storing it3hellohashmatch?typed password, hashed2cf24dba5fb0a3...the hash that was storedlet in
  1. The same hash function turns hello, Hello and a whole book into hashes of the same length. A tiny change gives a completely different hash.
  2. A hash is the same every time for the same input, cannot be reversed, and is a fixed length.
  3. Store only the hash of a password. When someone logs in, hash what they typed and compare it with the stored hash.
Hashing is not encryption: there is no key and no way back. That is why it suits passwords and checking that a file has not changed.

Key terms you must be able to define

Lossy compression
Compression that permanently discards data, giving large reductions but no exact recovery.
Lossless compression
Compression that allows the original data to be restored exactly.
Run length encoding
Replacing runs of identical consecutive values with the value and a count.
Dictionary encoding
Replacing repeated sequences with references to an index stored with the data.
Symmetric encryption
Encryption using the same key to encrypt and decrypt.
Asymmetric encryption
Encryption using a public key to encrypt and a matching private key to decrypt.
Key distribution problem
The difficulty of getting a symmetric key to the recipient securely.
Digital signature
A hash encrypted with a private key, proving authorship and that the message is unaltered.
Digital certificate
A document from a certificate authority binding a public key to a verified identity.
Hash function
A function producing a fixed size output from an input of any size, designed to be one way.
Salt
A random value added to a password before hashing so identical passwords hash differently.
Collision
Two different inputs producing the same hash value.

Flashcards

Say the definition out loud before you flip the card. Retrieving it from memory is what builds recall, reading it again is not.

DefineLossy compression
AnswerCompression that permanently discards data, giving large reductions but no exact recovery.
DefineLossless compression
AnswerCompression that allows the original data to be restored exactly.
DefineRun length encoding
AnswerReplacing runs of identical consecutive values with the value and a count.
DefineDictionary encoding
AnswerReplacing repeated sequences with references to an index stored with the data.
DefineSymmetric encryption
AnswerEncryption using the same key to encrypt and decrypt.
DefineAsymmetric encryption
AnswerEncryption using a public key to encrypt and a matching private key to decrypt.
DefineKey distribution problem
AnswerThe difficulty of getting a symmetric key to the recipient securely.
DefineDigital signature
AnswerA hash encrypted with a private key, proving authorship and that the message is unaltered.

Reach the top grade on this topic

  • Explain why run length encoding can increase file size, and on what data it works well
  • State the key distribution problem as the reason asymmetric encryption exists
  • Explain how HTTPS uses both types together, and why
  • Describe the digital signature process in four ordered steps
  • Never describe hashing as reversible, and explain salting as defeating rainbow tables

Mistakes that cost marks here

Avoid these
  • Saying hashing is a form of encryption. It is one way and cannot be reversed.
  • Claiming run length encoding always reduces size. On data with few runs it makes files larger.
  • Saying asymmetric encryption is better than symmetric. It is much slower, which is why both are used together.
  • Forgetting the salt when discussing password storage.
  • Confusing which key does what. Encrypt with the public key for secrecy, with the private key for a signature.
Fun fact

In asymmetric encryption you can publish your public key to the entire world and it still cannot be used to read messages sent to you. Only the matching private key can decrypt them, and deriving one from the other is computationally infeasible.

Knowledge check

0 of 10 answered
1When can run length encoding increase the size of a file?
2What problem does asymmetric encryption solve?
3Why does HTTPS use both symmetric and asymmetric encryption?
4What is the avalanche effect?
5Why are passwords stored as salted hashes?
6A digital signature is created by:
7Which compression method replaces repeated sequences with index references?
8What is a hash collision?
9Which key encrypts a message so that only the intended recipient can read it?
10What is the main disadvantage of symmetric encryption?
Score 0 / 10

Exam-style questions

Five written questions in the style of the real paper. Write a full answer, then let the marker check it against the mark scheme.

21 marks
1Explain how run length encoding compresses data and state one type of data for which it is unsuitable.4 marks
Explain

Mark scheme0 / 4
  • LRuns of identical consecutive values are replaced by the value and a count
  • LGives a valid example of the encoding
  • LIt is lossless, so the original is restored exactly
  • LIt is unsuitable for data with few runs, such as a photograph, where it may increase size
Model answer

Run length encoding works by scanning the data for runs of identical consecutive values and replacing each run with a single copy of the value together with a count of how many times it repeated, so a sequence of five As followed by three Bs is stored as 5A3B. It is lossless, because the count contains everything needed to reconstruct the original sequence exactly. It is unsuitable for data that contains few runs of identical adjacent values, such as a photograph, where neighbouring pixels almost always differ slightly. In that case almost every value would be stored with a count of one, so the encoded file would actually be larger than the original.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
2Explain the difference between symmetric and asymmetric encryption.4 marks
Explain

Mark scheme0 / 4
  • LSymmetric uses the same key to encrypt and decrypt
  • LAsymmetric uses a public key to encrypt and a matching private key to decrypt
  • LSymmetric is faster but the key must be distributed securely
  • LAsymmetric solves key distribution because the public key can be shared openly, but it is slower
Model answer

Symmetric encryption uses a single shared key for both encryption and decryption, so both parties must possess the same secret. Asymmetric encryption uses a mathematically related pair of keys: a public key that may be distributed openly and a private key that is never shared, where data encrypted with the public key can only be decrypted with the private one. The practical difference is a trade off between speed and key distribution. Symmetric encryption is considerably faster and therefore suited to encrypting large volumes of data, but it requires the key itself to be transmitted to the recipient by some secure means, and if that transmission is intercepted the encryption is worthless. Asymmetric encryption removes that problem entirely, since the public key can be published without compromising anything, but it is significantly slower, which is why real systems use asymmetric encryption to exchange a symmetric session key and then use that faster symmetric key for the actual data.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
3Describe how a digital signature is created and used to verify a message.5 marks
Describe

Mark scheme0 / 5
  • LA hash of the message is calculated
  • LThe hash is encrypted with the sender's private key to form the signature
  • LThe recipient decrypts the signature using the sender's public key
  • LThe recipient independently hashes the received message
  • LIf the two hashes match, the message is unaltered and came from the private key holder
Model answer

To create a digital signature, the sender first passes the message through a hash function to produce a fixed size digest. That digest is then encrypted using the sender's private key, and the result is attached to the message as the signature. To verify it, the recipient decrypts the signature using the sender's public key, which recovers the original hash value, and separately calculates the hash of the message they actually received using the same hash function. If the two hashes are identical then two things are proven at once: the message has not been altered in transit, since any change would produce a completely different hash because of the avalanche effect, and it genuinely came from the holder of the private key, since no one else could have produced a signature that decrypts correctly with that public key.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
4Explain why passwords should be stored as salted hashes rather than as encrypted values.5 marks
Explain

Mark scheme0 / 5
  • LHashing is a one way function that cannot be reversed
  • LEncryption is reversible, so an attacker who obtains the key recovers every password
  • LLogin works by hashing the entered password and comparing the hashes
  • LA salt is a random value added before hashing
  • LThe salt ensures identical passwords hash differently and defeats precomputed rainbow tables
Model answer

Hashing is a one way function: it is computationally infeasible to recover the original input from the hash, so an attacker who steals the database obtains only hashes and not the passwords themselves. Encryption, by contrast, is reversible by design, which means the system must hold a decryption key somewhere, and an attacker who obtains both the database and that key recovers every password in plain text at once. Authentication does not require the original password to be recoverable: when a user logs in, the system hashes what they typed and compares the result with the stored hash, and a match proves they supplied the right password. Salting adds a random value, unique to each user, to the password before it is hashed, and that salt is stored alongside the hash. This matters because without it two users who chose the same password would produce identical hashes, which is visible information, and an attacker could use a precomputed rainbow table mapping common passwords to their hashes to crack thousands of accounts instantly. With a unique salt per user, every hash is different even for identical passwords, precomputed tables are useless, and the attacker must mount a separate brute force attack against each account individually.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
5Explain why lossless compression must be used for a program file.3 marks
Explain

Mark scheme0 / 3
  • LEvery byte of a program file is a meaningful instruction or data value
  • LLossy compression permanently discards data
  • LRemoving any part would corrupt the program so it would not run correctly
Model answer

A program file consists entirely of machine instructions and the data they operate on, and every single byte is significant, since there is no redundant detail that a human observer would fail to notice. Lossy compression achieves its size reduction precisely by permanently discarding information judged to be imperceptible, which is a reasonable trade for an image or a piece of audio but is meaningless for executable code. Discarding any portion of a program would corrupt the instruction stream, so the program would either fail to start, crash during execution or, worse, run while behaving incorrectly. Lossless compression must therefore be used, since it reduces the file size by encoding the same information more efficiently and restores it byte for byte on decompression.

The marker looks for key phrases. If you said it another way and it is still right, give yourself the mark.
Machine mark 0 / 21