Skip to content

About

Classical ciphers to RSA: 7 crypto labs, incl. an ECB mode structure-leak demo.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Applied Cryptography Labs

Seven labs from a university cryptography module, covering classical ciphers through modern public-key crypto. Code and write-ups are my own; course-provided instructional material (assignment sheets, lecture slides) isn't included.

Lab 4 — Block Ciphers: ECB Mode Leaks Structure

The clearest result of the set. I implemented the PRESENT lightweight block cipher and wrapped it in ECB mode, then recovered this image purely from ciphertext block repetition — no key required:

Recovered pattern

Identical plaintext blocks always encrypt to identical ciphertext blocks under ECB, so the encrypted image's structure leaks straight through. extract.py finds the most-common repeated block (the background) and reconstructs the image layout from block-level matches against it — the classic demonstration of why ECB shouldn't be used for anything with structure.

python3 extract.py -i letter.e -o op.txt -hh header.pbm

Lab 1 — Shift Cipher

Byte-wise shift cipher for text (printable-charset) and binary files. Part III brute-forces an unknown shift key by trying all 256 offsets and checking each result against the PNG magic-byte header, recovering a hidden flag image without knowing the key in advance.

python3 ex1.py -i sherlock_short.txt -o out.en -k 42 -m e   # encrypt (printable charset)
python3 ex1.py -i out.en -o out.de.txt -k 42 -m d            # decrypt back
python3 ex2.py -i flag -o flag_decrypted.png                 # brute-force Part III

Lab 2 — Substitution Cipher

Broke a monoalphabetic substitution cipher via frequency analysis — used single-letter word matches ("I", "a") to pin down the key rather than guessing letter-by-letter. Also exploited OTP key reuse: given ciphertext under a known plaintext, XOR-ed in the difference between the original and a chosen new plaintext to flip specific bits and produce an attacker-chosen decryption, without ever recovering the OTP key itself.

python3 ex1.py   # frequency analysis, decrypts story_cipher.txt -> solution.txt

Lab 3 — Hashing (MD5)

Covers MD5's length-invariance and length-extension weakness, then three cracking exercises: brute-forcing unsalted hashes with timing, repeating against salted hashes (~40x slower per hash), and a class-wide cracking competition (145/148 hashes) using John the Ripper with combined wordlists and rule-based mutations.

Lab 5 — Modular Arithmetic (GF(2^n))

From-scratch polynomial arithmetic over GF(2^n) — addition, multiplication, and reduction by an irreducible polynomial. This is the field structure block ciphers like AES and PRESENT build their S-boxes on top of.

Lab 6 — Diffie-Hellman Key Exchange

Implemented DH key exchange from primitives: modular exponentiation via square-and-multiply, and Miller-Rabin for primality testing. The derived shared secret is used directly as the key for PRESENT-ECB message encryption/decryption between two parties.

DHKE guarantees the exchange itself is safe over an insecure channel (an attacker who sees both public keys still can't feasibly derive the shared secret) — but it doesn't authenticate either party, so a standard implementation is open to a man-in-the-middle attack unless identities are verified some other way.

Lab 7 — RSA

RSA key generation, encryption/decryption, and digital signatures via the EVP-style sign-the-hash pattern. Also covers a signature-forgery exercise: it's possible to forge a valid RSA signature over some message without the private key, but not over a message of the attacker's choosing — the forged plaintext comes out as unreadable binary, which is the core limitation explored in ans.txt.


Note on scope: private keys generated for Lab 7 aren't included (only the public keys) — throwaway lab keys, but not worth committing as a matter of habit. ex2.py specifically needs a local mykey.pem.priv to run — generate your own RSA keypair (e.g. Crypto.PublicKey.RSA.generate() or openssl genrsa) and point mykey.pem.pub / mykey.pem.priv at it.

About

Classical ciphers to RSA: 7 crypto labs, incl. an ECB mode structure-leak demo.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages