|
@@ -14,72 +14,8 @@ import org.springframework.stereotype.Component;
|
|
|
* @create 2021/9/22
|
|
|
* @desc Jasypt安全框架加密类工具包
|
|
|
**/
|
|
|
-@Component
|
|
|
public class EncryptDecrypt {
|
|
|
|
|
|
- private static String password;
|
|
|
-
|
|
|
- @Value("${jasypt.encryptor.password}")
|
|
|
- public void setPassword(String password) {
|
|
|
- this.password = password;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * Jasypt生成加密结果
|
|
|
- *
|
|
|
- * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
|
|
|
- * @param value 待加密值
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String encryptPwd(String password, String value) {
|
|
|
- PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
|
|
|
- encryptOr.setConfig(cryptOr(password));
|
|
|
- String result = encryptOr.encrypt(value);
|
|
|
- return result;
|
|
|
- }
|
|
|
- public static String encrypt(String value) {
|
|
|
- PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
|
|
|
- encryptOr.setConfig(cryptOr(password));
|
|
|
- String result = encryptOr.encrypt(value);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 解密
|
|
|
- *
|
|
|
- * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
|
|
|
- * @param value 待解密密文
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String decyptPwd(String password, String value) {
|
|
|
- PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
|
|
|
- encryptOr.setConfig(cryptOr(password));
|
|
|
- String result = encryptOr.decrypt(value);
|
|
|
- return result;
|
|
|
- }
|
|
|
- public static String decrypt( String value) {
|
|
|
- PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
|
|
|
- encryptOr.setConfig(cryptOr(password));
|
|
|
- String result = encryptOr.decrypt(value);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @param password salt
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static SimpleStringPBEConfig cryptOr(String password) {
|
|
|
- SimpleStringPBEConfig config = new SimpleStringPBEConfig();
|
|
|
- config.setPassword(password);
|
|
|
- config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM);
|
|
|
- config.setKeyObtentionIterations("1000");
|
|
|
- config.setPoolSize("1");
|
|
|
- config.setProviderName(null);
|
|
|
- config.setSaltGeneratorClassName("org.jasypt.iv.NoIvGenerator");
|
|
|
- config.setStringOutputType("base64");
|
|
|
- return config;
|
|
|
- }
|
|
|
-
|
|
|
public static void main(String[] args) {
|
|
|
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
|
|
|
//配置文件中配置如下的算法
|
|
@@ -87,13 +23,17 @@ public class EncryptDecrypt {
|
|
|
//配置文件中配置的password
|
|
|
standardPBEStringEncryptor.setPassword("4dage168...");
|
|
|
//要加密的文本
|
|
|
- String name = standardPBEStringEncryptor.encrypt("root");
|
|
|
String password = standardPBEStringEncryptor.encrypt("JK20220120%JIK");
|
|
|
- String redisPassword = standardPBEStringEncryptor.encrypt("123456");
|
|
|
+ String name = standardPBEStringEncryptor.encrypt("LTAI5tJZBJwP5qazE6b3b4Gr");
|
|
|
+ String redisPassword = standardPBEStringEncryptor.encrypt("zSQCG0yyvRJISPokNZGhbhaAfh4hGX");
|
|
|
+ String redisPassword1 = standardPBEStringEncryptor.encrypt("admin");
|
|
|
+ String redisPassword2 = standardPBEStringEncryptor.encrypt("admin1231");
|
|
|
//将加密的文本写到配置文件中
|
|
|
System.out.println("name=" + name);
|
|
|
System.out.println("password=" + password);
|
|
|
System.out.println("redisPassword=" + redisPassword);
|
|
|
+ System.out.println("redisPassword=" + redisPassword1);
|
|
|
+ System.out.println("redisPassword=" + redisPassword2);
|
|
|
|
|
|
//要解密的文本
|
|
|
//String name2 = standardPBEStringEncryptor.decrypt("FarrmxSQX5uwtH/NZRxy+g==");
|
|
@@ -103,5 +43,6 @@ public class EncryptDecrypt {
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
}
|
|
|
|