Introduction to Cryptography
Goals of cryptography: confidentiality, integrity, authenticity
Skills taught: crypto.basics
The Four Goals of Cryptography
Cryptography provides four core security properties. Confidentiality ensures data is readable only by authorised parties. Integrity ensures data has not been modified. Authentication verifies identity. Non-repudiation means a sender cannot later deny having sent a message.
- Confidentiality — data is kept secret from unauthorised readers
- Integrity — any modification of data is detectable
- Authentication — parties can prove their identity
- Non-repudiation — a signed action cannot be denied
Core Vocabulary
- Plaintext
- The original, readable data before encryption. Also called cleartext.
- Ciphertext
- The encrypted, unreadable form produced by an encryption algorithm. Looks like random bytes to an attacker.
- Key
- A secret value used by a cryptographic algorithm. All security rests on the key — the algorithm itself is public knowledge (Kerckhoffs's principle).
- Algorithm (Cipher)
- The mathematical procedure for encryption/decryption. Good algorithms are public, peer-reviewed, and standardised (e.g. AES, ChaCha20, RSA).
Your First Cryptographic Operation
The simplest operation is hashing: a one-way function that turns any input into a fixed-size fingerprint. Even a one-character change produces a completely different hash.
import hashlib
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
# --- Hashing: one-way fingerprint ---
msg = b"Hello, cryptography!"
hash_val = hashlib.sha256(msg).hexdigest()
print("SHA-256:", hash_val)
# 185f8db32921bd46d35... (always the same for the same input)
# --- Symmetric encryption: reversible with a key ---
key = AESGCM.generate_key(bit_length=256) # random 256-bit key
nonce = os.urandom(12) # random 96-bit nonce
aesgcm = AESGCM(key)
ciphertext = aesgcm.encrypt(nonce, msg, None)
plaintext = aesgcm.decrypt(nonce, ciphertext, None)
print("Ciphertext (hex):", ciphertext.hex())
print("Decrypted:", plaintext) # b'Hello, cryptography!'
Tip: Kerckhoffs's principle: a cryptographic system must be secure even if everything about it except the key is public. Never rely on 'security by obscurity'.
Hashing vs Encryption
- Hashing: One-way transformation — cannot be reversed
- Encryption: Two-way transformation — reversible with the correct key
Warning: Never invent your own cryptographic algorithm. Home-grown crypto is almost always broken. Use standardised algorithms (AES, ChaCha20, SHA-256, RSA, ECDSA) from audited libraries.
The Cryptographic Stack
Real-world security stacks layers of primitives. A primitive (AES, SHA-256) is a building block. A scheme (AES-GCM) combines primitives safely. A protocol (TLS) orchestrates multiple schemes to solve a full security problem.
Python's cryptography library (pyca/cryptography) is the recommended choice. Install with: pip install cryptography argon2-cffi pyjwt.
Frequently Asked Questions
Is keeping the algorithm secret a good idea?
No. Per Kerckhoffs's principle, security must rest on the key, not the algorithm. Secret algorithms are hard to audit and historically break once discovered.
Can I write my own encryption algorithm?
Only for learning. Production systems must use standardised, peer-reviewed algorithms. Creating secure cryptography requires years of cryptanalysis.
What is the difference between encoding and encryption?
Encoding (Base64, URL encoding) transforms data for transport — it provides zero security. Encryption requires a key and provides confidentiality. Never confuse the two.