springboot集成jasypt加密

  • springboot配置

jasypt:
  encryptor:
    password: 盐值

  • Jasypt工具 

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtil {

		// 加密
	   public static String encyptPwd(String password,String value) {
	        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
	        encryptor.setConfig(cryptor(password));
	        String encrypt = encryptor.encrypt(value);
	        return encrypt;
	    }
	   	// 解密
	    public static String decyptPwd(String password,String value) {
	        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
	        encryptor.setConfig(cryptor(password));
	        String decrypt = encryptor.decrypt(value);
	        return decrypt;
	    }

	    private static SimpleStringPBEConfig cryptor(String password) {
	        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
	        config.setPassword(password);
	        config.setAlgorithm("PBEWithMD5AndDES");
	        config.setKeyObtentionIterations("1000");
	        config.setPoolSize("1");
	        config.setProviderName("SunJCE");
	        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
	        config.setStringOutputType("base64");
	        return config;
	    }

	    public static void main(String[] args) {
	    	//加密: xxx为盐值,secritery为密码
	       System.out.println(encyptPwd("xxx","secritery"));
	       //解密: xxx为盐值,使用加密后的密码: ENC(EVVBR9iTfxRUtsZZW7FbPPUoBhqd0Zr4)
	       System.out.println(decyptPwd("xxx","EVVBR9iTfxRUtsZZW7FbPPUoBhqd0Zr4"));
	    }
}


 

 

你可能感兴趣的:(springboot集成jasypt加密)