SECURITY ENGINEERING · PART 4 · CRYPTOGRAPHY IN PRACTICE · BASICS · 45 OF 229

Encoding vs Hashing vs Encryption Explained

Three things that all turn readable data into unreadable-looking data, and are constantly confused: encoding (Base64, hex) changes the format and needs no key, hashing (SHA-256) is a one-way fingerprint, and encryption (AES) is reversible only with a key. Each shown on real values, with what it is for, what it protects, and the mistakes that come from mixing them up.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 13

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Encoding: format, no secret
  • hashing: one-way fingerprint
  • encryption: reversible with a key
  • the key is the only secret

Notes

Encoding

Encoding changes the format of data so it can travel safely through systems that only handle text. Base sixty four and hexadecimal are the common ones. Here the word password one two three becomes a string of letters ending in an equals sign. It looks scrambled. It is not protected at all. No key was used, and one function call turns it straight back. Encoding is for compatibility. It gives zero confidentiality, and anyone who claims otherwise has confused it with encryption.

Base64 and hex
>>> base64.b64encode(b"password123")
b'cGFzc3dvcmQxMjM='
>>> b"password123".hex()
'70617373776f7264313233'
>>> base64.b64decode(
...   'cGFzc3dvcmQxMjM=')
b'password123'

# no key was used at any point

Spot it

Learn to recognise encoding on sight, because it appears everywhere. Base sixty four uses letters, digits, plus and slash, and often ends with one or two equals signs. Hex is digits and the letters a to f. Two cases catch people out. A JSON web token is three Base sixty four parts joined by dots, and its middle part, which holds the user's details, can be read by anyone who has the token. And HTTP basic authentication is just the user name and password in Base sixty four.

What encoding looks like
  • Base64: A-Z a-z 0-9 + / often ends in = or == hex: only 0-9 and a-f, even length
  • URL encoding: %20 %3D
  • a JWT: three Base64 parts joined by dots; the middle part is readable by anyone
  • HTTP Basic auth: Base64 of user:password, not encrypted

Hashing

Hashing is different in kind. A hash function takes any input, of any size, and produces a fixed-size fingerprint. SHA two fifty six always gives two hundred and fifty six bits, shown here as sixty four hex characters. The same input always gives the same output. There is no key, and there is no way back: you cannot compute the input from the fingerprint. You can only guess inputs and compare, which matters a great deal when we get to passwords.

SHA-256
>>> hashlib.sha256(b"hello").hexdigest()
'2cf24dba5fb0a30e26e83b2ac5b9e29e
 1b161e5c1fa7425e73043362938b9824'

# any input, always 256 bits out
# same input, same output
# no key, and no way back

Avalanche effect

A good hash has the avalanche effect. We changed one letter, hello to hell p, and the fingerprint is unrecognisably different: one hundred and thirty one of the two hundred and fifty six bits flipped, about half, which is what randomness looks like. That property is why hashes prove integrity. If a downloaded file's hash matches the published one, not a single bit has changed. If it differs at all, something did, and you should not trust the file.

the avalanche effect
sha256("hello") = 2cf24dba5fb0a30e...
sha256("hellp") = fdd7585e08c4e2af...

# one letter changed
# 131 of the 256 output bits
# flipped: about half

# so a hash proves that data
# has not changed at all

Encryption

Encryption is the one that keeps secrets. It transforms data using a key, and only someone with the right key can transform it back. Here we encrypt a short string with AES, the standard cipher. Twenty one bytes go in and thirty seven come out: the data plus a sixteen byte tag that detects tampering. With the key, decryption returns the original. With the wrong key, or if one bit of the ciphertext changed, it raises an error and returns nothing at all.

AES-256-GCM
key   = AESGCM.generate_key(256)
nonce = os.urandom(12)
ct = AESGCM(key).encrypt(
        nonce, b"card=4111...", None)
# 21 bytes in, 37 bytes out
# (data + a 16-byte check tag)
AESGCM(key).decrypt(nonce, ct, None)
# b'card=4111...'
# wrong key or changed data:
# InvalidTag, nothing returned

Side by side

Three questions tell them apart. Is there a key? Can it be reversed? And what is it for? Encoding has no key, anyone can reverse it, and it exists for format. Hashing has no key, nobody can reverse it, and it is for integrity and for storing passwords. Encryption has a key, only the key holder can reverse it, and it is for secrecy. One rule covers a lot of confusion: if there is no key, there is no secret.

The comparison
            key?  reverse?    used for
encoding    no    anyone      format
hashing     no    nobody      integrity,
                              passwords
encryption  yes   key holder  secrecy

if there is no key,
there is no secret

Which one when

So which one when? To send binary data through a system that expects text, encode. To check that a file was not changed, hash. To store a user's login password, hash it, with a deliberately slow password hash that gets its own episode. To store something you must read back later, such as a card number or an address, encrypt. And for data in transit, use TLS, which combines all three inside one protocol you do not have to assemble yourself.

Choosing
  • send bytes through a text system: encode
  • check a file was not changed: hash
  • store a login password: hash, with a slow password hash (episode 5)
  • store a card number you must read back: encrypt
  • protect data in transit: TLS, which uses all three

The real secret

One principle sits under everything in this part. A cryptographic system must stay secure even if everything about it is public, except the key. AES and SHA two fifty six are published standards that thousands of experts have attacked for decades, and that scrutiny is why we trust them. So the secret is never the algorithm. It is the key. And that makes the hard problems practical ones: creating keys, storing them, sharing them and rotating them. Most of Part four is about exactly that.

Kerckhoffs's principle
  • a system must stay secure even if everything about it is public, except the key
  • AES, SHA-256, RSA: all published and studied for decades
  • so the hard problems are: making keys, storing them, sharing them, rotating them
  • that is most of this part

Never invent your own

A rule every security engineer repeats: never invent your own cryptography. Anyone can design a scheme that they personally cannot break, and it means nothing. Home-made schemes fail in ways their authors cannot see. Even combining good pieces in a new way is risky. Use high-level, reviewed libraries, and your cloud's key management service, and choose the boring, standard options. In cryptography, boring is the highest compliment.

Use the standard pieces
  • do not design algorithms
  • do not combine primitives yourself unless you must
  • use high-level, reviewed libraries: libsodium, Tink, the cryptography package, your platform KMS
  • pick "boring" defaults: AES-GCM, SHA-256, Argon2, TLS 1.3

Common mistakes

Mistakes. Calling something encrypted when it is only Base sixty four encoded. Storing passwords encrypted, or worse in plain text, instead of hashed. Using a fast hash like MD five or plain SHA two fifty six for passwords. Hashing data you later need to read back. Treating the contents of a JSON web token as secret. Storing the encryption key right beside the data it protects. And inventing a cipher. Each of these has caused real breaches.

Avoid these
  • "encrypted" with Base64
  • passwords stored encrypted (or plain) instead of hashed
  • MD5 or plain SHA-256 for passwords
  • hashing data you need back
  • trusting a JWT payload as secret
  • a key stored beside the data it protects
  • a home-made cipher

Say it like this

In the interview: encoding changes the format of data with no key, and anyone can reverse it; Base sixty four is the example, and it provides no security. Hashing is a one-way function producing a fixed-size fingerprint, used for integrity and, with a slow algorithm such as Argon two, for storing passwords. Encryption is reversible only with a key and provides confidentiality; AES in GCM mode is the standard choice. Then give one example of each being misused.

In the interview
  • "Encoding changes format, no key, anyone reverses it: Base64. Hashing is a one-way
  • fixed-size fingerprint for integrity and password
  • storage: SHA-256, Argon2.
  • Encryption is reversible only with a key, for confidentiality: AES-GCM."

Interview questions

Q1What is the difference between encoding, hashing and encryption?

Encoding converts data to another format without a key and is trivially reversible (Base64, hex). Hashing is a one-way function producing a fixed-size digest, used for integrity and password storage. Encryption is reversible only with a key and provides confidentiality.

Q2Is Base64 a form of encryption?

No. Base64 is an encoding that represents binary data as text. It uses no key and anyone can decode it, so it provides no confidentiality. Data that is only Base64-encoded should be treated as plain text.

Q3What properties does a cryptographic hash function have?

It is deterministic, produces a fixed-size output, is one-way (preimage resistant), is collision resistant, and has the avalanche effect, where a small input change alters about half the output bits.

Q4Why are passwords hashed rather than encrypted?

The system only needs to verify a password, not read it back. Hashing means even someone who steals the database, or the administrators, cannot recover passwords directly, whereas encrypted passwords are exposed as soon as the key is obtained.

Q5When should data be encrypted rather than hashed?

When the original value must be recovered later, for example card numbers, addresses or files. Hashing is irreversible, so it suits verification and integrity checks but not data you need to read.

Q6What is Kerckhoffs’s principle?

A cryptographic system should remain secure even if everything about it except the key is public. Security must rest on the secrecy of the key, not the algorithm, which is why standard, publicly analysed algorithms are preferred.

Q7Is the payload of a JWT encrypted?

Not in the common signed form (JWS). The header and payload are only Base64URL-encoded and readable by anyone holding the token; the signature protects integrity, not confidentiality. Sensitive data should not be placed in the payload.

Q8Why should you never implement your own cryptography?

Subtle design and implementation flaws, such as nonce reuse, padding oracles or timing leaks, are invisible to the author yet exploitable. Reviewed libraries and standard constructions have survived extensive analysis and should be used instead.