Java 01 - Jasypt: Protecting sensitive data with encryption.
Tags: java, encryp, programming, tutorial
Published: 2023-06-04 16:57:25

Data security is a critical aspect of any application or system, especially when it comes to sensitive information such as passwords, credit card numbers, or personal data. Encryption is a widely used technique to protect this sensitive data, making it unintelligible to anyone without the correct key. One of the popular libraries for encryption in Java is Jasypt.

## **What is Jasypt?**

Jasypt is a Java encryption library that provides comprehensive features for protecting sensitive data in applications. It is easy to use and supports various cryptographic algorithms, including AES (Advanced Encryption Standard), DES (Data Encryption Standard), RSA (Rivest-Shamir-Adleman), and others.

Developed by Daniel Fernández, the Jasypt library was created to simplify the encryption process in Java and provide an additional layer of security to applications.

## How to use Jasypt?

**Implementation in your Project's dependencies**

- [Jasypt ](http://www.jasypt.org/index.html) website where you can find all the information
First you have to add in your pom.xml the jasypt dependency as you can see below.

 ```
  <dependencies>
	  <dependency>
		  <groupId>org.jasypt</groupId>
		  <artifactId>jasypt</artifactId>
		  <version>1.9.3</version>
	  </dependency>
  </dependencies>
```

#### Using simple encryption

Let's use the example that we want to encrypt a password.

01) The first step is to import the BasicTextEncryptor class from the Jasypt library

```

import org.jasypt.util.text.BasicTextEncryptor;
```

02) Keep in mind that we create the password with some program, to make it simpler I will create a string as an example.

```
String password = "my_password";
```

03) Create a new instance of the BasicTextEncryptor class.

```
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
```

04) The next step is to create an encryption key for the textEncryptor object using the setPassword method.

```
textEncryptor.setPassword("my_encryption_key");

```
05) Next we use the encrypt method to encrypt this password.

```
String encryptedPassword = textEncryptor.encrypt(password);
```


That's it! Now we have an encrypted password, remembering this is the simplest way to use this library.

06)You can save this encrypted password in a file or just show it in the console to see if it's working.


```
  System.out.println("Encrypted password: " + encryptedPassword);
```


07) To decrypt previously encrypted data, you need to use the same encryption key or algorithm you used in the encryption step, and then instead of using the encypt method we use decrypt.

Example of a simple ready-made program.

```
import org.jasypt.util.text.BasicTextEncryptor;
public class EncryptionExample {

    public static void main(String[] args) {
        String password = "my_password";
        String encryptionkey = "my_key";

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        textEncryptor.setPassword(encryptionkey);

        String encryptedText = textEncryptor.encrypt(password);
        System.out.println("Encrypted text: " + encryptedText);

        String decryptedText = textEncryptor.decrypt(encryptedText);
        System.out.println("Decrypted text: " + decryptedText);
    }
}
```

**For those who want to dig deeper.**

I will leave a project from my github where I created an application that generates passwords based on the desired conditions and saves them in an encrypted
in an encrypted notepad. It then allows you to decrypt the passwords and create a new file
file, which will be deleted in 30 seconds, containing all passwords.

https://github.com/DaviJonck/Password-Generator-and-Encryptor