Encryption vs Encoding vs Hashing

They might appear in the same context, but they are vastly different

Martin Thoma
Feb 2 · 3 min read

Encryption is about keeping a secret and being able to restore it. Hashing is about fingerprinting — you don’t need to restore the original, but you need to make sure it is identical. Encoding is about data representation to enable information exchange. Encoding does not involve keeping secrets.

This was my Twitter-length explanation. Let’s dive into details!

Encoding

Photo by Quino Al on Unsplash

Encoding is about data representation. For example, for icons on the web, we prefer not to store image files but have them directly on the web page. This prevents the client from creating many HTTP requests for little data.

But then the binary data of the image has to be converted to text data. A common way to do that is base64 encoding.

As you can see, the “translation” is trivial: We build 6-bit blocks of the binary data and look up the character in the table above. It’s called base64 because there are 2⁶ = 64 digits. Hence it can be interpreted as a number base conversion.

Please note that this does not keep the content secret. Base64 does not use a secret key. Hence it is not encryption.

Character encodings are also extremely common. They map an integer to a character. The three which I stumble over most often are UTF8, ASCII, Latin1.

Encryption

Photo by Mauro Sbicego on Unsplash

Encryption is about keeping secrets. You don’t want to keep the method how you encrypt and decrypt secret. Instead, you should have a secret key that is necessary to decrypt. This is called Kerckhoffs’s principle.

Mathematically speaking, you have two functions:

encrypt(plain text, key) -> cipher text
decrypt(cipher text, key) -> plain text

This is called a symmetric-key algorithm as you use the same key for encrypting and for decrypting. There are asymmetric-key algorithms as well, but this would go too far.

A very early scheme to encrypt was to use a natural sentence as a key to encrypt single characters. If the sentence happens to have one character multiple times, the second and following characters are deleted:

Sentence   : The quick brown fox jumps
Derived key: the quickbrownfxjmpsadglvyz

Then you translate character by character:

Original     : the quickbrownfxjmpsadglvyz
Translates to: abcdefghijklmnopqrstuvwxyz

Note that “space” translates to “d” and “z” translates to “space”.

Putting it all together, the message secret translates to tchkca .

In Python, it looks like this:

Modern encryption algorithms are a bit more complicated. The state of the art is AES — the Advanced Encryption Standard. Notable mentions are Twofish, Serpent, SM4, and SEED.

Hashing

Photo by Immo Wegmann on Unsplash

Hashing is about fingerprinting. You want to be able to uniquely identify a list of bytes (e.g. a string or a file), but you don’t want to store it. You either don’t need to be able to go back to the original or you don’t even want it. Just like with a fingerprint: You can take two fingerprints and conclude that they belong to the same person. But given only one fingerprint, you cannot reconstruct that person.

I wrote an article that explains in detail why this loss of information is desired:

But there are several other applications of hash functions as well:

State-of-the-art hash functions are SHA-256 or SHA-512.

Plain and Simple

Explain concepts in just a few words

Martin Thoma

Written by

I’m a Software Engineer with focus on Security, Data Science, and ML. I have over 10 years of experience with Python. https://www.linkedin.com/in/martin-thoma/

Plain and Simple

Some concepts are hard to put into context. Examples and contrasting the concepts to related ones help

Martin Thoma

Written by

I’m a Software Engineer with focus on Security, Data Science, and ML. I have over 10 years of experience with Python. https://www.linkedin.com/in/martin-thoma/

Plain and Simple

Some concepts are hard to put into context. Examples and contrasting the concepts to related ones help

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store