關於jdk8使用jasypt 256加密的問題


由於接觸一個新的項目,里面的配置文件使用了jasypt加密,項目本身是jdk11,估計創建項目的人本地安裝的也是jdk11,而我本地只安裝了jdk8,這是前提。

我想把線上的數據庫改成本地,這個試試涉及到username加密,password本身使用了druid加密,由於之前並未接觸,也未有人告知使用方法,所以自己就摸索了一下,根據度娘說了,執行了以下加密

java PS E:\repository\health\repository\org\jasypt\jasypt\1.9.3> java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=allanpassword algorithm=PBEWITHHMACSHA512ANDAES_256

----ENVIRONMENT-----------------

Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.281-b09



----ARGUMENTS-------------------

algorithm: PBEWITHHMACSHA512ANDAES_256
input: 123456
password: allanpassword



----ERROR-----------------------

Operation not possible (Bad input or parameters)

一臉懵逼,百度也說不出啥來,后面找了許久,發現是默認JDK8的AES最大支持128bit的密鑰,如果使用256bit的密鑰,會拋出一個異常

需要下載“Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for JDK/JRE 8”,替換JDK/JRE里的2個jar包。

下載地址:

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

下載zip包,將里面的local_policy.jar和US_export_policy.jar解壓到\jre\lib\security下覆蓋原文件即可。(參照https://www.cnblogs.com/merray/p/9437797.html)

但是還是不行

Exception in thread "main" org.jasypt.exceptions.EncryptionOperationNotPossibleException
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.encrypt(StandardPBEByteEncryptor.java:1001)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:655)
    at model.Mencryption.main(Mencryption.java:23)

執行源代碼才發現實際是報異常

java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long

好像是說 錯誤的IV長度:必須是16字節長,具體解決方案是

        //加密 
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        standardPBEStringEncryptor.setIvGenerator(new RandomIvGenerator()); // 默認為空,如果是256的加密方式,必須配置
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("777");// 這個如果加密有配置 那么解密也要配置,默認是1000 具體有什么用也不清楚
        config.setPassword("wqblb!@3456");                        // 加密的密鑰
        standardPBEStringEncryptor.setConfig(config);
        String plainText = "root";
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println(encryptedText);


         // 解密
        String ss="JJ5KWxPiuM0WmvhauXPaO/SAKyGuTRuije21QUKAIYSsajLMHwJG7ox3dWwU/fhk";
        String encryptedTex1t = standardPBEStringEncryptor.decrypt(ss);
        System.out.println(encryptedTex1t);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM