Why are two texts which are equal generate different encrypted text

336 views Asked by At

This class :

import org.jasypt.util.text.BasicTextEncryptor;

    public class TestSame {

        public static void main(String args[]){

            BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
            textEncryptor.setPassword("test");

            System.out.println(textEncryptor.encrypt("test"));
            System.out.println(textEncryptor.encrypt("test"));

            System.out.println(textEncryptor.decrypt("Nv4nMcuVwsvWVuYD7Av44Q=="));
            System.out.println(textEncryptor.decrypt("bjU82X18p9gAivwomA+NpQ=="));

        }

    }

Generates this output :

n3G0M4YH8QjPU+YMYsfTmw==
ftokGN7dMKyLtKBaim2RTQ==
test
test

Why are the encrypted texts different? The decrypted texts are the same so how is able to decrypt the password since each encrypted text is different ?

I would expect that the same text using the same password and same encryption would generate the same encrypted text, but this does not seem to be the case ?

1

There are 1 answers

0
chrylis -cautiouslyoptimistic- On BEST ANSWER

From the documentation:

If a random salt generator is used, two encryption results for the same message will always be different (except in the case of random salt coincidence). This may enforce security by difficulting brute force attacks on sets of data at a time and forcing attackers to perform a brute force attack on each separate piece of encrypted data.