2017-03-06 18:04:28 +01:00
|
|
|
package com.baeldung.jasypt;
|
2017-02-27 19:06:34 +01:00
|
|
|
|
|
|
|
|
|
2017-02-27 19:29:20 +01:00
|
|
|
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
|
|
|
|
|
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
|
2017-03-02 10:53:24 +01:00
|
|
|
import org.jasypt.util.password.BasicPasswordEncryptor;
|
2017-02-27 19:06:34 +01:00
|
|
|
import org.jasypt.util.text.BasicTextEncryptor;
|
2017-02-27 19:29:20 +01:00
|
|
|
import org.junit.Ignore;
|
2017-02-27 19:06:34 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
2017-03-06 18:04:28 +01:00
|
|
|
import static junit.framework.Assert.*;
|
2017-02-27 19:06:34 +01:00
|
|
|
import static junit.framework.TestCase.assertEquals;
|
|
|
|
|
|
|
|
|
|
public class JasyptTest {
|
|
|
|
|
|
|
|
|
|
@Test
|
2017-03-06 18:04:28 +01:00
|
|
|
public void givenTextPrivateData_whenDecrypt_thenCompareToEncrypted() {
|
2017-02-27 19:06:34 +01:00
|
|
|
//given
|
|
|
|
|
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
|
2017-03-06 18:04:28 +01:00
|
|
|
String privateData = "secret-data";
|
|
|
|
|
textEncryptor.setPasswordCharArray("some-random-data".toCharArray());
|
2017-02-27 19:06:34 +01:00
|
|
|
|
|
|
|
|
//when
|
2017-03-06 18:04:28 +01:00
|
|
|
String myEncryptedText = textEncryptor.encrypt(privateData);
|
|
|
|
|
assertNotSame(privateData, myEncryptedText); //myEncryptedText can be save in db
|
2017-02-27 19:06:34 +01:00
|
|
|
|
|
|
|
|
//then
|
|
|
|
|
String plainText = textEncryptor.decrypt(myEncryptedText);
|
2017-03-06 18:04:28 +01:00
|
|
|
assertEquals(plainText, privateData);
|
2017-02-27 19:29:20 +01:00
|
|
|
}
|
|
|
|
|
|
2017-03-02 10:53:24 +01:00
|
|
|
@Test
|
|
|
|
|
public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldBeSame(){
|
|
|
|
|
String password = "secret-pass";
|
|
|
|
|
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
|
|
|
|
|
String encryptedPassword = passwordEncryptor.encryptPassword(password);
|
|
|
|
|
|
|
|
|
|
//when
|
|
|
|
|
boolean result = passwordEncryptor.checkPassword("secret-pass", encryptedPassword);
|
|
|
|
|
|
|
|
|
|
//then
|
|
|
|
|
assertTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldNotBeSame(){
|
|
|
|
|
String password = "secret-pass";
|
|
|
|
|
BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
|
|
|
|
|
String encryptedPassword = passwordEncryptor.encryptPassword(password);
|
|
|
|
|
|
|
|
|
|
//when
|
|
|
|
|
boolean result = passwordEncryptor.checkPassword("secret-pass-not-same", encryptedPassword);
|
|
|
|
|
|
|
|
|
|
//then
|
|
|
|
|
assertFalse(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-02-27 19:29:20 +01:00
|
|
|
|
|
|
|
|
@Test
|
2017-02-27 19:31:19 +01:00
|
|
|
@Ignore("should have installed local_policy.jar")
|
2017-03-06 18:04:28 +01:00
|
|
|
public void givenTextPrivateData_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() {
|
2017-02-27 19:29:20 +01:00
|
|
|
//given
|
|
|
|
|
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
|
2017-03-06 18:04:28 +01:00
|
|
|
String privateData = "secret-data";
|
|
|
|
|
encryptor.setPassword("some-random-data");
|
2017-02-27 19:29:20 +01:00
|
|
|
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
|
|
|
|
|
|
|
|
|
|
//when
|
|
|
|
|
String encryptedText = encryptor.encrypt("secret-pass");
|
2017-03-06 18:04:28 +01:00
|
|
|
assertNotSame(privateData, encryptedText);
|
2017-02-27 19:29:20 +01:00
|
|
|
|
|
|
|
|
//then
|
|
|
|
|
String plainText = encryptor.decrypt(encryptedText);
|
2017-03-06 18:04:28 +01:00
|
|
|
assertEquals(plainText, privateData);
|
2017-02-27 19:29:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2017-02-27 19:31:19 +01:00
|
|
|
@Ignore("should have installed local_policy.jar")
|
2017-03-06 18:04:28 +01:00
|
|
|
public void givenTextPrivateData_whenDecryptOnHighPerformance_thenDecrypt(){
|
2017-02-27 19:29:20 +01:00
|
|
|
//given
|
2017-03-06 18:04:28 +01:00
|
|
|
String privateData = "secret-data";
|
2017-02-27 19:29:20 +01:00
|
|
|
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
|
|
|
|
|
encryptor.setPoolSize(4);
|
2017-03-06 18:04:28 +01:00
|
|
|
encryptor.setPassword("some-random-data");
|
2017-02-27 19:29:20 +01:00
|
|
|
encryptor.setAlgorithm("PBEWithMD5AndTripleDES");
|
|
|
|
|
|
|
|
|
|
//when
|
2017-03-06 18:04:28 +01:00
|
|
|
String encryptedText = encryptor.encrypt(privateData);
|
|
|
|
|
assertNotSame(privateData, encryptedText);
|
2017-02-27 19:29:20 +01:00
|
|
|
|
|
|
|
|
//then
|
|
|
|
|
String plainText = encryptor.decrypt(encryptedText);
|
2017-03-06 18:04:28 +01:00
|
|
|
assertEquals(plainText, privateData);
|
2017-02-27 19:06:34 +01:00
|
|
|
}
|
|
|
|
|
}
|