Java Cipher doFinal()

In this guide, you will learn about the Cipher doFinal() method in Java programming and how to use it with an example.

1. Cipher doFinal() Method Overview


The doFinal() method of the Cipher class in Java is used to perform the final encryption or decryption operation, depending on how the Cipher object has been initialized. It takes as input a byte array, processes it, and returns a new byte array containing the result.


public byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException


- input: a byte array containing the input data to be processed.

Key Points:

- The doFinal() method can throw an IllegalBlockSizeException if the input data's length is incorrect.

- It can also throw a BadPaddingException if the padding of the input data is incorrect.

- Before calling doFinal(), the Cipher object must be properly initialized for encryption or decryption.

2. Cipher doFinal() Method Example

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

public class DoFinalExample {

    public static void main(String[] args) {
        try {
            // Define the transformation
            String transformation = "AES/ECB/PKCS5Padding";

            // Create a key
            SecretKeySpec key = new SecretKeySpec("abcdefghijklmnop".getBytes(), "AES");

            // Get Cipher instance and initialize it for encryption
            Cipher cipher = Cipher.getInstance(transformation);
            cipher.init(Cipher.ENCRYPT_MODE, key);

            // Encrypt the data
            byte[] encryptedData = cipher.doFinal("Hello, World!".getBytes());

            // Encode the encrypted data to Base64
            String encodedData = Base64.getEncoder().encodeToString(encryptedData);

            // Print the encrypted data
            System.out.println("Encrypted Data: " + encodedData);

            // Initialize the Cipher for decryption
            cipher.init(Cipher.DECRYPT_MODE, key);

            // Decode the Base64 encoded data and decrypt it
            byte[] decodedData = Base64.getDecoder().decode(encodedData);
            byte[] decryptedData = cipher.doFinal(decodedData);

            // Convert the decrypted byte array to a String
            String originalString = new String(decryptedData);

            // Print the original string
            System.out.println("Original String: " + originalString);

        } catch (Exception e) {


Encrypted Data: zQY9zjTZl9yvmDf+KluNUQ==
Original String: Hello, World!


In this example, we used the Cipher.getInstance() method to create a Cipher instance for the "AES/ECB/PKCS5Padding" transformation. 

We initialized this instance in encryption mode and used doFinal() to encrypt the input string "Hello, World!". The encrypted data was then Base64 encoded and printed. 

Subsequently, we reinitialized the Cipher instance in decryption mode and used doFinal() again to decrypt the encrypted data back to the original string, which was then printed, showcasing the symmetry and inverse nature of the encryption and decryption processes.