|
@@ -0,0 +1,94 @@
|
|
|
+package com.fdkankan.manage_jp.util;
|
|
|
+
|
|
|
+
|
|
|
+import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
|
|
|
+import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
|
|
|
+import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author ys
|
|
|
+ * @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.salt.RandomSaltGenerator");
|
|
|
+ config.setStringOutputType("base64");
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ // 加密
|
|
|
+ System.out.println("ENC("+encryptPwd("4dage168...", "zSQCG0yyvRJISPokNZGhbhaAfh4hGX")+")");
|
|
|
+ // 解密
|
|
|
+// mysql@1234
|
|
|
+// System.out.println(decyptPwd("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7", "bgWQ4OfVCUJ1ExsqNhGV+KKBgpx8alv+"));
|
|
|
+
|
|
|
+// root@1234
|
|
|
+// System.out.println(decyptPwd("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7", "tdHzge8YvviOJaiV/+P6uQ9wgB44D1aH"));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|