Introduction to Cryptography
Cryptography is the science of securing communication and data through mathematical techniques. From ancient ciphers to modern quantum-resistant algorithms, cryptography has been the silent guardian of secrets for millennia. Today, it underpins everything from secure web browsing and encrypted messaging to digital signatures and cryptocurrency transactions.
Modern cryptography serves three core purposes: confidentiality (keeping data secret), integrity (ensuring data hasn't been altered), and authenticity (verifying the identity of senders). These functions combine to enable trust in digital systems, forming the foundation of our connected world.
1. Historical Foundations
The history of cryptography spans thousands of years, with each era bringing new innovations and attacks.
- Caesar Cipher (c. 50 BCE): Simple shift cipher used by Julius Caesar
- Vigenère Cipher (1553): Polyalphabetic cipher that resisted frequency analysis for centuries
- Enigma Machine (1918): German encryption device broken by Alan Turing at Bletchley Park
- Data Encryption Standard (DES - 1977): First modern symmetric cipher, now considered broken
- RSA (1977): First practical public-key cryptosystem
- Advanced Encryption Standard (AES - 2001): Current standard for symmetric encryption
2. Symmetric Encryption
Symmetric encryption uses the same key for both encryption and decryption. It's fast and efficient, ideal for bulk data encryption.
2.1 Advanced Encryption Standard (AES)
AES is the current gold standard for symmetric encryption. It operates on 128-bit blocks with key sizes of 128, 192, or 256 bits. AES-256 is considered secure against brute-force attacks even with future technological advances.
# AES encryption in Python with PyCryptodome from Crypto.Cipher import AES from Crypto.Util.Padding import pad, unpad import os key = os.urandom(32) # 256-bit key cipher = AES.new(key, AES.MODE_CBC) iv = cipher.iv # Encrypt plaintext = b"Secret message" ciphertext = cipher.encrypt(pad(plaintext, AES.block_size)) # Decrypt cipher = AES.new(key, AES.MODE_CBC, iv) decrypted = unpad(cipher.decrypt(ciphertext), AES.block_size)
3. Asymmetric Encryption (Public-Key Cryptography)
Asymmetric encryption uses key pairs β a public key for encryption and a private key for decryption. This enables secure communication without prior key exchange.
3.1 RSA (Rivest-Shamir-Adleman)
RSA is the most widely used asymmetric algorithm, based on the difficulty of factoring large prime numbers. RSA-2048 is currently considered secure.
# RSA encryption in Python from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP # Generate key pair key = RSA.generate(2048) private_key = key.export_key() public_key = key.publickey().export_key() # Encrypt with public key cipher = PKCS1_OAEP.new(RSA.import_key(public_key)) ciphertext = cipher.encrypt(b"Secret message") # Decrypt with private key cipher = PKCS1_OAEP.new(RSA.import_key(private_key)) plaintext = cipher.decrypt(ciphertext)
4. Hash Functions
Hash functions produce fixed-length outputs (digests) from arbitrary inputs. They are one-way functions β infeasible to reverse.
# Hash functions in Python import hashlib # SHA-256 hash_sha256 = hashlib.sha256(b"Hello World").hexdigest() # Output: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e # SHA-3 hash_sha3 = hashlib.sha3_256(b"Hello World").hexdigest() # Password hashing with bcrypt (includes salt) import bcrypt salt = bcrypt.gensalt() hash_password = bcrypt.hashpw(b"secret", salt)
5. Digital Signatures
Digital signatures provide authentication, integrity, and non-repudiation β proving that a message originated from a specific sender and hasn't been altered.
# Digital signatures with ECDSA
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec
# Generate key pair
private_key = ec.generate_private_key(ec.SECP256R1())
public_key = private_key.public_key()
# Sign message
message = b"Important document"
signature = private_key.sign(message, ec.ECDSA(hashes.SHA256()))
# Verify signature
try:
public_key.verify(signature, message, ec.ECDSA(hashes.SHA256()))
print("Valid signature")
except InvalidSignature:
print("Invalid signature")
6. Public Key Infrastructure (PKI)
PKI enables trust in public keys through digital certificates issued by Certificate Authorities (CAs).
# Generate a self-signed certificate with OpenSSL openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes # View certificate details openssl x509 -in cert.pem -text -noout
7. Key Exchange Protocols
Key exchange protocols enable two parties to establish a shared secret over an insecure channel.
8. Post-Quantum Cryptography
Quantum computers, when sufficiently powerful, will break RSA and ECC using Shor's algorithm. NIST is standardizing quantum-resistant algorithms.
NIST Post-Quantum Finalists
- CRYSTALS-Kyber: Lattice-based KEM for encryption (selected for standardization)
- CRYSTALS-Dilithium: Lattice-based signature (selected)
- FALCON: Lattice-based signature (selected)
- SPHINCS+: Stateless hash-based signature (selected)
9. Cryptographic Implementation Best Practices
- Use authenticated encryption (AES-GCM, ChaCha20-Poly1305)
- Use constant-time comparison for secret values
- Store passwords with bcrypt, Argon2id, or PBKDF2
- Use cryptographically secure random generators (os.urandom, secrets module)
- Keep libraries updated (OpenSSL vulnerabilities appear regularly)
- Follow NIST, OWASP, and industry best practices
Conclusion
Cryptography is the foundation of digital security. From the ancient Caesar cipher to modern post-quantum algorithms, the field continues to evolve to meet new threats and challenges. Understanding symmetric and asymmetric encryption, hash functions, digital signatures, and key exchange protocols is essential for anyone building or securing digital systems.
As quantum computing advances, the transition to post-quantum cryptography will be one of the largest infrastructure changes in computing history. The principles you've learned here will guide you through that transition and beyond.