zxl
2025-06-11 861582390dc7c395897159077a547d3e7078727c
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
58
59
60
61
62
package cn.lili.common.utils;
 
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
 
 
/**
 * 加密解密
 * @author Chopper
 */
public class JasyptUtil {
 
    /**
     * Jasypt生成加密结果
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value 待加密值
     * @return 加密字符串
     */
    public static String encryptPwd(String password, String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(encryptor(password));
        return encryptor.encrypt(value);
    }
    /**
     * 解密
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value 待解密密文
     * @return 揭秘字符串
     */
    public static String decryptPwd(String password, String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(encryptor(password));
        encryptor.decrypt(value);
        return encryptor.decrypt(value);
    }
 
    /**
     * 加密处理
     * @param password 密码
     * @return 加密字符串
     */
    public static SimpleStringPBEConfig encryptor(String password){
        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");
        return config;
    }
 
    public static void main(String[] args){
 
        //加密 若修改了第一个参数加密password记得在配置文件同步修改
        System.out.println(encryptPwd("jasypt.encryptor.password","123456"));
        //解密
        System.out.println(decryptPwd("jasypt.encryptor.password","PYVnAYh+j5C3jkMV1d+myj6JzDaUk7pcfTWUaYsvQdEVkuvIVf7Y0mOU9XkffxT8"));
    }
}