下面的 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. LSM Tree存储组织结构介绍

    LSM Tree(Log Structured Merge Trees)数据组织方式被应用于多种数据库,如LevelDB.HBase.Cassandra等,下面我们从为什么使用LSM tree.LSM ...

  2. mybatis入门基础(九)----逆向工程

    一.什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po. ...

  3. 初学Python遇到的问题一二

    这篇文章只是学Python最最最基础的部分知识,如果你已经有过python经验,哪怕就一点点经验,或许你都遇到过,但相信这些问题对你来说早已不算问题了,所以请跳过吧,用你的时间去学习更多其他的知识就好 ...

  4. iOS查看3D效果的手势交互

    公司项目中用到的,仿的人家厂子的效果,看起来还是挺高大上的,其实实现起来很简单,是一种伪3D;用手势滑动查看一个商品的3D展示. 在手机上手指左右滑动可以360°无死角查看这个商品,有兴趣的可以下de ...

  5. Openfire 4.1.0

    http://www.igniterealtime.org/downloads/index.jsp 服务器端口 接口 端口   类型 描述 所有的地址 5222 客户端到服务器 客户端使用标准端口连接 ...

  6. [工具] Firemonkey Style 调色工具(可另存 Style 文件)

    版本:2016.12.21 (新增可取代颜色) 下载:[工具]OneStylePalette_调色工具_20161221.zip 版本:2016.12.09 (新增可导出全平台的 Style) 下载: ...

  7. Android源码编译make的错误处理

    android源码下载:官方下载 或参考android源码下载方式 Android编译版本: PLATFORM_VERSION=4.0.1(最新Android 4.0.1) OS 操作系统平台: Li ...

  8. Scalaz(57)- scalaz-stream: fs2-多线程编程,fs2 concurrency

    fs2的多线程编程模式不但提供了无阻碍I/O(java nio)能力,更为并行运算提供了良好的编程工具.在进入并行运算讨论前我们先示范一下fs2 pipe2对象里的一些Stream合并功能.我们先设计 ...

  9. 【转】acm小技巧

    1.一般用c语言节约空间,要用c++库函数或STL时才用c++: cout.cin和printf.scanf最好不要混用. 大数据输入输出最后不用cin.cout,纺织超市. 2.有时候int型不够用 ...

  10. eclipse中egit插件使用

    这篇文章当时制作有点粗糙,建议阅读升级版:eclipse中egit插件使用--升级版 使用git作为项目的代码管理工具现在是越来越火,网上有各种各样的文章.博客.讨论,其中以命令行居多.使用eclip ...