javax.crypto.BadPaddingException: Given final block not properly padded 解决方法
下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误:
javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
public class Des {
static Des instance;
static Key key;
static Cipher encryptCipher;
static Cipher decryptCipher;
protected Des() {
}
protected Des(String strKey) {
key = setKey(strKey);
try {
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
public static Des getInstance() {
if (instance == null) {
instance = new Des("diaxxxxoft@201Y10");
}
return instance;
}
// 根据参数生成KEY
private Key setKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
return _generator.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 加密String明文输入,String密文输出
public String setEncString(String strMing) {
BASE64Encoder base64en = new BASE64Encoder();
try {
byte[] byteMing = strMing.getBytes("UTF-8");
byte[] byteMi = this.getEncCode(byteMing);
return base64en.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//加密以byte[]明文输入,byte[]密文输出
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
try {
byteFina = encryptCipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
}
return byteFina;
}
// 解密:以String密文输入,String明文输出
public String setDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
try {
byte[] byteMi = base64De.decodeBuffer(strMi);
byte[] byteMing = this.getDesCode(byteMi);
return new String(byteMing, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
// 解密以byte[]密文输入,以byte[]明文输出
private byte[] getDesCode(byte[] byteD) {
byte[] byteFina = null;
try {
byteFina = decryptCipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
}
return byteFina;
}
//多线程测试一下
public static void main(String[] args) throws InterruptedException {
//没有依赖注入的配置,所以在这里手动生成一次
Des dtDes = Des.getInstance();
final String[] mi = new String[10];
for (int i = 0; i < 10; i++) {
final Integer integer = i;
Thread thread = new Thread() {
public void run() {
//明文加密:
Des dtDes = Des.getInstance();
mi[integer] = dtDes.setEncString("ShowHistory.jsp?MenuId=345&MenuBelong=1&tableLimits=where a1450=RecordId"); //调用get函数获取加密后密文。
}
};
thread.start();
}
Thread.sleep(5000);
for (int i = 0; i < 10; i++) {
final Integer integer = i;
Thread thread2 = new Thread() {
public void run() {
System.out.println(String.format("mi[%s] = %s", integer, mi[integer]));
//这样来模拟另外一个页面的获取
Des dtDes2 = Des.getInstance();
String M = dtDes2.setDesString(mi[integer]);//调用get函数获取解密后明文。
System.out.println(String.format("des[%s] = %s", integer, M));
}
};
thread2.start();
}
//等待打印完毕
Thread.sleep(5000);
}
}
解决方法:
将 setKey方法修改为如下:
// 根据参数生成KEY
private Key setKey(String strKey) {
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
DESKeySpec keySpec = new DESKeySpec(strKey.getBytes("utf-8"));
keyFactory.generateSecret(keySpec);
return keyFactory.generateSecret(keySpec);
} catch (Exception e) {
e.printStackTrace();
} return null;
}
不使用SecureRandom生成SecretKey,而是使用SecretKeyFactory;重新实现方法generateKey,代码如下
问题解决。
另外如果 加密时 和解密 时使用的秘钥 不一样,也会报 相同的错误。
比如加密时使用的秘钥:
diaxxxxoft@201xxxx10
而解密时使用的秘钥:
addddxxxx
那么在解密时也可能会报这个错误。
javax.crypto.BadPaddingException: Given final block not properly padded 解决方法的更多相关文章
- javax.crypto.BadPaddingException: Given final block not properly padded
一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...
- javax.crypto.BadPaddingException: Given final block not properly padded解决方案
解密的时候报错: javax.crypto.BadPaddingException: Given final block not properly padded 该异常是在解密 ...
- exception javax.crypto.BadPaddingException: Given final block not properly padded
exception javax.crypto.BadPaddingException: Given final block not properly padded CreationTime--20 ...
- Java 之 Given final block not properly padded
获取Cipher对象的时候一定要写成 Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 不要写成 Cipher ci ...
- 左右 android AES 所述机器的一部分 javax.crypto.BadPaddingException: pad block corrupted
好多人 android 使用上述 AES 显现 javax.crypto.BadPaddingException: pad block corrupted 下面的代码发布没问题,比较自己.不解释! p ...
- java rsa 解密报:javax.crypto.BadPaddingException: Decryption error
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error at sun.se ...
- 关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法
此文章转载自:http://www.myexception.cn/mobile/1259076.html 关于javax.crypto.BadPaddingException: Blocktype异常 ...
- android AES 部分机器javax.crypto.BadPaddingException: pad block corrupted
package com.bbguoxue.poetry.util; import java.security.SecureRandom; import javax.crypto.Cipher; imp ...
- HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError: junit/framework/Test解决方法
java代码 package webViewer; import java.io.*; import junit.framework.Test; import com.aspose.words.*; ...
随机推荐
- 移动端 h5调试技巧
一 安卓 一 chrome 1.安卓手机安装chrome浏览器,手机打开开发者模式,用usb线链接电脑,并且允许调试. 2.电脑chrome地址栏输入 chrome://inspect 进入后点击 i ...
- C#基于两种需求向图片添加水印
使用场景 1.也就是大家经常用的,一般是图片的4个角落,基于横纵坐标来添加. 2.在图片内基于固定位置,文字始终居中.刚开始我基于第一种场景来根据水印汉字的长度来计算坐标,后来发现方法始终不可靠.现在 ...
- js作用域和变量提升
Function declarations and variable declarations are always moved (“hoisted”) invisibly to the top of ...
- Rafy 领域实体框架 - 领域模型设计器(建模工具)设计方案
去年4月,我们为 Rafy 框架添加了领域模型设计器组件.时隔一年,谨以本文,简要说明该领域模型设计器的设计思想. 设计目标 Rafy 实体框架中以领域驱动设计作为指导思想.所以在开发时,以领域建模为 ...
- [PHP]Maximum execution time of 30 seconds exceeded
前言 在使用PHP渲染页面页面的时候,如果程序处理的时间特别久,超过配置文件(php.ini)设置的超时时间,就会出现如下提示: Maximum execution time of 30 second ...
- form表单提交数据
js代码: // form 跳转 gotourl//跳转的页面 options json格式参数 function FromGoTo(gotourl, options) { var inputhtml ...
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
[源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...
- linux下安装mysql
下载Mysql包 因为mysql比较大,我们不能像安装nginx和php那样,通过下载源码,编译成二进制安装.mysql安装比php和nginx稍微麻烦一点. 这里mysql我们直接下载编译好的二进制 ...
- PHP流程控制之循环结构
计算机程序最擅长的功能之一就是按规定的条件,重复执行某些操作.循环结构可以减少源程序重复书写的工作量,即在给定条件成立时,反复执行某程序段,直到条件不成立为止.给定的条件称为循环条件,反复执行的程序段 ...
- MyBatis Mapper.xml文件中 $和#的区别
MyBatis Mapper.xml文件中 $和#的区别 网上有很多,总之,简略的写一下,作为备忘.例子中假设参数名为 paramName,类型为 VARCHAR . 1.优先使用#{paramN ...