获取Cipher对象的时候一定要写成

  Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");

  不要写成

  Cipher cipher = Cipher.getInstance("DES");

  否则解密的时候会报错:

  Given final block not properly padded

  原因是Cipher cipher = Cipher.getInstance("DES");与Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");等同,填充方式错误,加密的时候会得到16长度的字节数组。

  解密的时候报错: javax.crypto.BadPaddingException:   Given   final   block   not   properly   padded

  仔细分析一下,不难发现,该异常是在解密的时候抛出的,加密的方法没有问题。

  但是两个方法的唯一差别是Cipher对象的模式不一样,这就排除了程序写错的可能性。再看一下异常的揭示信息,大概的意思是:提供的字块不符合填补的。

  原来在用DES加密的时候,最后一位长度不足64的,它会自动填补到64,那么在我们进行字节数组到字串的转化过程中,可以把它填补的不可见字符改变了,所以引发系统抛出异常。

  问题找到,怎么解决呢?大家还记得邮件传输通常会把一些信息编码保存,对了,就是Base64,那样保证了信息的完整性。为了方便使用,我们再写一个新的方法封装一下原来的方法:

  1. public static String DataEncrypt(String str, byte[] key)
  2. {  
  3. String encrypt = null;  
  4. try
  5. {  
  6. byte[] ret = encode(str.getBytes("UTF-8"),key);  
  7. encrypt = new String(Base64.encode(ret));  
  8. }
  9. catch(Exception e){  
  10. System.out.print(e);  
  11. encrypt = str;  
  12. }  
  13. return encrypt;  
  14. }
  15. public static String DataDecrypt(String str, byte[] key)
  16. {  
  17. String decrypt = null;  
  18. try
  19. {  
  20. byte[] ret = decode(Base64.decode(str),key);  
  21. decrypt = new String(ret,"UTF-8");  
  22. }
  23. catch(Exception e)
  24. {  
  25. System.out.print(e);  
  26. decrypt = str;  
  27. }  
  28. return decrypt;  
  29. }  

  我们把方法的参数改成了字串,但是为什么要用UTF-8呢?不指定它的字节格式不行吗?大家知道,UTF-8是国际通用的字符编码,用它传输任何字串都不会有问题,通过它也可以很完美的解决J2EE的中文问题!所以我们最好用UTF-8编码,以减少不必要的麻烦。

Java 之 Given final block not properly padded的更多相关文章

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

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

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

    下面的 Des 加密解密代码,在加密时正常,但是在解密是抛出错误: javax.crypto.BadPaddingException: Given final block not properly p ...

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

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

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

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

  5. 详解Java中的final关键字

    本文原文地址:https://jiang-hao.com/articles/2019/coding-java-final-keyword.html1 final 简介2 final关键字可用于多个场景 ...

  6. 浅析Java中的final关键字(转载)

    自http://www.cnblogs.com/dolphin0520/p/3736238.html转载 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括 ...

  7. 关于Java中的final关键字

    Java中的final关键字是用来限制用户行为的,说白了,就是用来限制我们这些程序员的.final可以用来修饰:变量.方法.类. 1)Java final variable final用来修饰变量时, ...

  8. 深入理解Java中的final关键字

    Java中的final关键字非常重要,它可以应用于类.方法以及变量.这篇文章中我将带你看看什么是final关键字?将变量,方法和类声明为final代表了什么?使用final的好处是什么?最后也有一些使 ...

  9. 浅析Java中的final关键字

    浅析Java中的final关键字 谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来 ...

随机推荐

  1. IEEE Bigger系列题解

    Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...

  2. Codeforces Round #260 (Div. 2) B. Fedya and Maths

    B. Fedya and Maths time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. 记一次帮朋友解决apache站点403错误的过程

    apache版本: [root@iZ25eby2utyZ web]# rpm -qa | grep httpd httpd-tools--.el6.centos..x86_64 httpd--.el6 ...

  4. MAX II Device Compatibility with 5.0-V CMOS Devices

    http://www.altera.com/literature/hb/max2/max2_mii51009.pdf The open-drain pin never drives high, onl ...

  5. HTTP协议中 POST和GET的区别

    http://blog.csdn.net/whuslei/article/details/6667095 权威点的说明请参考:http://www.cs.tut.fi/~jkorpela/forms/ ...

  6. [Apache] Apache 從 2.2 換至 2.4 httpd.conf 的調整筆記 (windows 環境)

    原文地址: http://www.dotblogs.com.tw/maplenote/archive/2012/07/20/apache24_httpd_conf.aspx 整理一下 Windows ...

  7. extjs 事件监听 三种方式

    xtype : 'textarea', name : 'dataSetField', labelSeparator:'', fieldLabel:'', hideLabel: true, allowB ...

  8. TPL中限制进程数量

    The MaxDegreeOfParallelism sets the maximum number of simultaneous threads that will be used for the ...

  9. Latex:表格制作全攻略

    给出一个制作复杂表格的例子,制作表格主要用到multicolumn,multirow和cline,其中,要使用multirow,必须usepackage{multirow} 如果要制作出如下图所示的表 ...

  10. coursera课程Text Retrieval and Search Engines之Week 1 Overview

    Week 1 OverviewHelp Center Week 1 On this page: Instructional Activities Time Goals and Objectives K ...