Comparison
Encoding vs Encryption: What's the Difference and Why You Shouldn't Confuse Them
TL;DR
| Encoding | Encryption | |
|---|---|---|
| Purpose | Transport / storage format conversion | Confidentiality and data protection |
| Reversibility | Anyone who knows the scheme can decode | Only the key holder can decrypt (without the key, infeasible) |
| Key | No key — it's a deterministic algorithm | Requires a secret key or key pair |
| Examples | Base64, Hex, URL Encode, UTF-8 | AES-GCM, RSA-OAEP, ChaCha20-Poly1305 |
| Security | Zero security — do not treat encoded data as protected | Provides confidentiality (and with GCM, authenticity too) |
| Common misuse | Storing 'encrypted' passwords in Base64 | Using ECB mode (a variant that leaks patterns in the plaintext) |
The options in depth
Encoding
Data format transformation — zero security, pure convenience.
Good for
- ·Transmitting binary data in text-only channels (email, JSON)
- ·Debugging binary payloads (hex dumps)
- ·Making URLs safe (URL encoding)
Avoid when
- ·Storing secrets (use AES-GCM or a vault)
- ·Access control (encoding does not restrict who can read)
Encryption
Data confidentiality — requires a secret, purposefully hard to reverse.
Good for
- ·Protecting data at rest (databases, files on disk)
- ·Protecting data in transit (TLS already does this for HTTP)
- ·API key storage (encrypt the key before writing to disk)
Avoid when
- ·Display purposes only (encoding is sufficient)
- ·Data that needs to be read by multiple systems without key sharing (consider token-based auth instead)
Which one should you pick?
→ Is Base64 encoding 'enough' for storing API keys?
Absolutely not. Base64 is trivially reversible by anyone who sees the string — a one-line echo 'VG9rZW4=' | base64 --decode command in any terminal recovers the plaintext. Encrypt the key with AES-GCM and store the encryption key separately.
→ Why does JWT use Base64URL if it's not secure?
JWT uses Base64URL for the header and payload to make them URL-safe and compact — not for security. The security comes from the signature (third segment) which is a cryptographic HMAC or asymmetric signature. The header and payload are transmitted in the clear — that's why you should never put secrets in JWT claims.
Common pitfalls
- ⚠Calling Base64 'encryption' in documentation is a red flag for security auditors and could fail compliance reviews. Always use the precise term: encode/decode vs encrypt/decrypt.
- ⚠AES-ECB mode encrypts identical plaintext blocks into identical ciphertext blocks — the famous ECB penguin image is the canonical demonstration. Always use GCM (or at minimum CBC with a random IV).
Frequently Asked Questions
Can I combine encoding and encryption?
Yes, and you should. Encrypt first to get binary ciphertext, then Base64-encode it to make it safe for JSON or URL transmission. This is what JWE (JSON Web Encryption) does internally.
Is hashing a form of encoding or encryption?
Neither — hashing is a one-way function that produces a fixed-size digest (SHA-256, bcrypt, Argon2). You cannot recover the original from a hash, unlike both encoding and encryption.
Related comparisons
MD5 vs SHA-256 vs bcrypt
MD5 and SHA-256 are fast hashes for fingerprints; bcrypt is a deliberately slow hash for passwords. Using the wrong one is a top security bug — this guide explains when each fits.
RSA vs ECDSA
RSA offers compatibility with every system built before 2020; ECDSA offers smaller keys, faster signing, and better mobile performance. This guide compares key sizes, speed, ecosystem support, and real-world adoption.