shrikant.sharma
shrikant.sharma

Reputation: 63

jasypt decryption password working in test but not in spring boot application

I am trying to learn jasypt 3.0.2. I have created a simple spring boot application

spring.datasource.username=root
spring.datasource.password=root`
spring.datasource.url=jdbc:mysql://localhost:3306/student_db?jdbcCompliantTruncation=false&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect

its working fine and connecting DB.

now I am trying to encrypt the password field with jasypt. I have created a sample main method to generate encrypted text and decrypt it again to validate.

private static StringEncryptor stringEncryptor() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword("password");
    config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setProviderName("SunJCE");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
    config.setStringOutputType("base64");
    encryptor.setConfig(config);
    return encryptor;
}
private static String encrypt(String text) {
    StringEncryptor textEncryptor = stringEncryptor();
    String encryptedText = textEncryptor.encrypt(text);
    return encryptedText;
}
private static String decrypt(String text) {
    StringEncryptor textEncryptor = stringEncryptor();
    String decryptedText = textEncryptor.decrypt(text);
    return decryptedText;
}
public static void main(String[] args) {
    System.out.println(encrypt("root"));
    System.out.println(decrypt("L5fd23Kr2q0tFRpxe+FdjTSrcF1jMD1iiriNvNQEfYHR6tODsx8E5ec/uX+evaMI"));
}`

for instance, right now it generated maAm5JPu/Mw/e+/uSGzlkKUX7Vk1vv1py+Nr4ihrNUjKfdXwBO1SJKcuxjx5/nZQ so my updated applicaton.properties file is

    spring.datasource.url=jdbc:mysql://localhost:3306/student_db?jdbcCompliantTruncation=false&sessionVariables=sql_mode='NO_ENGINE_SUBSTITUTION'&useSSL=false&useServerPrepStmts=false&rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
    spring.datasource.username=root
    spring.datasource.password=ENC(maAm5JPu/Mw/e+/uSGzlkKUX7Vk1vv1py+Nr4ihrNUjKfdXwBO1SJKcuxjx5/nZQ)
    jasypt.encryptor.algorithm=PBEWITHHMACSHA512ANDAES_256
    jasypt.encryptor.password=password
    jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator

main class is

@SpringBootApplication
@EnableEncryptableProperties
public class DemoApp {
    public static void main(String[] args) {
        SpringApplication.run(DemoApp.class, args);
    }
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}    
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'    
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}    
repositories {
    mavenCentral()
}    
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compile 'mysql:mysql-connector-java'
    compile 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.2'
}    
test {
    useJUnitPlatform()
}

but it's giving me an exception

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceConfig': Injection of autowired dependencies failed; nested exception is com.ulisesbocchio.jasyptspringboot.exception.DecryptionException:
Unable to decrypt: ENC(maAm5JPu/Mw/e+/uSGzlkKUX7Vk1vv1py+Nr4ihrNUjKfdXwBO1SJKcuxjx5/nZQ). Decryption of Properties failed,  make sure encryption/decryption passwords match at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]

what am I doing wrong?

Upvotes: 2

Views: 4175

Answers (1)

Tohid Makari
Tohid Makari

Reputation: 2494

Kindly add following config in your application.properties:

 jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator

Upvotes: 3

Related Questions