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个空的字节数组,人家说"经验",怪咱太年轻呗

相关推荐
鹏易灵4 分钟前
C++——2.常量与 const、constexpr 初识详解
java·开发语言·c++
qq_452396237 分钟前
第十三篇:《K8s 安全基础:RBAC、ServiceAccount、Pod Security》
java·安全·kubernetes
张某布响丸辣31 分钟前
Spring AI 极简入门:Java 开发者快速上手 AI 开发
java·人工智能·spring·springai
java1234_小锋33 分钟前
请描述 Spring Boot 的启动流程,包括 SpringApplication 的初始化和 run 方法的核心步骤。
java·数据库·spring boot
疯狂成瘾者35 分钟前
Java 集合 LinkedList 详解:链表结构、常用方法和队列使用
java·开发语言·链表
lanyxp41 分钟前
Sentinel 管不到 SQL 这一层——我写了个 MyBatis SQL 熔断器
java
武子康1 小时前
Java-28 深入浅出 Spring 实现简易Ioc-04 在上节的业务下手动实现AOP
java·后端·mybatis
慧一居士1 小时前
SpringCloud 微服务Feigin 用的完整调用端和被调用的示例
java·spring cloud
CodeStats1 小时前
【虚拟机】 从 CPU 指令到虚拟机隔离:虚拟机就是一个“模拟了完整硬件的普通进程”
java·docker