iCode
iCode

Reputation: 9202

Password Encryption and Decryption with Jasypt and random salt

Hi I have a Java application. I want to use Jasypt to encrypt and decrypt the passwords with random salt generated based on size and algorithm.

This is what I want to achieve.

  1. Create a random salt.
  2. Encrypt the password with the salt.
  3. Save the salt and encrypted password for the user.

I want the salt size, algorithm to be given as input.

The reason why I want decryption is that, I have some configuration files created for the application and some values are passwords that I want to save as encrypted in the file and decrypt it when I want to use it.

I have this Java class which creates salt and then creates the hash code and I could use it for validating user (I can save salt and hascode instead of encrypted password.). I changed it some and created a method to create random salt in that example. But there is no decryption method.

That's why I choose Jasypt. But I havn't seen any proper example of how to use it.

I tried following and always it returns same salt.

public static void main(String[] args) {

    RandomSaltGenerator saltGenerator = new RandomSaltGenerator();
    byte[] salt = saltGenerator.generateSalt(24);
    System.out.println(salt);
}

Can anybody provide a proper example or how to use it in my own way? I want to achieve what Jasypt have mentioned in their article. But there are no codes available.

Upvotes: 2

Views: 13615

Answers (1)

Cristian Greco
Cristian Greco

Reputation: 2596

This article about password encryption with Jasypt describes standard best practices of storing encrypted user passwords with one-way encryption. Once you've stored a password using such techniques, there is now way to decrypt it.

If you need to encrypt and decrypt passwords for application configuration, you should consider using the StandardPBEStringEncryptor provided by Jasypt (or whatever provider best fits your data type). This page contains good explanation and example code.

Even better, Jasypt provides first class support for encrypted application configuration using .properties files (also with good support for Spring).

Upvotes: 3

Related Questions