长亭百川云 - 文章详情

Jasypt和Druid加解密函数 - sevck

博客园 - sevck

49

2024-07-19

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);
  
    }

}
相关推荐
关注或联系我们
添加百川云公众号,移动管理云安全产品
咨询热线:
4000-327-707
百川公众号
百川公众号
百川云客服
百川云客服

Copyright ©2024 北京长亭科技有限公司
icon
京ICP备 2024055124号-2