下面的 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 解决方法的更多相关文章

  1. javax.crypto.BadPaddingException: Given final block not properly padded

    一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...

  2. javax.crypto.BadPaddingException: Given final block not properly padded解决方案

    解密的时候报错: javax.crypto.BadPaddingException:   Given   final   block   not   properly   padded 该异常是在解密 ...

  3. exception javax.crypto.BadPaddingException: Given final block not properly padded

      exception javax.crypto.BadPaddingException: Given final block not properly padded CreationTime--20 ...

  4. Java 之 Given final block not properly padded

    获取Cipher对象的时候一定要写成 Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); 不要写成 Cipher ci ...

  5. 左右 android AES 所述机器的一部分 javax.crypto.BadPaddingException: pad block corrupted

    好多人 android 使用上述 AES 显现 javax.crypto.BadPaddingException: pad block corrupted 下面的代码发布没问题,比较自己.不解释! p ...

  6. java rsa 解密报:javax.crypto.BadPaddingException: Decryption error

    Exception in thread "main" javax.crypto.BadPaddingException: Decryption error    at sun.se ...

  7. 关于javax.crypto.BadPaddingException: Blocktype错误的几种解决方法

    此文章转载自:http://www.myexception.cn/mobile/1259076.html 关于javax.crypto.BadPaddingException: Blocktype异常 ...

  8. android AES 部分机器javax.crypto.BadPaddingException: pad block corrupted

    package com.bbguoxue.poetry.util; import java.security.SecureRandom; import javax.crypto.Cipher; imp ...

  9. 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.*; ...

随机推荐

  1. 在Visual Studio 2012中使用VMSDK开发领域特定语言(二)

    本文为<在Visual Studio 2012中使用VMSDK开发领域特定语言>专题文章的第二部分,在这部分内容中,将以实际应用为例,介绍开发DSL的主要步骤,包括设计.定制.调试.发布以 ...

  2. 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续3篇-导出时动态生成多Sheet EXCEL)

    ExcelUtility 类库经过我(梦在旅途)近期不断的优化与新增功能,现已基本趋向稳定,功能上也基本可以满足绝大部份的EXCEL导出需求,该类库已在我们公司大型ERP系统全面使用,效果不错,今天应 ...

  3. [Web API] Web API 2 深入系列(5) 特性路由

    目录 1. 特性路由注册 2. 路由解析 - 生成DataTokens - 选择HttpController - 选择Action 特性路由的目的在于更好的提供restful架构的接口,最近好忙(懒) ...

  4. 数字限时增长效果实现:numberGrow.js

    这是上周工作中写到的一个功能,大概的效果就是页面中有几处数字,统计公司的一些业务信息,需要在第一次出现的时候,做一个从0开始增长,大概2秒自动增长到真实数值,并停止增长的效果.这个问题的重点在于解决如 ...

  5. php页面静态化技术;学习笔记

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 【原创】kafka admin源代码分析

    admin包定义了命令行的一些实现 一.AdminOperationException.scala 一个异常类,表示执行admin命令时候抛出的异常 二.AdminUtils.scala admin一 ...

  7. CSS3+jQuery实现时钟插件

    查看效果:http://hovertree.com/texiao/hoverclock/demo4.htm 本插件使用方便,可以在博客园的页面中使用,请看本页面右侧:http://www.cnblog ...

  8. .Net中的并行编程-2.ConcurrentStack的实现与分析

    在上篇文章<.net中的并行编程-1.基础知识>中列出了在.net进行多核或并行编程中需要的基础知识,今天就来分析在基础知识树中一个比较简单常用的并发数据结构--.net类库中无锁栈的实现 ...

  9. Elasticsearch 的坑爹事——记录一次mapping field修改过程

    Elasticsearch 的坑爹事 本文记录一次Elasticsearch mapping field修改过程 团队使用Elasticsearch做日志的分类检索分析服务,使用了类似如下的_mapp ...

  10. IM聊天系统

    先上图片: c# 客户端,openfire服务端,基于java开源推送服务开发的及时聊天系统.大概功能有,单点消息支持文本/图片/截图/音频/视频发送直接播放/视频聊天/大文件传输/动态自定义表情等. ...