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.*; ...
随机推荐
- linker command failed with exit code
转载 : http://blog.csdn.net/chengwuli125/article/details/25051741
- Compute Resource Consolidation Pattern 计算资源整合模式
Consolidate multiple tasks or operations into a single computational unit. This pattern can increase ...
- Linux日志定时清理
linux是一个很能自动产生文件的系统,日志.邮件.备份等.虽然现在硬盘廉价,我们可以有很多硬盘空间供这些文件浪费,让系统定时清理一些不需要的文件很有一种爽快的事情.不用你去每天惦记着是否需要清理日志 ...
- 创建实体数据模型【Create Entity Data Model】(EF基础系列5)
现在我要来为上面一节末尾给出的数据库(SchoolDB)创建实体数据模型: SchoolDB数据库的脚本我已经写好了,如下: USE master GO IF EXISTS(SELECT * FROM ...
- SQL常见的系统存储过程
1.sp_datebases 列出服务器上的所有数据库信息,包括数据库名称和数据库大小 例:exec sp_datebases 2.sp_helpdb 报告有关指定数据库或所有数据库的信息 例:exe ...
- SQL语句分组排序,多表关联排序
SQL语句分组排序,多表关联排序总结几种常见的方法: 案例一: 在查询结果中按人数降序排列,若人数相同,则按课程号升序排列? 分析:单个表内的多个字段排序,一般可以直接用逗号分割实现. select ...
- Newtonsoft.Json(Json.Net)学习笔记
Newtonsoft.Json 在Vs2013中就有自带的: 下面是Json序列化和反序列化的简单封装: /// <summary> /// Json帮助类 /// </summar ...
- JVM监控工具介绍
JVM监控工具介绍 VisualVM是一种集成了多个JDK命令行工具的可视化工具,它能为您提供强大的分析能力.所有这些都是免费的!它囊括的命令行工具包括jps,jstat,jmap,jinfo,jst ...
- iscroll
在原生APP的开发中,有一个常见的功能,就是下拉刷新的功能,这个想必大家都是知道的,但是原生APP的开发,有一个很大的问题就是,你每次更新一些功能,就要用户重新下载一次版本,尤其是在iOS系统中,新版 ...
- Managing database evolutions
When you use a relational database, you need a way to track and organize your database schema evolut ...