Lab 4: Secret-Key Encryption
Adapted from the SEED Labs Secret-Key Encryption Lab (Ubuntu 20.04 edition) by Wenliang Du. See Attribution and License.
| Environment | SEED Ubuntu 20.04 VM (setup) or any Ubuntu VM with openssl and python3. This lab is architecture-independent — no 32-bit toolchain — so it runs fine on an Apple Silicon ARM VM. Docker is only needed for the optional Task 6.3. |
| Files | lab4.tar.gz — the SEED Labsetup, repackaged for this course. The Files/ folder holds ciphertext.txt, freq.py, pic_original.bmp, sample_code.py, and words.txt. |
| Submission | One PDF, structured as described in Submitting a Lab Report |
Overview
The first three labs attacked programs. This one steps back to the data: how we
keep it secret, and the mistakes that quietly undo that secrecy. You will use
openssl and a little Python to get hands-on with secret-key (symmetric)
encryption — the ciphers, the block-cipher modes, padding, and the
initialization vector (IV) — and then exploit a few classic developer
mistakes.
The theme is that a strong cipher (AES) is not enough. How you use it — which mode, whether you reuse an IV, whether your IV is predictable — decides whether the result is actually secure. Every attack in this lab works against unbroken AES; the weakness is always in the surrounding choices.
Topics covered:
- Substitution ciphers and frequency analysis
- Block-cipher modes: ECB, CBC, CFB, OFB
- PKCS#5 padding and error propagation
- The IV: why it must be unique and unpredictable
- Known-plaintext and chosen-plaintext attacks
Background reading
- Chapter 21 of Computer & Internet Security: A Hands-on Approach, 2nd/3rd edition, by Wenliang Du — https://www.handsonsecurity.net
man openssl,man enc(theopenssl encoptions),man xxd,man hexdump
How deep is the crypto here? CS487 is a systems security course. This lab is deliberately about using cryptography correctly, not about the internal math of AES. You will not be asked to break AES — you will be asked to spot the misuses that make correctly-implemented AES leak anyway.
Before you begin
Download and unpack the lab files inside the VM:
mkdir -p ~/cs487/lab4 && cd ~/cs487/lab4
wget https://xiaoguang.wang/computer-security-labs/labs/files/lab4.tar.gz
tar xzf lab4.tar.gz
cd Labsetup/Files && ls
You should see ciphertext.txt, freq.py, pic_original.bmp,
sample_code.py, and words.txt. Everything except Task 6.3 runs with
openssl and python3 — no containers, no special privileges.
A reminder on the openssl enc form you will use throughout. It encrypts (-e)
or decrypts (-d) a file with a key and IV given in hex:
$ openssl enc -aes-128-cbc -e -in plain.txt -out cipher.bin \
-K 00112233445566778889aabbccddeeff \
-iv 0102030405060708
Replace -aes-128-cbc with the cipher/mode you want (-aes-128-ecb,
-aes-128-cfb, -aes-128-ofb, -bf-cbc, …). -K and -iv take hex;
-d decrypts; add -nopad to disable padding (Task 4).
Task 1: Frequency Analysis
ciphertext.txt was produced from an English article with a monoalphabetic
substitution cipher — each letter is always replaced by the same other letter.
Spaces and punctuation were left in place to keep word boundaries visible. Your
job is to recover the original plaintext (and the substitution key) using
frequency analysis — no brute force.
The provided freq.py prints the most common single letters, bigrams, and
trigrams in the ciphertext:
$ ./freq.py
Compare those against the known frequencies of English (the most common letters
are e t a o i n, the top bigram is th, the top trigram is the — see the
Wikipedia frequency-analysis page). Guess a few mappings, substitute them with
tr, and read the partially-decrypted text for more clues. Use capital
letters for recovered plaintext so you can tell plaintext from ciphertext as
you go:
# Example: map ciphertext n,y,v to plaintext E,T,A
$ tr 'nyv' 'ETA' < ciphertext.txt > step1.txt
Iterate until the whole article is readable.
Deliverable. The recovered plaintext (at least the first paragraph is enough to show you solved it) and the substitution key you found. Describe your process: which letters you pinned down first and what evidence (which n-gram, matched to which English n-gram) let you pin each one.
Task 2: Encryption Using Different Ciphers and Modes
Get comfortable with openssl enc. Encrypt a file of your choice with at
least three different cipher/mode combinations, then decrypt each one back and
confirm you recover the original. Browse man enc for the list of supported
types.
$ openssl enc -aes-128-cbc -e -in plain.txt -out c1.bin -K ... -iv ...
$ openssl enc -aes-128-cbc -d -in c1.bin -out out.txt -K ... -iv ...
$ diff plain.txt out.txt # identical = round-trip worked
Deliverable. The three cipher/mode commands you ran and evidence that each
decrypts correctly (e.g. diff showing no difference). One or two sentences:
which of your choices are block ciphers and which behave like a stream cipher?
Task 3: Encryption Mode — ECB vs. CBC
This is the famous one. Encrypt the same picture two ways and look at the results.
pic_original.bmp is a simple image. A .bmp file starts with a 54-byte
header that a viewer needs in order to display it; the pixel data follows.
Encrypt the file with ECB and then with CBC, and for each, splice the
original header back onto the encrypted body so it is still a viewable
.bmp:
$ openssl enc -aes-128-ecb -e -in pic_original.bmp -out ecb.bmp -K 00112233445566778889aabbccddeeff
$ head -c 54 pic_original.bmp > header
$ tail -c +55 ecb.bmp > body
$ cat header body > ecb_view.bmp
$ openssl enc -aes-128-cbc -e -in pic_original.bmp -out cbc.bmp -K 00112233445566778889aabbccddeeff -iv 0102030405060708
$ tail -c +55 cbc.bmp > body
$ cat header body > cbc_view.bmp
Open ecb_view.bmp and cbc_view.bmp with an image viewer (eog on the SEED
VM). Repeat with a picture of your own choosing.
Deliverable. The encrypted ECB and CBC images (screenshots). What can you still recognize in the ECB image, and why does CBC hide it? Explain in terms of how ECB encrypts each block independently while CBC chains blocks together.
Task 4: Padding
Block ciphers encrypt in fixed-size blocks, so when the plaintext length is not a multiple of the block size, the cipher pads it (PKCS#5). Stream-like modes do not need padding. You will see both facts directly.
- Encrypt a file with ECB, CBC, CFB, and OFB. Report which modes padded the output (the ciphertext grew to a block-size multiple) and which did not — and explain why the ones that did not, do not need to.
- Create three files of 5, 10, and 16 bytes and encrypt each with
-aes-128-cbc. Note the resulting sizes. Then decrypt with-nopadso the padding is not stripped, and view the bytes with a hex tool to see exactly what was appended:
$ echo -n "12345" > f1.txt # 5 bytes (-n = no trailing newline)
$ openssl enc -aes-128-cbc -e -in f1.txt -out f1.bin -K ... -iv ...
$ openssl enc -aes-128-cbc -d -nopad -in f1.bin -out f1.dec -K ... -iv ...
$ xxd f1.dec
Deliverable. For step 1, the table of which modes padded. For step 2, the
encrypted sizes of the 5/10/16-byte files and a hex dump of each -nopad
decryption. What padding bytes did PKCS#5 add in each case — and what did it add
to the 16-byte file? Explain that last result.
Task 5: Error Propagation — Corrupted Ciphertext
How far does a single-bit error in the ciphertext spread when you decrypt? Different modes answer very differently, and that difference is a direct consequence of how each mode chains blocks.
- Create a text file at least 1000 bytes long.
- Encrypt it with AES-128 (do this once per mode: ECB, CBC, CFB, OFB).
- Flip one bit of the 55th byte of the ciphertext (use the
blesshex editor on the VM, or any hex editor). - Decrypt the corrupted ciphertext with the correct key and IV, and see how much plaintext comes back intact.
Predict the answer for each mode first, then run the experiment and check yourself.
Deliverable. For each of ECB, CBC, CFB, OFB: your prediction, then how much plaintext was actually corrupted (how many bytes/blocks). Justify the result from the mode’s structure — why does the damage stay local in some modes and spread in others?
Task 6: Initialization Vector (IV) and Common Mistakes
Most modes take an IV. Using it carelessly breaks security even though the cipher itself is sound. Tasks 6.1 and 6.2 are required; Task 6.3 is optional.
Task 6.1: The IV Must Be Unique
Encrypt the same plaintext twice: once with two different IVs, and once with the same IV (keep the key fixed). Compare the ciphertexts.
Deliverable. The ciphertexts from both runs. What do you observe when the IV is reused versus changed, and why does this show an IV must never repeat under the same key?
Task 6.2: Reusing an IV — a Known-Plaintext Attack
Even if the plaintext never repeats, reusing an IV is dangerous. In OFB mode,
suppose an attacker captures one plaintext/ciphertext pair (P1, C1) and a second
ciphertext C2 encrypted with the same key and IV. They can recover P2
without the key. You are given:
P1: This is a known message!
C1: a469b1c502c1cab966965e50425438e1bb1b5f9037a4c159
C2: bf73bcd3509299d566c35b5d450337e1bb175f903fafc159
Recover P2. The key idea: in OFB the cipher turns the key+IV into a keystream
that is simply XORed with the plaintext, and the same key+IV gives the same
keystream. So P1 ⊕ C1 recovers that keystream, and XORing it into C2 gives
P2. sample_code.py shows how to XOR ascii and hex strings in Python.
Deliverable. Your XOR code (or steps) and the recovered P2. Briefly: if OFB
were replaced by CFB here, how much of P2 could you recover? (Answer only —
no demo needed.)
Task 6.3 (Optional, 1 bonus point): A Predictable IV — Chosen-Plaintext Attack
A unique IV is not enough; for CBC it must also be unpredictable. This task
uses the Docker container (the only task that does — dcbuild then dcup inside
Labsetup, see the SEED manual). An oracle plays “Bob,” who encrypts a secret
message that is either Yes or No with AES-128-CBC, and helpfully tells you the
next IV he will use:
$ nc 10.9.0.80 3000
Because you know the next IV and can submit a chosen plaintext, you can craft an
input that tests whether Bob’s secret was Yes or No. (Hint: CBC XORs the IV
into the first plaintext block before encrypting. If you know both IVs, you can
choose your plaintext so that your first block, after the IV is XORed in, equals
what Bob’s first block was after his IV — a matching ciphertext block confirms
the guess.)
Deliverable. The chosen plaintext you submitted, the oracle transcript, and
your conclusion (Yes or No) with the reasoning that connects the two IVs to
your crafted input.
Task 7 (Optional, 1 bonus point): Programming Using the Crypto Library
So far you have driven openssl from the command line. This task asks you to call
the crypto library from your own program. You are given a plaintext,
ciphertext, and IV, and must find the key by brute force over a word list:
Plaintext: This is a top secret.
Ciphertext: 764aa26b55a4da654df6b19e4bce00f4ed05e09346fb0e762583cb7da2ac93a2
IV: aabbccddeeff00998877665544332211
Cipher: aes-128-cbc
The key is an English word shorter than 16 characters, padded to 16 bytes with
# (0x23). Write a program that, for each word in words.txt, builds the
candidate key, encrypts the plaintext, and checks whether the result matches the
given ciphertext. Store the plaintext without a trailing newline:
$ echo -n "This is a top secret." > plaintext.txt
You may use C with OpenSSL’s EVP API (gcc ... -lcrypto) or Python’s crypto
library — but you must call the library from code. Just running the openssl
command earns no credit for this task.
Deliverable. Your program (key code with explanation) and the key you recovered. Explain how you padded each word to 16 bytes and how you detected the match.
Cleaning up
This lab makes no global changes to your VM — no relinked /bin/sh, no disabled
ASLR, no Set-UID binaries. If you ran the optional Task 6.3 container, stop it:
$ dcdown # alias for docker-compose down, run inside Labsetup
Delete any .bin/.bmp scratch files you generated.
Submission
Submit a single PDF containing, for each task: the commands you ran, a screenshot or transcript of what you observed, and an explanation of why it happened. For Task 1, state your recovered key; for Tasks 6.2 and 7, include your code with explanation. Listing code with no explanation will not receive credit.
Scoring. Tasks 1–5 and Tasks 6.1–6.2 are required. Task 6.3 and Task 7 are each worth 1 bonus point.
See Submitting a Lab Report for the expected structure.