73 lines
2.7 KiB
Java
73 lines
2.7 KiB
Java
|
|
package com.zhgd.xmgl.util;
|
||
|
|
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
|
||
|
|
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @program: wisdomSite
|
||
|
|
* @description: 加密
|
||
|
|
* @author: Mr.Peng
|
||
|
|
* @create: 2021-04-23 14:51
|
||
|
|
**/
|
||
|
|
@Slf4j
|
||
|
|
public class JasyptUtils {
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 加密方法
|
||
|
|
*
|
||
|
|
* @param plainText 需加密文本
|
||
|
|
*/
|
||
|
|
public static void testEncrypt(String plainText) {
|
||
|
|
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
|
||
|
|
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
|
||
|
|
// 加密的算法,这个算法是默认的
|
||
|
|
config.setAlgorithm("PBEWithMD5AndDES");
|
||
|
|
//加密的密钥,自定义
|
||
|
|
config.setPassword("CSEbfYkitulv73I2p0mXI50JMXoaxZTKJ7");
|
||
|
|
standardPBEStringEncryptor.setConfig(config);
|
||
|
|
String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
|
||
|
|
log.info(encryptedText);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 解密方法
|
||
|
|
*
|
||
|
|
* @param encryptedText 需解密文本
|
||
|
|
*/
|
||
|
|
public static void testDecrypt(String encryptedText) {
|
||
|
|
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
|
||
|
|
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
|
||
|
|
// 解密的算法,需同加密算法相同
|
||
|
|
config.setAlgorithm("PBEWithMD5AndDES");
|
||
|
|
//解密的密钥,需同加密密钥相同
|
||
|
|
config.setPassword("CSEbfYkitulv73I2p0mXI50JMXoaxZTKJ7");
|
||
|
|
standardPBEStringEncryptor.setConfig(config);
|
||
|
|
String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
|
||
|
|
log.info(plainText);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void main(String[] arg) {
|
||
|
|
|
||
|
|
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
|
||
|
|
/*配置文件中配置如下的算法*/
|
||
|
|
standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
|
||
|
|
/*配置文件中配置的password*/
|
||
|
|
standardPBEStringEncryptor.setPassword("JXJZHGDPT");
|
||
|
|
/*要加密的文本*/
|
||
|
|
String name = standardPBEStringEncryptor.encrypt("root");
|
||
|
|
String password = standardPBEStringEncryptor.encrypt("JXJ@admin");
|
||
|
|
/*将加密的文本写到配置文件中*/
|
||
|
|
log.info("name=" + name);
|
||
|
|
log.info("password=" + password);
|
||
|
|
/*解密的文本*/
|
||
|
|
String value = standardPBEStringEncryptor.decrypt("hHkiHEc6vSWjqfOtg2/2Uiihs0vX3l7V");
|
||
|
|
//String password =standardPBEStringEncryptor.encrypt("ee49c892ad3b");
|
||
|
|
/*将加密的文本写到配置文件中*/
|
||
|
|
log.info("value=" + value);
|
||
|
|
//log.info("password="+password);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|