EncryptConfig.java
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package org.nafmii.common.config;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.jasypt.exceptions.EncryptionOperationNotPossibleException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author by zhangbr
* @date 2020/7/25
*/
@Configuration
public class EncryptConfig {
private final static String ENCRYPT_PASSWORD = "P0O9I8U7Y6T5R4E3W2Q1";
private static StringEncryptor encrypt = initStringEncrypt();
@Bean("jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
return encrypt;
}
private static StringEncryptor initStringEncrypt(){
PooledPBEStringEncryptor encrypt = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(ENCRYPT_PASSWORD);
config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
encrypt.setConfig(config);
return encrypt;
}
/**
* 加密
* @param code 编码
* @return String
*/
public static String encrypt(String code){
return encrypt.encrypt(code);
}
/**
* 解密
* @param code 编码
* @return String
*/
public static String decrypt(String code) throws EncryptionOperationNotPossibleException {
return encrypt.decrypt(code);
}
}