Skip to content
Velloreth
Cybersecurity 4 min read

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

The three primitives

PrimitiveReversibleKeyUse
Symmetric encryptionyesonebulk data
Asymmetric encryptionyesa paira key exchange
Hashingnononeintegrity, 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:

AlgorithmNotes
AES-256-GCMthe modern default
ChaCha20-Poly1305fast without hardware support
AES-CBClegacy, 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:

AlgorithmNotes
RSAlarge keys, widely supported
ECDHa key agreement
X25519modern, 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:

AlgorithmUse
SHA-256integrity
SHA-3integrity
bcrypt, scrypt, Argon2a password
MD5, SHA-1broken, 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.

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.

Frequently asked questions

What is the difference between encryption and hashing?
Encryption is reversible with a key, and hashing is one-way. Encryption is used to hide data, and hashing is used to verify it or to store a password. A hash is not encryption, and a password should be hashed with a slow algorithm rather than encrypted.
What is symmetric encryption?
It uses one key for both encryption and decryption. It is fast and is used for bulk data. AES-256-GCM is the modern default. The hard part is the key distribution, because both sides need the same key.
What is asymmetric encryption?
It uses a key pair: the public key encrypts and the private key decrypts. It is slow, so it is used to exchange a symmetric key rather than to encrypt bulk data. RSA and elliptic curve are the common algorithms.
What is an AEAD mode?
An authenticated encryption mode, such as AES-GCM or ChaCha20-Poly1305, which encrypts the data and produces a tag that detects tampering. It is the modern default, because it prevents an attacker from modifying the ciphertext.
What is a nonce?
A number used once. In a stream cipher or an AEAD mode, reusing a nonce with the same key breaks the security completely, because the keystream repeats. Generate a random nonce for every message, and never reuse one.
Should I write my own encryption?
No. Use a well-reviewed library and a high-level API, such as libsodium or the AEAD mode of a standard library. A hand-rolled scheme is almost always broken, and the failure is invisible until it is exploited.