具有自定义前缀后缀的Jasypt 3DES

时间:2018-07-09 07:36:06

标签: spring-boot 3des jasypt

我正在尝试使用3DES实现自定义Jasypt,但字符串未解密。

@Configuration
@EnableEncryptableProperties
@PropertySource(name = "EncryptedProperties", value = "classpath:test-encrypted.properties")
public class TestEncryptionConfiguration {

    @Bean(name = "encryptablePropertyResolver")
    EncryptablePropertyResolver encryptablePropertyResolver(Environment environment) {
        return new MyEncryptablePropertyResolver(environment);
    }

    class MyEncryptablePropertyResolver implements EncryptablePropertyResolver {

        private final PooledPBEStringEncryptor encryptor;

        public MyEncryptablePropertyResolver(Environment environment) {
            char[] password = "password".toCharArray();
            this.encryptor = new PooledPBEStringEncryptor();
            SimpleStringPBEConfig config = new SimpleStringPBEConfig();
            config.setPasswordCharArray(password);
            config.setAlgorithm("PBEWithMD5AndTripleDES");
            config.setKeyObtentionIterations("1000");
            config.setPoolSize(1);
            config.setProviderName("BC");
            config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
            config.setStringOutputType("base64");
            encryptor.setConfig(config);
        }

        @Override
        public String resolvePropertyValue(String value) {
            if (value != null && value.startsWith("DES@{")) {
                return encryptor.decrypt(value.substring("DES@{".length()));
            }
            return value;
        }
    }

}

在我放入的test-encrypted.properties中

secret.property=DES@{U2FsdGVkX1/9gH2/vOs7O9MkLRdQS/qg}
jasypt.encryptor.property.prefix="DES@{"
jasypt.encryptor.property.suffix="}"
jasypt.encryptor.property.resolver-bean=encryptablePropertyResolver

在加载应用程序上下文之前,我正在引导时加载此配置。

将此文件设置为spring.factories中的云引导加载程序。

0 个答案:

没有答案