由于接触一个新的项目,里面的配置文件使用了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);