🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
Definition:
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.
Syntax:
public byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
Parameters:
- 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) {
e.printStackTrace();
}
}
}
Output:
Encrypted Data: zQY9zjTZl9yvmDf+KluNUQ== Original String: Hello, World!
Explanation:
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment