一、前言
随着当前安全局势愈发严峻,各行业安全要求越来越高,本文重点介绍SpringBoot结合jasypt进行数据库等配置文件进行加解密的方法。
二、基础使用
2.1 引入依赖
1 2 3 4 5
| <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.5</version> </dependency>
|
2.2 增加配置
1 2 3 4 5 6 7 8
| jasypt: encryptor: algorithm: PBEWithMD5AndDES #指定解密算法 password: wno704 #加密的密钥,自定义即可,必填项. iv-generator-classname: org.jasypt.iv.NoIvGenerator property: # 自定义的前后缀标记,格式为ENC(加密结果),此处我们修改为 wno704-密文-end prefix: wno704- suffix: -end
|
2.3 获取加密密文
在本地maven仓库中找到 org/jasypt/jasypt/1.9.3 目录(根据版本不同,目录不同),然后执行下面命令:
加密
1
| java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=${password} algorithm=${algorithm} input=${input}
|
解密
1
| java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI password=${password} algorithm=${algorithm} input=${input}
|
password: 加密的salt,解密时还需要它,项目自定义
algorithm: 加密的类型,默认 PBEWithMD5AndDES
input: 待加密的明文
执行结果中 OUTPUT 下方的内容时明文对应的密文或者明文,如果是加密过程多次执行同一个命令 OUTPUT 输出的内容不同,但不影响。
实际执行如下:
![]()
三、进阶使用
加解密工具JasyptUtil.java
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| package com.wno704.system.util;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
/** * @CalssName JasyptUtil * @Description TODO * @Author wno704 * @Date 2024/10/14 下午3:22 * @Version 1.0 */ public class JasyptUtil { /** * 加密 * * @param plaintext 明文 * @return */ public static String encrypt(String plaintext) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig(); // 指定算法 config.setAlgorithm("PBEWithMD5AndDES"); // 指定秘钥,和yml配置文件中保持一致 config.setPassword("wno704"); encryptor.setConfig(config); // 生成加密数据 return encryptor.encrypt(plaintext); }
/* * @Description: 加密 * @Param: [algorithm, password, plaintext] * @return: java.lang.String * @Author: wno704 * @Date: 2024/10/14 */ public static String encrypt(String algorithm,String password,String plaintext) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig(); // 指定算法 config.setAlgorithm(algorithm); // 指定秘钥,和yml配置文件中保持一致 config.setPassword(password); encryptor.setConfig(config); // 生成加密数据 return encryptor.encrypt(plaintext); }
/** * 解密 * * @param data 加密后数据 * @return */ public static String decrypt(String data) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig(); config.setAlgorithm("PBEWithMD5AndDES"); config.setPassword("wno704"); encryptor.setConfig(config); // 解密数据 return encryptor.decrypt(data); }
/** * 解密 * * @param data 加密后数据 * @return */ public static String decrypt(String algorithm,String password,String data) { StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig(); config.setAlgorithm(algorithm); config.setPassword(password); encryptor.setConfig(config); // 解密数据 return encryptor.decrypt(data); }
/* * @Description: 字符串前后缀取消 * @Param: [prefix, suffix, str] * @return: java.lang.String * @Author: wno704 * @Date: 2024/10/14 */ public static String dealStrFix(String prefix, String suffix, String str){ String reStr = str; if (str.indexOf(prefix)==0){ reStr = str.replaceFirst(prefix, ""); } if (reStr.lastIndexOf(suffix) == reStr.length() - suffix.length()) { reStr = reStr.substring(0, reStr.length() - suffix.length()); } return reStr; }
/* * @Description: 是否符合前后缀要求 * @Param: [prefix, suffix, str] * @return: java.lang.String * @Author: wno704 * @Date: 2024/10/14 */ public static boolean isContainFix(String prefix, String suffix, String str){ String reStr = str; if (str.indexOf(prefix)==0){ reStr = str.replaceFirst(prefix, ""); } if (reStr.lastIndexOf(suffix) == reStr.length() - suffix.length()) { reStr = reStr.substring(0, reStr.length() - suffix.length()); } return reStr.length()+prefix.length()+suffix.length() == str.length(); } }
|
工具使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Test public void testJasypt() throws Exception { String oldStr = "19920503"; String enStr = JasyptUtil.encrypt(oldStr); System.out.println(enStr); System.out.println(JasyptUtil.decrypt(enStr));
enStr = JasyptUtil.encrypt("PBEWithMD5AndDES","wno704",oldStr); System.out.println(enStr); System.out.println(JasyptUtil.decrypt("PBEWithMD5AndDES","wno704", enStr));
String deStr = "wno7041-2hudACDDwno704-v5Wh-endFSZkTp5rFw==-end"; String prefix = "wno704-"; String suffix = "-end";
System.out.println(JasyptUtil.isContainFix(prefix,suffix,deStr)); System.out.println(JasyptUtil.dealStrFix(prefix,suffix,deStr));
System.out.println(JasyptUtil.decrypt("PBEWithMD5AndDES","wno704", "jWKJXVnYWwB51ftdWVKYTx2IjqQeuWMy")); }
|