Cipher class in Java JCE

Java Cryptography Extension (JCE) is a set of APIs that provides cryptographic services such as encryption, decryption, hashing, and secure key generation in Java applications. The Cipher class is a fundamental part of JCE that facilitates symmetric encryption and decryption operations.

Overview of the Cipher class

The Cipher class is part of the javax.crypto package and acts as a cryptographic service provider that enables encryption and decryption of data. It supports a wide range of cryptographic algorithms, including the popular AES (Advanced Encryption Standard), DES (Data Encryption Standard), and RSA (Rivest-Shamir-Adleman).

To use the Cipher class, you need to follow these general steps:

  1. Create a Cipher object by calling a static getInstance method with the desired transformation and optional provider arguments.
  2. Initialize the cipher, providing the operation mode (encryption or decryption), the encryption key, and other necessary parameters.
  3. Process the data by calling update or doFinal methods of the Cipher class.

Here’s an example demonstrating how to use the Cipher class for encryption and decryption using AES algorithm:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESCipherExample {

    public static void main(String[] args) throws Exception {
        String plainText = "This is a secret message.";
        String encryptionKey = "ThisIsEncryptionKey";

        byte[] cipherText = encrypt(plainText, encryptionKey);
        System.out.println("Encrypted Text: " + Base64.getEncoder().encodeToString(cipherText));

        String decryptedText = decrypt(cipherText, encryptionKey);
        System.out.println("Decrypted Text: " + decryptedText);
    }

    public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        return cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
    }

    public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec keySpec = new SecretKeySpec(encryptionKey.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        byte[] decryptedBytes = cipher.doFinal(cipherText);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}

In this example, we create a Cipher object using the AES algorithm with ECB (Electronic Codebook) mode and PKCS5 padding. We generate a secret key using a specified encryption key and the AES algorithm. The encrypt method takes the plaintext and encryption key and returns the encrypted ciphertext. The decrypt method takes the cipher text and encryption key and returns the decrypted plaintext.

By using the Cipher class provided by the Java Cryptography Extension, you can easily implement encryption and decryption functionality in your Java applications. Make sure to handle encryption keys securely and follow best practices when working with cryptography.

#Java #JCE