John Mwaniki /   19 Jun 2023

What is the difference between Encryption and Hashing?

Encryption and hashing are two commonly used terms in the field of cybersecurity. They are both processes that play critical roles in information security, but they serve distinct purposes and offer different levels of protection.

In this article, we will have an in-depth look at what encryption and hashing are, and how they differ from each other.

Encryption

Encryption is a process of transforming plaintext (readable data) into ciphertext (unreadable data) using an encryption algorithm and a cryptographic key.

The ciphertext can only be deciphered or decrypted back into the original plain text by authorized parties who possess the necessary decryption key.

The primary objective of encryption is to ensure data confidentiality. It provides a secure way of transmitting sensitive information over insecure channels, such as the internet, by making it unintelligible (gibberish) to anyone who intercepts it.

Encryption algorithm: A set of mathematical operations and rules used to transform plaintext into ciphertext.

Key: A unique piece of information used in encryption algorithms to encrypt and decrypt data.

Types of Encryption

There are two main types of encryption:

  • Symmetric Encryption: Also known as secret-key encryption, this method uses the same key for both encryption and decryption processes. Examples include the Data Encryption Standard (DES), Advanced Encryption Standard (AES)), and Triple Data Encryption Standard (3DES).
  • Asymmetric Encryption: Also called public-key encryption, this method employs a pair of keys: a public key for encryption and a private key for decryption. Examples include the Rivest-Shamir-Adleman (RSA) and Elliptic Curve Cryptography (ECC) algorithms.

Hashing

Hashing is a one-way process that takes an input (often referred to as a message or data) and produces a fixed-length string of characters, known as a hash value or digest.

The resulting hash value is unique to the input data and even a slight change in the input will produce a significantly different hash value.

Hashing is primarily used for data integrity and verifying the integrity of transmitted or stored data.

The key aspect of hashing is that it is non-reversible, meaning the original input cannot be retrieved from the hash value.

Common hashing algorithms include MD5, SHA-1, SHA-256, and SHA-512.

MD5 produces a short hash value compared to the rest. A longer hash is more secure as it's much harder to crack. It's therefore advisable to avoid using MD5 at all costs if possible.

A common use case of hashing is for increasing security when storing user accounts passwords in the database. It is strongly advised against storing plaintext passwords in the database.

When a user creates an account on a website, their password is hashed and stored in the database as a hash value.

When the user attempts to log in, the entered password is hashed using the same algorithm, and the resulting hash value is compared to the stored hash value. If the hash values match, the user is granted access.

This method ensures that even if the database is compromised, the original passwords remain secure.

Differences between Encryption and Hashing

The table below summarizes the differences between encryption and hashing:

Aspect Encryption Hashing
Purpose Confidentiality of data Data integrity and verification
Reversibility Reversible - The original text can be obtained from the cyphertext through decryption. Irreversible - There is no way of finding from the hash value what the original text was.
Output Ciphertext Hash value
Key Usage Encryption requires a secret key or public key to encrypt and decrypt data. Does not require a key to produce a hash value.
Length of Output Variable - The length of the resulting ciphertext varies depending on the length of the original data. Fixed - Regardless of how long or short the input data is, the resulting hash value for that algorithm will always be of the same length.
Usage Scenario Secure communication Password storage, file integrity check
Examples RSA, AES, DES MD5, SHA-1, SHA-256, SHA-512