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.
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.
• 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
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
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.
• 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
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
• 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.
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!
• 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.
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
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.
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.
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.
• 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.
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.
• 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
• 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
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.
• 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
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
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
• 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
• 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)