Lab 04: Cryptography Basics
🎯 Objective
Understand the difference between symmetric and asymmetric encryption, apply them with OpenSSL, and see how hashing works. Learn why cryptography is the backbone of modern cybersecurity and how weak cryptography leads to breaches.
📚 Background
Cryptography transforms readable data (plaintext) into unreadable ciphertext, protecting it from unauthorized access. There are two main types: symmetric encryption uses one shared key for both encryption and decryption (like a house key that locks and unlocks the door). Asymmetric encryption uses a mathematically linked key pair — a public key to encrypt and a private key to decrypt (like a mailbox: anyone can drop mail in, only the owner can retrieve it).
Symmetric encryption (AES, DES, 3DES) is fast and efficient for bulk data. The challenge is key exchange — how do you securely share the key in the first place? This is solved by asymmetric encryption (RSA, ECC), which is slower but allows secure key exchange over public channels.
Hashing is one-way transformation — you can hash a password to get a fingerprint, but you can't reverse the hash to get the original password. This is how passwords are stored securely. Common hash algorithms: MD5 (broken — never use for security), SHA-1 (deprecated), SHA-256/SHA-3 (current standard).
In practice, most systems use hybrid encryption: asymmetric encryption to securely exchange a symmetric key, then symmetric encryption for bulk data. This is exactly how TLS/HTTPS works.
⏱️ Estimated Time
45 minutes
📋 Prerequisites
Labs 1-3 completed
Docker with
innozverse-cybersecimage
🛠️ Tools Used
openssl— Encryption, key generation, hashingpython3— Demonstrating cryptographic conceptsbase64— Encoding binary data for display
🔬 Lab Instructions
Step 1: Symmetric Encryption with AES-256
AES-256 is the gold standard for symmetric encryption. Let's encrypt and decrypt a file:
📸 Verified Output:
💡 What this means: The plaintext was encrypted with AES-256-CBC using our password. The output is base64-encoded ciphertext. Notice it starts with
U2FsdGVkX1— that'sSalted__in base64, indicating OpenSSL added a random salt to the password-derived key (making brute-force harder). Without the password, this ciphertext is computationally infeasible to decrypt.
Step 2: Decrypt the AES-256 Encrypted Data
📸 Verified Output:
💡 What this means: With the correct password, we recovered the original plaintext perfectly. Try decrypting with a wrong password — you'll get garbled output or an error. This demonstrates the fundamental property of symmetric encryption: same key in, same data out. The
-pbkdf2flag uses PBKDF2 key derivation (adds computational cost to brute-force attacks).
Step 3: Generate an RSA Key Pair (Asymmetric)
RSA is the most common asymmetric algorithm. A 2048-bit RSA key is currently secure:
📸 Verified Output:
💡 What this means: RSA key generation creates a mathematically linked key pair. The private key is kept secret — it can decrypt data encrypted with the public key, and it can create digital signatures. The public key can be shared freely — it encrypts data that only the private key can decrypt. The security comes from the difficulty of factoring large numbers (the modulus).
Step 4: Encrypt and Decrypt with RSA
📸 Verified Output:
💡 What this means: Anyone with the public key can encrypt, but ONLY the holder of the private key can decrypt. This solves the key exchange problem: you publish your public key openly, and anyone can encrypt sensitive data that only you can read. This is how email encryption (PGP/GPG) and TLS work.
Step 5: Digital Signatures — Proving Authenticity
Digital signatures use the private key to sign and the public key to verify:
📸 Verified Output:
💡 What this means: Digital signatures provide authentication (proves who signed it), integrity (any change to the document breaks the signature), and non-repudiation (the signer can't deny signing). The tampered contract fails verification — this is exactly how software code signing works (binaries are signed by the vendor; any modification breaks the signature, alerting users to malware).
Step 6: Hashing — One-Way Fingerprints
Hashing is irreversible (one-way) — you can't recover the original from the hash:
📸 Verified Output:
💡 What this means: The "avalanche effect" means even a 1-bit change produces a completely different hash. MD5 produces 128 bits (32 hex chars), SHA-256 produces 256 bits (64 hex chars). MD5 is broken — researchers have found collisions (two different inputs producing the same hash). Never use MD5 for security purposes. SHA-256 and SHA-3 are current standards.
Step 7: Password Hashing — The Right Way
How should passwords be stored? Not in plaintext, and not with raw MD5/SHA:
📸 Verified Output:
💡 What this means: Without salting, all users with the same password get the same hash — attackers can precompute "rainbow tables" of common passwords and look up hashes instantly. With a random salt, even identical passwords produce different hashes. Password-hashing functions like bcrypt/argon2 are intentionally slow (100ms to compute), making brute-force billions of times more expensive.
Step 8: Generate an Elliptic Curve Key Pair (Modern Alternative to RSA)
ECC (Elliptic Curve Cryptography) provides equivalent security to RSA with much smaller keys:
📸 Verified Output:
💡 What this means: A 256-bit EC key provides security equivalent to a 3072-bit RSA key but uses much less memory and CPU. This is why TLS 1.3 and modern systems prefer ECDHE (Elliptic Curve Diffie-Hellman Ephemeral) for key exchange. Smaller keys mean faster connections and less power consumption — critical for IoT devices.
Step 9: Demonstrate the Key Exchange Problem
The fundamental challenge that asymmetric cryptography solves:
📸 Verified Output:
💡 What this means: Every HTTPS connection uses hybrid encryption: the TLS handshake uses asymmetric cryptography to securely exchange a symmetric session key, then all data flows encrypted with AES (symmetric). This is why HTTPS is both secure AND fast — the slow asymmetric crypto only happens once at connection setup.
Step 10: Cryptographic Strength Comparison
Understanding which algorithms are secure vs broken:
📸 Verified Output:
💡 What this means: Using deprecated or broken algorithms is one of the most common security failures (it's OWASP A02: Cryptographic Failures). MD5 password hashes have been cracked in seconds using precomputed tables. SHA-1 certificates were deprecated by browsers in 2017. Always use modern algorithms — AES-256, SHA-256/3, RSA-2048+, or ECC P-256+.
✅ Verification
📸 Verified Output:
🚨 Common Mistakes
Using MD5 or SHA-1 for passwords: These are cryptographically broken. Always use bcrypt, argon2, or scrypt for password hashing
Hard-coding encryption keys: Keys in source code get committed to git repositories and exposed. Use environment variables or key management systems
Confusing hashing with encryption: Hashing is one-way (can't reverse). Encryption is two-way (can decrypt with key). Don't store passwords encrypted — store them hashed
📝 Summary
Symmetric encryption (AES) is fast and uses one key for both operations; asymmetric (RSA/ECC) uses key pairs and solves the key exchange problem
Digital signatures provide authentication, integrity, and non-repudiation — essential for software distribution and code signing
Hashing is one-way: MD5 and SHA-1 are broken; use SHA-256 or SHA-3; for passwords use bcrypt/argon2
Real-world systems use hybrid encryption: asymmetric for key exchange, symmetric for bulk data encryption
🔗 Further Reading
Last updated
