Skip to content

1.3.1 Compression, encryption and hashing

Paper 1 · Computer Systems · Section 1.3 · 20 flashcards · 12 questions

Lossy and lossless compression, symmetric/asymmetric encryption, hashing for passwords and data integrity

1.3.1 Compression, Encryption and Hashing

1.3.1(a) Lossy vs Lossless Compression

Compression is essential in modern computing - without it, storing and transmitting large files like videos, images, and documents would be impractical. There are two fundamentally different approaches to compression, each with specific use cases.

Definition
Compression is the process of reducing the size of a file, enabling more efficient storage and faster transmission over networks.

Lossy Compression

Lossy compression achieves dramatic file size reductions by permanently removing data - specifically, the parts that are least noticeable to human perception. Once compressed, the original data cannot be fully recovered.

Definition
Lossy compression permanently removes some data (typically the least noticeable parts) to achieve significant file size reduction. The original file cannot be perfectly reconstructed.
Key Point
How Lossy Compression Works:
• Analyses data to identify less important information
• Removes data that humans are less likely to notice
• JPEG removes colour variations the eye can't distinguish
• MP3 removes frequencies outside normal hearing range
• The more compression, the more quality loss
Example
JPEG Image Compression:
Original photo: 10 MB
High quality JPEG (90%): 2 MB - minimal visible difference
Medium quality JPEG (50%): 500 KB - some blur/artifacts visible
Low quality JPEG (10%): 100 KB - significant quality loss, blocky artifacts

MP3 Audio Compression:
Original WAV: 50 MB
320 kbps MP3: 10 MB - near-CD quality
128 kbps MP3: 4 MB - acceptable quality for most listeners
64 kbps MP3: 2 MB - noticeable quality loss
Exam Tip
When to use Lossy: Media files where perfect reproduction isn't required - photos for web, music for portable devices, streaming video. NEVER for text, code, or medical images.

Lossless Compression

Lossless compression reduces file size without losing any data - the original can be perfectly reconstructed. This is achieved by finding and encoding patterns in the data more efficiently.

Definition
Lossless compression reduces file size without losing any data. The original file can be perfectly reconstructed from the compressed version.
Key Point
How Lossless Compression Works:
• Identifies patterns and redundancy in data
• Encodes repeated patterns with shorter representations
• No information is discarded
• Decompression perfectly restores original
• Compression ratio depends on data patterns
Example
ZIP File Compression:
A folder of text documents might compress from 100 MB to 20 MB (80% reduction) because text has many repeating patterns.

A folder of already-compressed JPEGs might only go from 100 MB to 98 MB (2% reduction) because there's little redundancy left to exploit.

Comparison

Key Point
Data Lost?
• Lossy: Yes - permanently
• Lossless: No - perfect reconstruction

Compression Ratio
• Lossy: High (often 90%+)
• Lossless: Lower (typically 50-70%)

Best For
• Lossy: Images, audio, video
• Lossless: Text, code, documents, archives

Examples
• Lossy: JPEG, MP3, MPEG, H.264
• Lossless: ZIP, PNG, FLAC, GIF

Quality
• Lossy: Degrades with more compression
• Lossless: Always perfect

1.3.1(b) Run Length Encoding and Dictionary Coding

These are two specific algorithms for achieving lossless compression, each suited to different types of data.

Run Length Encoding (RLE)

Run Length Encoding is one of the simplest compression algorithms. It works by replacing consecutive identical values with a single value and a count.

Definition
Run Length Encoding (RLE) stores consecutive occurrences of the same data value as a single value paired with a count of how many times it repeats.
Worked Example
Worked Example: Compressing with RLE

Original string: AAAAAABBBCCCCCCCCDD

Step 1: Identify runs of identical characters
• A appears 6 times consecutively
• B appears 3 times consecutively
• C appears 8 times consecutively
• D appears 2 times consecutively

Step 2: Encode as value-count pairs
A6 B3 C8 D2

Result:
• Original: 19 characters
• Compressed: 8 characters (A6B3C8D2)
• Compression ratio: 58% reduction

When RLE fails:
Original: ABCDEFGH (no repetition)
"Compressed": A1B1C1D1E1F1G1H1
Result: 16 characters - LARGER than original!
Key Point
RLE is effective when:
• Data has long runs of identical values
• Simple graphics with large areas of same colour
• Fax transmissions (long runs of white/black)

RLE is ineffective when:
• Data is highly varied with little repetition
• Photographs (pixels rarely repeat exactly)
• Encrypted or random data

Dictionary Encoding

Dictionary encoding identifies frequently occurring sequences and replaces them with shorter codes, maintaining a dictionary that maps codes to their original values.

Definition
Dictionary encoding replaces frequently occurring data sequences with shorter codes. A dictionary mapping codes to original sequences is stored alongside the compressed data.
Worked Example
Worked Example: Dictionary Compression

Original text:
"the cat sat on the mat and the rat ate the fat"

Step 1: Identify frequently occurring patterns
• "the " appears 4 times
• "at " appears 4 times (in cat, sat, mat, rat, fat)

Step 2: Build dictionary
Dictionary:
• Code 1 = "the "
• Code 2 = "at"

Step 3: Replace occurrences
"1c2 s2 on 1m2 and 1r2 2e 1f2"

Result:
• Original: 45 characters
• Compressed text: 28 characters + dictionary overhead
• Net saving depends on how often patterns repeat
Exam Tip
Dictionary vs RLE: Dictionary encoding works better for text with repeating words/phrases. RLE works better for data with repeating individual values. Real compression algorithms (like ZIP) often combine multiple techniques.

1.3.1(c) Symmetric and Asymmetric Encryption

Encryption transforms readable data (plaintext) into an unreadable format (ciphertext) to protect it from unauthorised access. Understanding the two main approaches is crucial for computer science.

Definition
Encryption is the process of encoding data so that only authorised parties can read it. The original data (plaintext) is transformed into encrypted data (ciphertext) using an algorithm and a key.

Symmetric Encryption

In symmetric encryption, the same key is used for both encryption and decryption. This is like having a single key that both locks and unlocks a door.

Definition
Symmetric encryption uses a single shared key for both encrypting and decrypting data. Both the sender and recipient must possess the same secret key.
Worked Example
Worked Example: Symmetric Encryption Process

Scenario: Alice wants to send a secret message to Bob

Step 1: Alice and Bob agree on a secret key (e.g., "SECRET123")
• This must happen through a secure channel

Step 2: Alice encrypts her message
• Plaintext: "Meet at noon"
• Key: "SECRET123"
• Ciphertext: "Xk3$p@9nLm2!" (unreadable)

Step 3: Alice sends ciphertext to Bob (can use insecure channel)

Step 4: Bob decrypts using the same key
• Ciphertext: "Xk3$p@9nLm2!"
• Key: "SECRET123"
• Plaintext: "Meet at noon"

The Problem: How do Alice and Bob securely share the key in the first place? If an attacker intercepts the key, they can decrypt all messages.
Key Point
Symmetric Encryption Characteristics:
• Fast - suitable for encrypting large amounts of data
• Simple - one key to manage
• Problem: Key distribution - how to share the key securely?
• If key is compromised, all data encrypted with it is vulnerable
• Examples: AES (Advanced Encryption Standard), DES

Asymmetric Encryption

Asymmetric encryption uses two mathematically related keys - a public key and a private key. What one key encrypts, only the other can decrypt.

Definition
Asymmetric encryption uses a pair of keys: a public key (shared openly) and a private key (kept secret). Data encrypted with the public key can only be decrypted with the corresponding private key.
Worked Example
Worked Example: Asymmetric Encryption Process

Scenario: Alice wants to send a secret message to Bob

Step 1: Bob generates a key pair
• Public key: Can be shared with anyone
• Private key: Bob keeps this secret, never shares it

Step 2: Bob publishes his public key (website, email, etc.)

Step 3: Alice encrypts her message using Bob's PUBLIC key
• Plaintext: "Meet at noon"
• Bob's public key applied
• Ciphertext: "7h$kL@2mNp!" (unreadable)

Step 4: Alice sends ciphertext to Bob (can use insecure channel)

Step 5: Bob decrypts using his PRIVATE key
• Only Bob's private key can decrypt this
• Even Alice cannot decrypt it now!
• Plaintext: "Meet at noon"

Key insight: No secret key ever needs to be transmitted. The public key can be shared openly - knowing it doesn't help an attacker decrypt messages.
Key Point
Asymmetric Encryption Characteristics:
• Solves key distribution problem - public key can be shared openly
• Private key never transmitted - more secure
• Slower than symmetric - computationally intensive
• Enables digital signatures (encrypt with private key)
• Examples: RSA, Elliptic Curve Cryptography (ECC)

Comparison

Key Point
Keys
• Symmetric: 1 shared key
• Asymmetric: 2 keys (public + private)

Speed
• Symmetric: Fast
• Asymmetric: Slow

Key Distribution
• Symmetric: Difficult (must share secretly)
• Asymmetric: Easy (public key shared openly)

Best For
• Symmetric: Bulk data encryption
• Asymmetric: Key exchange, digital signatures

Security Risk
• Symmetric: Key interception
• Asymmetric: Private key theft
Exam Tip
Hybrid Encryption: Real-world systems like HTTPS use BOTH:
1. Asymmetric encryption to securely exchange a symmetric key
2. Symmetric encryption for the actual data (faster)
This combines the security of asymmetric with the speed of symmetric.

1.3.1(d) Hashing

Hashing is fundamentally different from encryption - it's a one-way process designed to create a unique "fingerprint" of data, not to hide it.

Definition
Hashing is a one-way function that converts input data of any size into a fixed-size string (the hash or digest). It cannot be reversed - you cannot recover the original data from the hash.
Key Point
Properties of a Good Hash Function:
• Deterministic - Same input always produces same hash
• Fast - Quick to compute the hash
• One-way - Cannot reverse to find original input
• Avalanche effect - Small input change = completely different hash
• Collision resistant - Hard to find two inputs with same hash

Use 1: Password Storage

Worked Example
Worked Example: Password Hashing

Why not store passwords directly?
If database is stolen, attacker sees all passwords in plain text!

Correct approach - store hashes:

Registration:
1. User creates password: "MyP@ssw0rd"
2. System hashes it: hash("MyP@ssw0rd") = "a7b9c3d2e1..."
3. Only the hash is stored in database

Login:
1. User enters password: "MyP@ssw0rd"
2. System hashes the attempt: hash("MyP@ssw0rd") = "a7b9c3d2e1..."
3. Compare with stored hash: Match! User authenticated.

If database is stolen:
• Attacker sees: "a7b9c3d2e1..."
• Cannot reverse to get "MyP@ssw0rd"
• Would need to try hashing millions of guesses

Use 2: Data Integrity

Worked Example
Worked Example: Verifying File Integrity

Scenario: You download a large software file

Step 1: Website publishes file AND its hash
• File: "software.exe" (500 MB)
• Published hash: "5d41402abc4b2a76..."

Step 2: You download the file

Step 3: You hash your downloaded file
• Your hash: "5d41402abc4b2a76..."

Step 4: Compare hashes
• Hashes match = file is intact, unchanged
• Hashes differ = file corrupted or tampered with

Use 3: Hash Tables

Definition
A hash table is a data structure that uses hashing to enable fast data retrieval. The hash of a key determines where its value is stored.
Key Point
Hash Table Performance:
• Average case: O(1) lookup, insert, delete
• Hash function maps key to array index
• Collisions occur when different keys hash to same index
• Collision handling: Chaining (linked lists) or Open Addressing (probe for empty slot)

Encryption vs Hashing

Key Point
Direction
• Encryption: Two-way (encrypt/decrypt)
• Hashing: One-way (cannot reverse)

Output Size
• Encryption: Variable (depends on input)
• Hashing: Fixed (always same length)

Purpose
• Encryption: Confidentiality (hide data)
• Hashing: Integrity/verification

Key Required
• Encryption: Yes
• Hashing: No

Can Recover Original
• Encryption: Yes (with key)
• Hashing: No (by design)
Exam Tip
Remember: Encryption is for HIDING data (confidentiality). Hashing is for VERIFYING data (integrity) and secure storage. They serve different purposes!