当前位置:网站首页>Input length must be multiple of 8 when decrypting with padded cipher
Input length must be multiple of 8 when decrypting with padded cipher
2022-07-31 05:08:00 【小果子^_^】
Des密码加密报错:Input length must be multiple of 8 when decrypting with padded cipher
错误原因:
- 判断加密代码上一句执行的是否是解密
我的是先执行解密,然后加密,报的这个错误,原本是直接加密就好的 - 针对文件加密多写了一个字符
附上加密解密代码:
解密:直接调用DesUtil的decrypt方法即可
// DES解密key,限定24位
String DES_KEY = "WfJTKO9S4eLkrPz2JKrAnzdb";
// DES解密iv,限定8位
String DES_IV = "D076D35C";
String pwd = DesUtil.decrypt(userDto.getPsw(), ContentSecurityConstants.DES_KEY,
ContentSecurityConstants.DES_IV);
加密:
- 生成盐值
public static final Integer SALT_SIZE = 8;
/** * 生成随机的Byte[]作为salt. * * @param numBytes * byte数组的大小 */
public static byte[] generateSalt(int numBytes) {
Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);
byte[] bytes = new byte[numBytes];
random.nextBytes(bytes);
return bytes;
}
public static byte[] generateSaltFix() {
return generateSalt(SALT_SIZE);
}
// 盐值
byte[] salt = generateSaltFix();
- PasswdService接口类
/** * 密码 * @param saltBytes 混淆码数组 * @param rawPass 明文密码 * @return 密码 */
public String entryptPassword(byte[] saltBytes,String rawPass);
- PasswdServiceImpl接口实现类
@Override
public String entryptPassword(byte[] saltBytes, String rawPass) {
String encPass = CredentialsDigest.digest(rawPass, saltBytes);
return encPass;
}
- CredentialsDigest证书加密接口
public interface CredentialsDigest {
/** * 散列生成摘要 * @Title: digest * @param plainCredentials 密码明文 * @param salt 混淆码数组 * @return: String */
public String digest(String plainCredentials, byte[] salt);
}
- CredentialsDigest证书加密抽象实现类
/** * Hash证书加密 * */
public abstract class AbstractHashCredentialsDigest implements CredentialsDigest {
public static final int HASH_INTERATIONS = 1024;
@Override
public String digest(String plainCredentials, byte[] salt) {
if (StringUtils.isBlank(plainCredentials)) {
return null;
}
byte[] hashPassword = digest(Cryptos.utf8encode(plainCredentials), salt);
return Encodes.encodeHex(hashPassword);
}
/** * 需要实现加密转换 * @Title: digest * @param input 输入字节数组 * @param salt 混淆字节数组 * @return: byte[] */
protected abstract byte[] digest(byte[] input, byte[] salt);
}
- 不同加密方式(当前使用的是SHA1密码加密 )
(1) SHA1证书加密
public class Sha1CredentialsDigest extends AbstractHashCredentialsDigest {
@Override
protected byte[] digest(byte[] input, byte[] salt) {
return Digests.sha1(input, salt, HASH_INTERATIONS);
}
}
(2) SHA256证书加密
/** * SHA256证书加密 */
public class Sha256CredentialsDigest extends AbstractHashCredentialsDigest implements
CredentialsDigest {
@Override
protected byte[] digest(byte[] input, byte[] salt) {
return Digests.sha256(input, salt, HASH_INTERATIONS);
}
}
(3) SHA512加密方式
/** * SHA512证书加密 */
public class Sha512CredentialsDigest extends AbstractHashCredentialsDigest implements
CredentialsDigest {
@Override
protected byte[] digest(byte[] input, byte[] salt) {
return Digests.sha512(input, salt, HASH_INTERATIONS);
}
}
边栏推荐
- Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
- MySQL-如何分库分表?一看就懂
- Temporal介绍
- Reference code series_1. Hello World in various languages
- The monitoring of Doris study notes
- 快速掌握并发编程 --- 基础篇
- Multiple table query of sql statement
- ES source code API call link source code analysis
- MySQL常见面试题汇总(建议收藏!!!)
- 【一起学Rust】Rust的Hello Rust详细解析
猜你喜欢
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
MySQL8--Windows下使用压缩包安装的方法
STM32 - DMA
面试官,不要再问我三次握手和四次挥手
1. Get data - requests.get()
快速掌握并发编程 --- 基础篇
MySQL忘记密码怎么办
mysql uses on duplicate key update to update data in batches
Redis进阶 - 缓存问题:一致性、穿击、穿透、雪崩、污染等.
MySQL8.0.26安装配置教程(windows 64位)
随机推荐
MySQL_关于JSON数据的查询
Minesweeper game (written in c language)
ERP Production Operation Control Kingdee
MySQL transaction (transaction) (this is enough..)
matlab abel变换图片处理
The monitoring of Doris study notes
Centos7 install mysql5.7
【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数
MySQL window function
On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels
面试官,不要再问我三次握手和四次挥手
centos7安装mysql5.7步骤(图解版)
pycharm专业版使用
Unity Fighter
1. Get data - requests.get()
Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
MySQL transaction isolation level, rounding
Temporal对比Cadence
C Implementation of Simple Network File Copy
MySQL优化:从十几秒优化到三百毫秒