DEV Community

Dev Cookies
Dev Cookies

Posted on

πŸ” Encoding vs Hashing vs Encryption β€” Explained with Examples


When working with data security, three concepts often create confusion: encoding, hashing, and encryption. While they may look similar at first glance, they serve very different purposes.

In this blog, we’ll break them down one by one with clear definitions, use cases, and Java code snippets.


1️⃣ Encoding β€” Making Data Portable & Readable

πŸ‘‰ Definition:
Encoding is the process of converting data into a specific format so it can be safely transmitted or stored.
It’s not meant for security β€” anyone can decode it back to the original form.

πŸ‘‰ Examples:

  • Base64 encoding for sending binary files over text-based systems (like email, JSON, XML).
  • URL encoding for safe transmission of query parameters.

πŸ‘‰ Code Example (Base64 Encoding in Java):

import java.util.Base64;

public class EncodingExample {
    public static void main(String[] args) {
        String original = "Hello Nitesh!";

        // Encode
        String encoded = Base64.getEncoder().encodeToString(original.getBytes());
        System.out.println("Encoded: " + encoded);

        // Decode
        String decoded = new String(Base64.getDecoder().decode(encoded));
        System.out.println("Decoded: " + decoded);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Encoded: SGVsbG8gTml0ZXNoIQ==
Decoded: Hello Nitesh!
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ Key point: Encoding is reversible and used for data formatting, not for security.


2️⃣ Hashing β€” One-Way Fingerprint of Data

πŸ‘‰ Definition:
Hashing transforms input data into a fixed-length string (hash). It’s one-way only β€” you cannot get the original data back.

πŸ‘‰ Uses:

  • Password storage in databases
  • Data integrity checks (e.g., MD5, SHA-256 checksums)

πŸ‘‰ Code Example (SHA-256 Hashing in Java):

import java.security.MessageDigest;

public class HashingExample {
    public static void main(String[] args) throws Exception {
        String password = "MySecret123";

        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] hashBytes = digest.digest(password.getBytes());

        // Convert bytes to hex
        StringBuilder hexString = new StringBuilder();
        for (byte b : hashBytes) {
            hexString.append(String.format("%02x", b));
        }

        System.out.println("Original: " + password);
        System.out.println("SHA-256 Hash: " + hexString);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output Example:

Original: MySecret123
SHA-256 Hash: 94e0a3a98a5e8... (64 hex chars)
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ Key point: Hashing is irreversible and is best for verifying data, not hiding it.


3️⃣ Encryption β€” Secure & Reversible Transformation

πŸ‘‰ Definition:
Encryption converts data into a secret format using a key.

  • With the correct key, it can be decrypted back.
  • Without the key, it’s unreadable.

πŸ‘‰ Types:

  • Symmetric encryption: Same key for encryption & decryption (e.g., AES).
  • Asymmetric encryption: Public key to encrypt, private key to decrypt (e.g., RSA).

πŸ‘‰ Code Example (AES Symmetric Encryption in Java):

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class EncryptionExample {
    public static void main(String[] args) throws Exception {
        String secretMessage = "Top Secret Data!";

        // Generate AES Key
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);
        SecretKey secretKey = keyGen.generateKey();

        // Encrypt
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(secretMessage.getBytes());
        System.out.println("Encrypted: " + new String(encryptedBytes));

        // Decrypt
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        System.out.println("Decrypted: " + new String(decryptedBytes));
    }
}
Enter fullscreen mode Exit fullscreen mode

Output Example:

Encrypted: ¹Ç¼... (random unreadable chars)
Decrypted: Top Secret Data!
Enter fullscreen mode Exit fullscreen mode

βœ”οΈ Key point: Encryption is reversible with a key, and it’s the foundation of secure communication.


πŸ“Š Quick Comparison Table

Feature Encoding Hashing Encryption
Purpose Data formatting Data integrity Data security
Reversible? βœ… Yes ❌ No βœ… Yes (with key)
Example Base64, URL SHA-256, MD5 AES, RSA
Security Level ❌ None ⚑ Integrity πŸ” Confidentiality

πŸ“ Conclusion

  • Use Encoding when you need safe transmission of data (but not security).
  • Use Hashing when you need to verify data integrity or store passwords.
  • Use Encryption when you need to protect sensitive data and allow controlled access.

πŸ‘‰ Together, these three techniques form the backbone of data security and handling strategies in modern systems.


Top comments (0)