JDK8安裝JCE無限強度

原文:https://www.jianshu.com/p/de81059a9e97

https://blog.csdn.net/arctan90/article/details/68066660

報錯提示:

Exception in thread "main" org.jasypt.exceptions.EncryptionOperationNotPossibleException:

下載jar:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

將 local_policy.jar和US_export_policy.jar替換Java\jdk1.8.0_77\jre\lib\security\路徑下的文件

測試:測試代碼來源https://github.com/Exrick/xboot

package cn.exrick.xboot.core.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;


/**
 * @author Exrickx
 */
@Slf4j
public class JasyptUtil {

    /**
     * Jasypt生成加密結果
     * @param password 配置文件中設定的加密密碼 jasypt.encryptor.password
     * @param value 待加密值
     * @return
     */
    public static String encyptPwd(String password,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));
        encryptor.decrypt(value);
        String result = encryptor.decrypt(value);
        return result;
    }

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

    public static void main(String[] args){

        //加密 若修改了第一個參數加密password記得在配置文件同步修改
        System.out.println(encyptPwd("xboot","123456"));
        //解密
        System.out.println(decyptPwd("xboot",
"ZUNmpH3qrSfC8MGvcT0dEBuJvTl9v9XOMc2cmNj1KH8sQfPmr081eJJlS3ksrpn8"));
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章