Java - DES Encryption and Decryption example

Key Concepts

  1. Symmetric Encryption: DES (Data Encryption Standard) is a symmetric key encryption algorithm, meaning the same key is used for both encryption and decryption.
  2. DES Algorithm: DES uses a 56-bit key (plus 8 bits for parity) to encrypt blocks of data (64 bits each). It performs several complex transformations (16 rounds of encryption) to secure the data.
  3. Java Cryptography Architecture (JCA): The javax.crypto package provides the necessary tools for implementing encryption and decryption, including key generation, cipher operations, and transformations.

Here’s an example of using the DES (Data Encryption Standard) algorithm in Java for encryption and decryption. This example uses the javax.crypto package, which provides a straightforward way to implement cryptographic algorithms.

Code Example

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DESEncryptionDecryption {
    public static void main(String[] args) throws Exception {
        String textToEncrypt = "Hello, World!";
        System.out.println("Original Text: " + textToEncrypt);

        // Generate a DES key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56); // DES uses a 56-bit key size
        SecretKey secretKey = keyGenerator.generateKey();

        // Encrypt the text
        String encryptedText = encrypt(textToEncrypt, secretKey);
        System.out.println("Encrypted Text: " + encryptedText);

        // Decrypt the text
        String decryptedText = decrypt(encryptedText, secretKey);
        System.out.println("Decrypted Text: " + decryptedText);
    }

    public static String encrypt(String plainText, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedText, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        return new String(decryptedBytes);
    }
}

Explanation

  1. Key Generation:

    • The KeyGenerator is used to generate a DES key of 56 bits.
  2. Encryption:

    • A Cipher instance is created for the "DES" transformation.
    • The cipher is initialized in encryption mode with the generated key.
    • The input text is encrypted and encoded in Base64 for readability.
  3. Decryption:

    • The cipher is initialized in decryption mode with the same key.
    • The Base64-encoded encrypted text is decoded and decrypted back to the original text.

Sample Output

Original Text: Hello, World!
Encrypted Text: kf3d8LqYWx0=
Decrypted Text: Hello, World!

Notes

  1. Security: DES is considered insecure by modern standards due to its short key length. Use AES (Advanced Encryption Standard) for better security.
  2. Dependencies: This example uses built-in Java libraries, so no additional dependencies are required.

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete