Basically, encryption is the process or conversion of user data in code or more specifically in the cyphertext form in order to prevent unauthorized access, So, encryption is very much important in today’s world where we all are working on large datasets stored in databases and the credentials of the database must be secured in order to secure the privacy and unauthorized access.
To encrypt our database credentials we will be using Jaspyt api. We can download the jaspyt library from here.
Java Simplified Encryption
Jasypt is a java library which allows the developer to add basic encryption capabilities to the projects with minimum effort, and without writing any code with the help of a few additions in your project here and there. Jasypt is highly configurable.
To encrypt database credentials we’ll be doing these tasks-
- Create a POJO class.
- Create a properties file.
- Create a Java class.
Step 1: Creating a POJO class
So, we have created a Plain java class named Details.java having the actual username and actual password and the keys for username and password having special and non-special characters. The code as follows-
Step 2: Create an empty Properties file
Step 3- Create a MainConnecton class named TestJDBC2.java having all the lines of codes required for encryption and decryption process. We have used javax.crypto.Cipher Class, java.security.MessageDigest Abstract Class, org.jasypt.util.text.BasicTextEncryptor FinalClass which will be going to perform the encryption and decryption process.
So. First, we will going to use the key defined in the details.java file for encryption and decryption process of both username and password and will be going to call encrypt and decrypt method of BasicTextEncryptor class.
Now let’s see the code-
As can be seen the process of encryption and decryption clearly in this code. After the execution of this code, the encrypted username and password can be seen in the Config.properties file.
As Salt Algorithm processing can be clearly seen in the console process.
Now, let’s have a look at the Config.properties file-
As the encrypted credentials can be seen clearly in the Config. Properties file and the original database credentials are in the details.java but the Connection properties are fetching the details from properties file in the encrypted form and decrypting the same to communicate with the database servers.
So now the database encryption is an easy task for all of us in Java.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to pre populate database in Android using SQLite Database
- Symmetric Encryption Cryptography in Java
- Asymmetric Encryption Cryptography in Java
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- How to add Image to MySql database using Servlet and JDBC
- Spring Boot | How to access database using Spring Data JPA
- How to Execute Multiple SQL Commands on a Database Simultaneously in JDBC?
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1
- Java.io.InputStream Class in Java
- Java.io.SequenceInputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 2
- Java.io.Console class in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.