# Encoding vs hashing vs encryption: what is the difference?

Canonical: https://hackergpt.org/learn/encoding-hashing-encryption/

**Encoding rewrites data in another format, such as base64, and anyone can reverse it. Hashing turns data into a fixed-length fingerprint that cannot be reversed, only guessed and compared. Encryption scrambles data with a key so that only someone holding the right key can turn it back into the original.**

Part of the roadmap: [Stage 1, Foundations](https://hackergpt.org/learn/roadmap/#foundations)

## Why it matters

All three turn readable text into something that looks scrambled, which is why they get confused, and the confusion is behind real bugs. A token that is only base64-encoded hides nothing. A password that is encrypted instead of hashed can be recovered by anyone who steals the key. A password stored with a fast, unsalted hash such as MD5 can be guessed at high speed.

The question to ask is who can get the original back. With encoding, anyone: base64 is a public format defined in RFC 4648, and it exists so bytes survive systems that only handle text. With a hash, nobody, by design: NIST describes an approved hash function as one-way and collision-resistant, so the only attack is to guess an input and compare. With encryption, whoever holds the key, which NIST describes as transforming plaintext into ciphertext that conceals its meaning.

That decides when each one is right. Encode to transport data. Hash to check data without keeping it, which is how passwords should be stored, with a slow, salted algorithm such as Argon2id or bcrypt. Encrypt data you need to read again later, such as a stored API key or traffic over TLS.

## Example: the same word three ways

Run these on your own machine; they need nothing but a terminal. The word is hunter2:

```
$ printf 'hunter2' | base64
aHVudGVyMg==
$ printf 'aHVudGVyMg==' | base64 -d
hunter2

$ printf 'hunter2' | shasum -a 256
f52fbd32b2b3b86ff88ef6c490628285f482af15ddcb29541f94bcf526a3f6c7
$ printf 'hunter3' | shasum -a 256
fb8c2e2b85ca81eb4350199faddd983cb26af3064614e737ea9f479621cfa57a

$ printf 'hunter2' | openssl enc -aes-256-cbc -pbkdf2 -salt -pass pass:correct-horse -base64
U2FsdGVkX18Ly+IscI4F78vyYTS6Ark50Zs75ZpsYhU=
$ echo 'U2FsdGVkX18Ly+IscI4F78vyYTS6Ark50Zs75ZpsYhU=' | openssl enc -d -aes-256-cbc -pbkdf2 -pass pass:correct-horse -base64
hunter2
```

The base64 output decodes with no secret at all. The two hashes share nothing although the inputs differ by one letter, and neither can be turned back into the word; you could only hash guesses until one matches. The encrypted output changes every time you run the command, because of the random salt, and decrypts only with the passphrase: with a wrong one, OpenSSL answers 'bad decrypt'.

On CyberGym, the legal practice target, you can see the mistakes in one place. Its session token is a JWT, which is base64url-encoded, so you can read it. Inside, for your own test account, is your password as an unsalted MD5 hash, a fast hash that guess-and-compare cracks quickly. Two weaknesses, one token.

## Common mistakes

- **Treating base64 as protection.** It is a format, not a secret. Anything base64-encoded in a cookie, token or config file is readable by whoever has it.
- **Encrypting passwords instead of hashing them.** A stolen key recovers every password. Store passwords with a slow, salted hash such as Argon2id or bcrypt.
- **Using a fast hash such as MD5 or SHA-256 for passwords.** Fast hashes are built for speed, so guessing is fast too. Use an algorithm designed for passwords.
- **Saying 'decrypt the hash'.** A hash is never decrypted. It is cracked, by guessing inputs and comparing the results.

## Related terms

- [JSON Web Token (JWT)](https://hackergpt.org/learn/jwt/)
- [CTF (capture the flag)](https://hackergpt.org/learn/ctf/)
- [Security headers](https://hackergpt.org/learn/security-headers/)
- [CWE](https://hackergpt.org/learn/cwe/)

## Go deeper

- [Crypto CTF challenges for beginners: encodings, hashes and XOR](https://hackergpt.org/blog/crypto-ctf-challenges-for-beginners/)
- [Broken authentication examples: login, sessions and 2FA](https://hackergpt.org/blog/broken-authentication-examples/)
- [JWT vulnerabilities: examples you can test on your own app](https://hackergpt.org/blog/jwt-vulnerabilities-examples/)
- [CTF Mode: run a CTF on your own website](https://hackergpt.org/features/ctf-mode/)

## Sources

- [RFC 4648: The Base16, Base32, and Base64 Data Encodings](https://www.rfc-editor.org/rfc/rfc4648)
- [NIST CSRC Glossary: hash function](https://csrc.nist.gov/glossary/term/hash_function)
- [NIST CSRC Glossary: encryption](https://csrc.nist.gov/glossary/term/encryption)
- [OWASP Password Storage Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html)

Test only sites you own or have written permission to test. To practise without one, use CyberGym, a legal, deliberately vulnerable target. [Practise on CyberGym](https://cybergym.hackergpt.app).
