☼
⌃
☰

Encryption

Table of Contents

Transforms plaintext into ciphertext using an algorithm and key, making the data unreadable to anyone without the correct key. Distinct from Hashing (which is one-way) — encryption is reversible by the intended recipient.

Related: Cryptography Hashing PKI TLS / SSL Elliptic Curves

Symmetric Encryption

The same key encrypts and decrypts. Fast and efficient for bulk data. The key distribution problem — how do two parties securely share the key in the first place — is the primary limitation.

Algorithm Key size Notes
AES-256 256-bit Industry standard — use this
AES-128 128-bit Acceptable, slightly faster
3DES 168-bit effective Legacy — avoid in new systems
DES 56-bit Broken — never use
ChaCha20 256-bit Fast in software, used in TLS 1.3

Modes of operation

The mode determines how AES processes data larger than one block (128 bits). Mode choice matters — ECB is insecure regardless of key length:

Mode Properties Use
ECB No chaining — identical blocks produce identical ciphertext Never use
CBC Chaining with IV — patterns hidden Legacy TLS, disk encryption
CTR Stream cipher mode, parallelisable General purpose
GCM CTR + authentication tag TLS 1.3, AEAD — preferred

GCM (Galois/Counter Mode) provides both confidentiality and integrity in one operation — the standard for new systems.

PYTHON
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os

key = AESGCM.generate_key(bit_length=256)
aesgcm = AESGCM(key)
nonce = os.urandom(12)   # 96-bit nonce for GCM

ciphertext = aesgcm.encrypt(nonce, b"plaintext data", None)
plaintext  = aesgcm.decrypt(nonce, ciphertext, None)

Asymmetric Encryption

A mathematically linked key pair: a public key (shareable) and a private key (secret). Data encrypted with the public key can only be decrypted with the private key. Solves the key distribution problem but is orders of magnitude slower than symmetric encryption.

In practice: asymmetric encryption is used to exchange a symmetric key, which then encrypts the actual data (hybrid encryption). This is how TLS works.

Algorithm Based on Notes
RSA Integer factorisation Widely deployed, minimum 2048-bit keys
ECDH Elliptic curves Key exchange — used in TLS 1.3
ECDSA Elliptic curves Digital signatures
Ed25519 Twisted Edwards curves SSH keys, fast, modern

See Elliptic Curves for the mathematical foundation.

Digital signatures

Signing uses the private key; verification uses the public key — the inverse of encryption. Provides authentication and non-repudiation.

TEXT
Signing:      hash(data) → encrypt with private key → signature
Verification: decrypt signature with public key → compare to hash(data)

Hybrid Encryption

The standard in practice — TLS, PGP, SSH all use this pattern:

TEXT
1. Generate a random symmetric session key
2. Encrypt the session key with the recipient's public key
3. Encrypt all data with the session key (AES-GCM)
4. Send: encrypted session key + encrypted data
5. Recipient decrypts session key with private key
6. Recipient decrypts data with session key

At Rest vs In Transit

Context What it means Example
At rest Data stored on disk or in a database Full disk encryption (BitLocker, LUKS), encrypted database columns
In transit Data moving over a network TLS/HTTPS, SSH, encrypted VPN
In use Data being processed in memory Confidential computing, homomorphic encryption (experimental)

Security Relevance

Common weaknesses

TEXT
— Using weak algorithms (DES, RC4, MD5 for key derivation)
— ECB mode — leaks block patterns
— Short key lengths (RSA < 2048-bit)
— Hardcoded keys in source code
— Poor key management — keys stored alongside encrypted data
— Not validating certificates — allows MITM despite encryption
— Encrypting without integrity (non-AEAD modes) — malleable ciphertext

Identifying encryption in use

SHELL
# Check TLS cipher suite offered by a server
nmap --script ssl-enum-ciphers -p 443 $TARGET
sslscan $TARGET:443
testssl.sh https://$TARGET

# Check certificate
openssl s_client -connect $TARGET:443 | openssl x509 -text | grep -E "Subject:|Issuer:|Not After:|Public Key"

# Find hardcoded keys or base64 blobs
grep -r "AES\|BEGIN.*KEY\|ENCRYPTED\|password.*=" /var/www/ 2>/dev/null
strings binary_file | grep -E "[A-Za-z0-9+/]{32,}={0,2}"

Tips

  • Always use authenticated encryption (AES-GCM, ChaCha20-Poly1305) rather than encryption-only modes — unauthenticated ciphertext can be manipulated without the key
  • Key length matters less than algorithm choice — AES-128 is computationally secure; DES with a 56-bit key is broken
  • Encryption and integrity are separate properties — always use AEAD modes or combine encryption with HMAC
  • Encrypting data on disk with the key stored in the same location is equivalent to no encryption
  • For most practical security work, trust the platform — use TLS for in-transit, use the OS keystore for keys at rest, and use a vetted library (cryptography, OpenSSL) rather than implementing algorithms yourself