Cryptography Explained: Encryption, Hashing and Digital Signatures
Cryptography is the mathematics that makes secure communication, online banking, password storage and digital identity possible. You do not need to be a mathematician to use it well, but you do need a clear mental model of what each tool does, what it does not do, and the mistakes people make with it. This lesson builds that model and lets you practise with OpenSSL on your own machine.
What Cryptography Provides
- Confidentiality: encryption keeps data secret.
- Integrity: hashes and message authentication codes detect changes.
- Authentication: signatures and certificates prove who produced something.
- Non-repudiation: signatures make it hard to deny authorship.
Key Vocabulary
| Term | Meaning |
|---|---|
| Plaintext | Readable data before encryption |
| Ciphertext | Unreadable output of encryption |
| Key | The secret value that controls encryption and decryption |
| Algorithm (cipher) | The mathematical procedure; assumed public |
| IV / nonce | A value that makes each encryption unique; must never be reused improperly |
| Salt | Random data added before hashing a password |
Kerckhoffs’s principle: a system should remain secure even if everything about it except the key is public. Secret, home-made algorithms are a warning sign; use well-reviewed, standard ones.
Symmetric Encryption
The same key encrypts and decrypts. It is fast and ideal for large amounts of data. The challenge is sharing the key securely.
| Algorithm | Status | Notes |
|---|---|---|
| AES (128, 192, 256-bit) | Standard, recommended | Use with an authenticated mode such as GCM |
| ChaCha20-Poly1305 | Modern, recommended | Fast on devices without AES hardware support |
| 3DES | Deprecated | Small block size; avoid |
| DES, RC4 | Broken | Never use |
Modes of operation determine how a block cipher handles more than one block. ECB mode encrypts identical blocks to identical ciphertext and leaks patterns, so never use it. CBC needs separate integrity protection. GCM and similar authenticated encryption modes provide confidentiality and integrity together, and are preferred.
Asymmetric (Public-Key) Encryption
Each party has a key pair: a public key that can be shared and a private key that must be kept secret. What one key encrypts, only the other can decrypt (and the private key can sign data that the public key verifies). It is slower than symmetric encryption, so it is mostly used to exchange symmetric keys and to create signatures.
- RSA: widely used; use at least 2048-bit keys (3072 bits or more for long-term protection).
- Elliptic-curve cryptography (ECC): smaller keys with similar strength; ECDSA, ECDH and Ed25519 are common.
- Diffie-Hellman key exchange: lets two parties agree on a shared secret over an open channel.
Quantum computers, if built at sufficient scale, could break RSA and ECC. Standards bodies, including NIST, have published post-quantum algorithms, and organisations are beginning to plan migrations. Treat this as something to monitor, not a reason to avoid today’s standards.
Hashing
A hash function turns any input into a fixed-size fingerprint. Good hashes are one-way (you cannot recover the input), deterministic and collision-resistant, and a tiny change in input produces a completely different output.
| Algorithm | Output | Status |
|---|---|---|
| MD5 | 128 bits | Broken for security use; fine only for non-security checksums |
| SHA-1 | 160 bits | Collisions demonstrated; avoid |
| SHA-256, SHA-512 (SHA-2) | 256 or 512 bits | Recommended |
| SHA-3 | Variable | Recommended alternative |
Hashing is not encryption; there is no key and no decryption. Use cases: file integrity, digital signatures, and (with special slow algorithms and salts) password storage; see Password Security. An HMAC combines a hash with a secret key to prove both integrity and that the sender knows the key.
Digital Signatures
To sign, the sender hashes the message and encrypts the hash with their private key. Anyone can verify with the public key by decrypting the signature and comparing it with a fresh hash. A valid signature proves the message was not altered and that it came from the private-key holder. Signatures underpin software updates, code signing, email signing and certificates (see PKI, Certificates and TLS).
Practice Lab 1: Hashing and the Avalanche Effect
echo -n "hello" | sha256sum
echo -n "hellp" | sha256sum # one letter different
echo -n "hello" | md5sum # compare output size and format
Notice that changing a single character changes the entire output. Now verify a file’s integrity the way you would verify a download:
echo "lab test data" > test.txt
sha256sum test.txt > test.txt.sha256
sha256sum -c test.txt.sha256 # prints: test.txt: OK
echo "tampered" >> test.txt
sha256sum -c test.txt.sha256 # prints: test.txt: FAILED
Practice Lab 2: Symmetric Encryption With OpenSSL
echo "my lab secret" > secret.txt
# Encrypt (you will be asked for a passphrase)
openssl enc -aes-256-cbc -pbkdf2 -salt -in secret.txt -out secret.enc
cat secret.enc # unreadable
# Decrypt
openssl enc -d -aes-256-cbc -pbkdf2 -in secret.enc -out recovered.txt
cat recovered.txt
This command-line tool is excellent for learning. For real applications, use a vetted library with an authenticated mode such as AES-GCM instead of rolling your own.
Practice Lab 3: Public-Key Pairs and Signatures
# Generate a 2048-bit RSA private key and extract the public key
openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:2048
openssl pkey -in private.pem -pubout -out public.pem
# Sign a file with the private key
echo "release v1.0" > release.txt
openssl dgst -sha256 -sign private.pem -out release.sig release.txt
# Verify with the public key
openssl dgst -sha256 -verify public.pem -signature release.sig release.txt
# Expected: Verified OK
# Tamper and verify again
echo "x" >> release.txt
openssl dgst -sha256 -verify public.pem -signature release.sig release.txt
# Expected: Verification failure
Also try modern SSH keys: ssh-keygen -t ed25519 -C "lab key". Never share the private key file, and protect it with a passphrase.
Practice Lab 4: Encoding Is Not Encryption
echo -n "password123" | base64 # cGFzc3dvcmQxMjM=
echo "cGFzc3dvcmQxMjM=" | base64 -d # password123
Base64 is an encoding, reversible by anyone with no key. Seeing it in credentials or tokens is a finding, not protection. The same applies to hex, URL encoding and ROT13.
Common Cryptographic Mistakes
- Inventing your own algorithm or protocol.
- Using MD5 or SHA-1 for security purposes, or unsalted fast hashes for passwords.
- Using ECB mode, or reusing an IV or nonce with the same key.
- Hard-coding keys in source code or storing them beside the encrypted data.
- Confusing encoding, hashing and encryption.
- Poor randomness: use the operating system’s cryptographically secure random generator, not a general-purpose random function.
- Neglecting key management: generation, storage, rotation, backup and destruction.
Practice Exercises
- Classify each as encoding, hashing, or encryption: Base64, SHA-256, AES-GCM, URL encoding.
- Why is it dangerous to store passwords with SHA-256 alone? (Think about speed and precomputed tables.)
- Explain how a digital signature differs from encrypting a message to a recipient.
- Generate two RSA keys and sign the same file with each. Why do the signatures differ?
- Why does TLS use asymmetric cryptography at the start and symmetric cryptography afterwards?
Mentor tip: The first rule of applied cryptography is “don’t implement it yourself.” Your skill is in choosing the right primitive, using a trusted library correctly and protecting the keys.
Educational use only: practise security techniques only on systems you own or have explicit written permission to test.
- Symmetric encryption is fast with one shared key; asymmetric uses key pairs for exchange and signatures.
- Hashes are one-way fingerprints; they are not encryption.
- Signatures prove integrity and origin; use SHA-256 or better, AES-GCM or ChaCha20-Poly1305, and RSA 2048+ or modern elliptic curves.
- Key management and correct usage matter more than algorithm choice.
- Never confuse encoding with encryption.