#Why this matters
Every time you sign in to a university portal, send a message, or verify a software update, cryptography enforces the CIA triad across untrusted networks. Modern systems protect banking transfers, TLS 1.3 web sessions, and code-signing certificates using peer-reviewed math rather than secret tricks.
Understanding these building blocks prevents costly mistakes. Developers frequently confuse Base64 encoding with encryption, deploy broken algorithms, or invent custom ciphers that collapse under basic analysis. A clear map of cryptographic goals and primitives prepares you for both defensive architecture and CTF challenges.
#Kerckhoffs's Principle and the four security goals
In 1883, Auguste Kerckhoffs stated a rule that still anchors security engineering: a cryptosystem must stay secure even if the attacker knows the entire algorithm, as long as the key remains secret. Hiding the algorithm is "security through obscurity." Once a binary leaks or is reverse-engineered, a secret algorithm is broken for every user. Public standards like AES and SHA-256 survive decades of open review because only the key stays private.
Modern cryptography delivers four distinct security goals:
| Goal | What it guarantees | Modern mechanisms |
|---|---|---|
| Confidentiality | Only authorized key holders can read the data | AES-256-GCM, ChaCha20-Poly1305 |
| Integrity | Accidental or malicious modification is detected | SHA-256, SHA-3, HMAC-SHA256 |
| Authentication | Proves the identity of the sender or server | X.509 certificates, ECDSA, Ed25519 |
| Non-repudiation | The sender cannot later deny sending the message | Digital signatures (RSA-PSS, ECDSA) |
Encryption alone hides plaintext but does not prove who sent it or whether ciphertext bytes were altered in transit. Authenticated encryption (AES-GCM) or pairing encryption with HMAC or a digital signature closes those gaps.
#Encoding vs. hashing vs. encryption
Beginners often group encoding, hashing, and encryption together because all three turn readable text into unfamiliar strings, yet they solve completely different problems.
| Property | Encoding | Hashing | Encryption |
|---|---|---|---|
| Purpose | Safe transport format | Integrity and password storage | Confidentiality |
| Secret key | No | No (keyed form: HMAC) | Yes |
| Reversible | Always, by anyone | No (one-way) | Only with the key |
| Examples | Base64, Hex, UTF-8 | SHA-256, SHA-3, Argon2id | AES-256-GCM, RSA-OAEP, ECC |
Encoding converts bytes into a transport-safe alphabet: Base64 turns Hello into SGVsbG8=, and Hex turns it into 48656c6c6f. Anyone can reverse it with no key.
Hashing maps any input to a fixed-size digest. Changing one bit scrambles the output, and you cannot invert a secure hash. Password storage pairs a random salt with Argon2id or bcrypt. Avoid MD5 and SHA-1 for security due to practical collision attacks.
Encryption transforms plaintext into ciphertext with a secret key so only authorized holders can decrypt it.
#Symmetric, asymmetric, and hybrid encryption
- One shared secret key for encrypt and decrypt
- Very fast for bulk data
- Hard to distribute the shared key safely
- Confidentiality (plus integrity in AEAD)
- Public key encrypts or verifies; private key decrypts or signs
- Computationally slow for large payloads
- Solves key exchange over untrusted networks
- Also enables digital signatures and certificates
Symmetric ciphers like AES-256 and ChaCha20 run at hardware speeds, while legacy DES (56-bit) and RC4 are broken and retired. Asymmetric algorithms like RSA and ECC - such as Bitcoin's 256-bit secp256k1 curve (~2^256 keys) or NIST P-256 - avoid pre-shared secrets but are much slower.
Real protocols like TLS 1.3 (HTTPS) combine both via hybrid encryption:
- AuthenticateServer proves identity with X.509 certificate and signature
- Key ExchangeClient and server agree on a fresh session key via ECDHE or ML-KEM
- Bulk EncryptionBoth sides encrypt traffic quickly with AES-GCM or ChaCha20-Poly1305
- Integrity CheckEach record carries an authentication tag to detect tampering
Because quantum computers threaten RSA and classical ECC, NIST standardized post-quantum algorithms in 2024: ML-KEM (FIPS 203) for key encapsulation, plus ML-DSA (FIPS 204) and SLH-DSA (FIPS 205) for signatures.
#Practical primitives in Python 3
Python's standard library handles Base64, Hex, SHA-256, HMAC, and bitwise XOR cleanly:
import base64
import hashlib
import hmac
# 1. Encoding: Base64 and Hex (keyless, always reversible)
print(base64.b64decode("SGVsbG8gV29ybGQ=")) # b'Hello World'
word = bytes.fromhex("48656c6c6f")
print(word, word.hex()) # b'Hello' 48656c6c6f
# 2. Hashing and keyed integrity (one-way)
print(hashlib.sha256(b"secret").hexdigest()) # 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
print(hmac.new(b"shared-key", b"transfer=100", hashlib.sha256).hexdigest()[:16])
# 3. Bitwise XOR (self-inverse: x ^ k ^ k == x)
pt = bytes([0x61, 0x62, 0x63]) # b'abc'
print(bytes(b ^ 0x20 for b in pt)) # b'ABC'#Defending / Common mistakes
- Treating encoding as encryption: Base64 and Hex offer zero secrecy. Never hide tokens or credentials with encoding.
- Inventing custom ciphers: Always use vetted libraries (
cryptography, OpenSSL, libsodium) rather than home-grown schemes. - Using broken algorithms: Avoid MD5, SHA-1, DES, and RC4; standardize on AES-256-GCM or ChaCha20-Poly1305 and SHA-256/SHA-3.
- Ignoring integrity: Plain encryption without an authentication tag can be tampered with silently. Prefer AEAD modes or verify an HMAC before decrypting.
#Summary
- Kerckhoffs's Principle puts security in the secret key, never in hiding the algorithm.
- Encoding converts format without a key, hashing creates a one-way integrity digest, and encryption protects confidentiality with a key.
- Hybrid encryption pairs asymmetric key exchange with fast symmetric ciphers for protocols like TLS 1.3.
- Combining encryption with HMAC or digital signatures covers confidentiality, integrity, authentication, and non-repudiation.