DES对称加解密

从服务端取回来一串加密串:加密方式DES(PHP加密的)。

对称加解密,使用Hutool解密出现错误如下:

解密方法:

SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DES, key.getBytes());

byte\[\] encrypt = content.getBytes();

byte\[\] decrypt = des.decrypt(encrypt);

String decryptStr = des.decryptStr(decrypt);

错误信息1:

llegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

解密方法:

/**

* 解密

* @param encryptedText 加密的密文

* @param secretKey 秘钥

* @return

* @throws Exception

*/

public static String decrypt(String encryptedText, String secretKey) throws Exception {

SecretKey key = generateKey(secretKey);

Cipher cipher = Cipher.getInstance(DES_KEY);

cipher.init(Cipher.DECRYPT_MODE, key);

byte\[\] encryptedBytes = Base64.getDecoder().decode(encryptedText);//进行Base64解码

//byte\[\] encryptedBytes = Hex.decodeHex(encryptedText);//进行十六进制解码

byte\[\] decryptedBytes = cipher.doFinal(encryptedBytes);

return new String(decryptedBytes, StandardCharsets.UTF_8);

}

错误信息2:Wrong key size

换了好几种解密方式都不能正确解密,但是此加密串在站长工具上又是可以正常解密的,折腾大半天,请教了同事,给出可解的实现:

byte\[\] ivKey=new byte8;
DES desa = new DES(Mode.CBC, Padding.PKCS5Padding,key.getBytes( "UTF-8"),ivKey);

String resulat = desa.decryptStr(content);

通过站长工具(DES在线加密解密工具 - MKLab在线工具)解密时确实设置了密钥、模式、编码,自己反复设置了这些参数仍然不好使,后来发现有个偏移向量的问题,这个偏移向量还不能乱写(正常情况下设置了偏移向量的话是需要把这个偏移向量保存下来的)~~~

具体为什么猜中是8个空的字节数组,人家说"经验",怪咱太年轻呗

相关推荐
文丑颜不良啊12 小时前
LeetCode2761~2780题目解析
java·leetcode
IT枫斗者枫哥12 小时前
子线程断言失败,JUnit 为什么还显示通过?
java·后端
考虑考虑15 小时前
JDK27中的Set.ofLazy
java·后端·java ee
文丑颜不良啊15 小时前
LeetCode2841~2860题目解析
java·leetcode
小南家的青蛙15 小时前
Tomcat与Jetty深度对比
java·tomcat·jetty
乌暮16 小时前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
YDS82917 小时前
Small Spring IOC篇:容器事件及其监听器
java·spring
金銀銅鐵17 小时前
带您看懂字节码指令(三):一行加法里有什么
java·jvm
文丑颜不良啊17 小时前
LeetCode2921~2940题目解析
java·leetcode
北漂人Java18 小时前
基于 Spring AI Alibaba WorkFlow 构建 Java 生产级 AI Agent
java·人工智能·spring