Encryption Basics: Symmetric, Asymmetric and Hashing
Encryption hides data, hashing verifies it, and a signature proves origin. Learn the three, the keys, and the mistakes to avoid.
By Velloreth
The three primitives
| Primitive | Reversible | Key | Use |
|---|---|---|---|
| Symmetric encryption | yes | one | bulk data |
| Asymmetric encryption | yes | a pair | a key exchange |
| Hashing | no | none | integrity, a password |
They solve different problems. Confusing them is the source of most mistakes.
Symmetric encryption
One key for both directions.
ciphertext = encrypt(plaintext, key)
plaintext = decrypt(ciphertext, key)
Fast. It handles bulk data.
The algorithms:
| Algorithm | Notes |
|---|---|
| AES-256-GCM | the modern default |
| ChaCha20-Poly1305 | fast without hardware support |
| AES-CBC | legacy, needs a separate MAC |
The hard part is the key distribution. Both sides need the same key, and sending it is the problem that asymmetric encryption solves.
Asymmetric encryption
A key pair: the public key encrypts and the private key decrypts.
ciphertext = encrypt(plaintext, publicKey)
plaintext = decrypt(ciphertext, privateKey)
Slow. It is used to exchange a symmetric key, not to encrypt bulk data.
The algorithms:
| Algorithm | Notes |
|---|---|
| RSA | large keys, widely supported |
| ECDH | a key agreement |
| X25519 | modern, fast |
The pattern: use asymmetric encryption to agree on a symmetric key, then use the symmetric key for the data. This is what TLS does.
Hashing
One-way. There is no key and no reverse.
digest = SHA-256(data)
Uses:
- Verify a download
- Store a password, with a slow algorithm
- Build a cache key
- Detect a duplicate
The algorithms:
| Algorithm | Use |
|---|---|
| SHA-256 | integrity |
| SHA-3 | integrity |
| bcrypt, scrypt, Argon2 | a password |
| MD5, SHA-1 | broken, do not use |
A hash is not encryption. A password is hashed, not encrypted, because the server must not be able to recover it.
The AEAD mode
Authenticated encryption with associated data.
It encrypts the data and produces a tag that detects tampering.
ciphertext, tag = AES-GCM-encrypt(plaintext, key, nonce, aad)
Why it matters: without authentication, an attacker can modify the ciphertext and the decryption produces a wrong plaintext without an error.
AES-GCM and ChaCha20-Poly1305 are the modern defaults.
The alternative is encrypt-then-MAC, which is correct but easy to get wrong.
The nonce
A number used once.
Reusing a nonce with the same key breaks the security completely, because the keystream repeats and the XOR cancels.
The rule: generate a random nonce for every message, and never reuse one.
The size: 96 bits for GCM, which is large enough that a random value is safe.
The counter mode uses a counter, which must not wrap.
The key
The key is the secret. The algorithm is public.
The rules:
- Generate the key from a cryptographic random source
- Use 256 bits for a symmetric key
- Store it in a secret manager, not in the code
- Rotate it on a schedule and on a suspicion
- Never log it
A key in the code is a key that is in the repository, the image and the backup.
The mistakes
A nonce reuse. A complete break.
A weak key. A short key or a key from a password without a KDF.
No authentication. A modified ciphertext is not detected.
A hand-rolled scheme. A subtle flaw.
A password encrypted rather than hashed. The server can recover it.
A hash for a password without a salt. A rainbow table.
A fast hash for a password. A GPU cracks it.
A key in the code. A leaked secret.
A comparison with ==. A timing attack. Use a constant-time comparison.
A random value from a non-cryptographic source. A predictable key.
Common mistakes
Confusing a hash with encryption. A password is hashed.
A nonce reuse. A complete break.
No authentication. A silent modification.
A weak algorithm. MD5, SHA-1, DES, RC4.
A key in the repository. A leaked secret.
A hand-rolled scheme. A subtle flaw.
A short key. A brute force.
A timing-unsafe comparison. A leak.
No rotation. A long-lived compromise.
A quotable summary
Symmetric encryption uses one key and is fast, asymmetric encryption uses a key pair and is slow, and hashing is one-way and is not encryption. Use an AEAD mode such as AES-GCM or ChaCha20-Poly1305 so the ciphertext is authenticated, never reuse a nonce with the same key, store the key in a secret manager, and never write your own scheme.
Related reading
For the related topics, see encryption-at-rest-vs-transit and password-storage-best-practices. For the broader context, zero-trust-for-small-teams and oauth2-explained.