Jasypt和Druid加解密函数 - sevck
2023-5-22 17:18:0 Author: www.cnblogs.com(查看原文) 阅读量:29 收藏

spring boot jasypt 加解密

jasypt 加密ENC() 需要启动参数秘钥

Druid 加密,需要public-key

package com.example.demo;


import com.alibaba.druid.filter.config.ConfigTools;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

class JasyptUtil {
    private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";
    private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";


    public String DruidEncyrpt(String publickey, String text) throws Exception {
        return ConfigTools.decrypt(publickey, text);
    }

    /**
     * @param text  待加密原文
     * @param crack 盐值(密钥)
     * @return 加密后的字符串
     * @Description: Jasypt加密(PBEWithMD5AndDES)
     */
    public static String encryptWithMD5(String text, String crack) {
        //1.创建加解密工具实例
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //2.加解密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm(PBEWITHMD5ANDDES);
        config.setPassword(crack);
        encryptor.setConfig(config);
        //3.加密
        return encryptor.encrypt(text);
    }

    /**
     * @param text  待解密原文
     * @param crack 盐值(密钥)
     * @return 解密后的字符串
     * @Description: Jasypt解密(PBEWithMD5AndDES)
     */
    public static String decryptWithMD5(String text, String crack) {
        //1.创建加解密工具实例
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //2.加解密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm(PBEWITHMD5ANDDES);
        config.setPassword(crack);
        encryptor.setConfig(config);
        //解密
        return encryptor.decrypt(text);
    }

    /**
     * @param text  待加密的原文
     * @param crack 盐值(密钥)
     * @return 加密后的字符串
     * @Description: jasypt 加密(PBEWITHHMACSHA512ANDAES_256)
     */
    public static String encryptWithSHA512(String text, String crack) {
        //1.创建加解密工具实例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        //2.加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(crack);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 为减少配置文件的书写,以下都是 Jasypt 3.x 版本,配置文件默认配置
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        //3.加密
        return encryptor.encrypt(text);
    }

    /**
     * @param text  待解密原文
     * @param crack 盐值(密钥)
     * @return 解密后的字符串
     * @Description: jasypt 解密(PBEWITHHMACSHA512ANDAES_256)
     */
    public static String decryptWithSHA512(String text, String crack) {
        //1.创建加解密工具实例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        //2.加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(crack);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 为减少配置文件的书写,以下都是 Jasypt 3.x 版本,配置文件默认配置
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        //3.解密
        return encryptor.decrypt(text);
    }

    public static void main(String[] args) {
        String text = "xxxxxxxxxxxxlvXSw=";
        String key = "jasypt key";  // -Djasypt.encryptor.password=
        String ret = JasyptUtil.decryptWithSHA512(text,key);
        System.out.println(ret);
  

    }

}

【版权所有@Sevck 博客地址http://www.cnblogs.com/sevck】 可以转载,注明出处.


文章来源: https://www.cnblogs.com/sevck/p/17421208.html
如有侵权请联系:admin#unsafe.sh