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.*; ...
随机推荐
- ORM小结
最近看园子里 @李林峰的园子 关于ORM的介绍,真的很好.自己看了也有一点点小心的,记录一下. ORM即为一种数据模型和数据库中关系映射的一种方式. 学过“三层架构”,知道怎么把表 示层(UI)--& ...
- TeamCity : 安装 Agent
笔者在前文中介绍了 TeamCity Server 的安装过程,本文介绍在 Ubuntu Server 14.04 中安装 TeamCity Agent.Ubuntu Server 上已经创建了用户 ...
- WinFrom 公共控件 Listview 的使用
Listview绑定数据库数据展示与操作使用 1.拖一个Listview控件到项目中先将视图改为Details 2.编辑列 设置列头 添加columnHeader成员 Text 是显示的名称 3. ...
- jquery easyui使用(三)······datagrid加载数据(已解决)
<div id="table_Data"> </div> $("#table_Data").datagrid({ toolbar: '# ...
- GBDT的基本原理
这里以二元分类为例子,给出最基本原理的解释 GBDT 是多棵树的输出预测值的累加 GBDT的树都是 回归树 而不是分类树 分类树 分裂的时候选取使得误差下降最多的分裂 计算的技巧 最终分裂收益按照下面 ...
- sns社区架构设计案例分享
今天看了个社区,了解了一下该产品的架构设计,做一下分享. 内容来源:http://www.jinhusns.com/Document/FrameworkDocument/?type=cc 一. 架构使 ...
- Java字节、十进制、十六进制、字符串之间的相互转换
1. 字节转10进制 直接使用(int)类型转换. /* * 字节转10进制 */ public static int byte2Int(byte b){ int r = (int) b; retur ...
- uct框架数据库sql文件导入错误之 sql_mode
uct框架在导入sql文件时可能会出现一种错误 ERROR 1101 (42000): BLOB/TEXT column 'brief' can't have a default value 这是由于 ...
- Cats(3)- freeK-Free编程更轻松,Free programming with freeK
在上一节我们讨论了通过Coproduct来实现DSL组合:用一些功能简单的基础DSL组合成符合大型多复杂功能应用的DSL.但是我们发现:cats在处理多层递归Coproduct结构时会出现编译问题.再 ...
- async.whilst 的一个简化版实现
function whilst(condition, todo, fin){ var cb = function(){ if(condition()){ todo(cb); }else{ fin(); ...