Skip to content

Cryptography

Cryptography is the science of securing communication and data so that only intended parties can access it. It is the mathematical foundation upon which virtually all digital security is built — from the HTTPS lock icon in your browser to the encrypted messages on your phone, from password storage to blockchain consensus. Without cryptography, the modern internet would be impossible.


Why Encryption Matters

Every day, billions of sensitive transactions cross networks that are fundamentally untrusted. Without cryptography:

  • Passwords would travel over the internet in plain text
  • Credit card numbers could be intercepted by anyone between you and the merchant
  • Medical records would be readable by any network administrator along the path
  • Private messages would be as public as a postcard
  • Software updates could be replaced with malware in transit

Real-World Consequences of Weak Cryptography

IncidentWhat Went WrongImpact
Adobe (2013)Passwords encrypted with weak, reversible encryption (3DES-ECB) instead of hashing153 million credentials exposed
Heartbleed (2014)OpenSSL bug leaked private keys from server memoryMillions of servers exposed
WPA2 KRACK (2017)Protocol flaw in Wi-Fi encryption allowed key reinstallationAll Wi-Fi devices vulnerable
SolarWinds (2020)Compromised code signing allowed malicious updates to appear legitimate18,000+ organizations affected

Key Concepts

Before diving into specific algorithms, you need to understand the fundamental vocabulary of cryptography.

Plaintext, Ciphertext, and Keys

┌──────────────┐ ┌──────────────┐
│ Plaintext │ Encryption │ Ciphertext │
│ │────────────────────▶ │ │
│ "Hello World"│ (using a key) │ "7g$kL9!xmP" │
│ │ │ │
└──────────────┘ └──────────────┘
▲ │
│ Decryption │
└────────────────────────────────────┘
(using a key)
TermDefinition
PlaintextThe original, readable data before encryption
CiphertextThe encrypted, unreadable output after encryption
KeyA secret value used by the encryption algorithm to transform plaintext into ciphertext (and back)
EncryptionThe process of converting plaintext to ciphertext using a key
DecryptionThe process of converting ciphertext back to plaintext using a key
CipherThe algorithm that performs encryption and decryption

Kerckhoffs’s Principle


Types of Cryptography

Cryptography falls into three main categories, each serving a different purpose.

┌──────────────────────────────────────────────────────────────┐
│ Types of Cryptography │
│ │
│ ┌──────────────────┐ ┌──────────────────┐ │
│ │ Symmetric │ │ Asymmetric │ │
│ │ Encryption │ │ Encryption │ │
│ │ │ │ │ │
│ │ Same key for │ │ Public key to │ │
│ │ encrypt and │ │ encrypt, private │ │
│ │ decrypt │ │ key to decrypt │ │
│ │ │ │ │ │
│ │ AES, ChaCha20 │ │ RSA, ECDSA, │ │
│ │ │ │ Diffie-Hellman │ │
│ └──────────────────┘ └──────────────────┘ │
│ │
│ ┌──────────────────────────────────────────┐ │
│ │ Hashing │ │
│ │ │ │
│ │ One-way function: input to fixed-size │ │
│ │ output. Cannot be reversed. │ │
│ │ │ │
│ │ SHA-256, bcrypt, Argon2, HMAC │ │
│ └──────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────┘

Quick Comparison

PropertySymmetricAsymmetricHashing
KeysOne shared keyPublic + private key pairNo key (or key for HMAC)
DirectionTwo-way (encrypt/decrypt)Two-way (encrypt/decrypt)One-way (cannot reverse)
SpeedVery fastSlow (100-1000x slower)Fast
Key distributionHard (must share secretly)Easy (public key is public)N/A
Use casesBulk data encryptionKey exchange, digital signaturesPassword storage, integrity checks
ExampleAES-256-GCMRSA-2048, Ed25519SHA-256, Argon2id

Cryptographic Properties

Secure systems rely on four core cryptographic properties. Different primitives provide different combinations of these properties.

PropertyDescriptionProvided By
ConfidentialityOnly authorized parties can read the dataSymmetric encryption, asymmetric encryption
IntegrityData has not been tampered withHashing, MACs, digital signatures
AuthenticationThe sender is who they claim to beDigital signatures, MACs, certificates
Non-repudiationThe sender cannot deny sending the messageDigital signatures (asymmetric only)
Confidentiality Integrity
"Can't read it" "Can't change it"
│ │
▼ ▼
┌──────────┐ ┌──────────────┐
│Encryption│ │ Hashing / │
│(AES, RSA)│ │ MAC / Sig │
└──────────┘ └──────────────┘
Authentication Non-repudiation
"Know who sent it" "Can't deny sending"
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Digital Sig │ │ Digital Sig │
│ MAC, Cert │ │ (asymmetric) │
└──────────────┘ └──────────────┘

A Practical Overview

Here is a simple example showing the three types of cryptography in action:

from cryptography.fernet import Fernet
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import hashes, serialization
import hashlib
# --- Symmetric Encryption (AES via Fernet) ---
# Same key for encryption and decryption
symmetric_key = Fernet.generate_key()
cipher = Fernet(symmetric_key)
plaintext = b"Sensitive data: account balance is $10,000"
ciphertext = cipher.encrypt(plaintext)
decrypted = cipher.decrypt(ciphertext)
print(f"Symmetric - Encrypted: {ciphertext[:40]}...")
print(f"Symmetric - Decrypted: {decrypted.decode()}")
# --- Asymmetric Encryption (RSA) ---
# Public key encrypts, private key decrypts
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
public_key = private_key.public_key()
ciphertext_rsa = public_key.encrypt(
b"Secret message",
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None,
),
)
decrypted_rsa = private_key.decrypt(
ciphertext_rsa,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None,
),
)
print(f"Asymmetric - Decrypted: {decrypted_rsa.decode()}")
# --- Hashing (SHA-256) ---
# One-way: cannot reverse the hash to get the original
message = b"Verify this message has not been tampered with"
digest = hashlib.sha256(message).hexdigest()
print(f"Hash (SHA-256): {digest}")
# Verify integrity: hash the message again and compare
is_intact = hashlib.sha256(message).hexdigest() == digest
print(f"Integrity check: {'PASS' if is_intact else 'FAIL'}")

Common Mistakes in Cryptography

MistakeWhy It Is DangerousWhat to Do Instead
Inventing your own cipherGuaranteed to have flawsUse AES-256-GCM or ChaCha20-Poly1305
Using ECB modeIdentical plaintext blocks produce identical ciphertextUse GCM or CTR mode
Reusing nonces/IVsDestroys confidentiality guaranteesGenerate a unique nonce for every encryption
Using MD5 or SHA-1Broken — collisions can be generatedUse SHA-256 or SHA-3
Storing passwords with encryptionReversible — attacker with the key gets all passwordsHash with bcrypt, scrypt, or Argon2id
Hardcoding keys in source codeKeys exposed in version controlUse a secrets manager (Vault, AWS KMS)
Not authenticating ciphertextAllows ciphertext manipulation attacksUse authenticated encryption (GCM, Poly1305)

When to Use What

ScenarioRecommended Approach
Encrypting data at rest (files, database columns)AES-256-GCM with keys from a KMS
Encrypting data in transit (network)TLS 1.3 (which uses symmetric + asymmetric internally)
Storing user passwordsArgon2id or bcrypt — never encryption
Verifying file integritySHA-256 hash
Authenticating API requestsHMAC-SHA256
Signing software releasesEd25519 or RSA digital signatures
Secure key exchange over untrusted networkDiffie-Hellman key exchange (ECDHE)
Sending encrypted emailAsymmetric encryption (RSA/PGP)

Topics in This Section

Symmetric & Asymmetric Encryption

Deep dive into AES, ChaCha20 (symmetric), RSA, ECDSA, Diffie-Hellman (asymmetric), key exchange, and hybrid encryption.

Explore Encryption

Hashing & Digital Signatures

Understand SHA-256, bcrypt, Argon2, HMAC, digital signatures, message authentication, and password hashing best practices.

Explore Hashing

TLS & PKI

Learn about TLS 1.3 handshake, certificates, Certificate Authorities, PKI, mTLS, certificate pinning, and Let’s Encrypt.

Explore TLS & PKI


Further Reading

  • Serious Cryptography by Jean-Philippe Aumasson
  • Cryptography and Network Security by William Stallings
  • The Code Book by Simon Singh (accessible, historical perspective)
  • Crypto101 (free online book at crypto101.io)