参考:http://blog.csdn.net/rj042/article/details/8196125

单点登录:https://github.com/ebnew/ki4so

redis客户端操作:http://itoedr.blog.163.com/blog/static/120284297201361843854546

GC问题:http://ifeve.com/garbage-collection-increasing-the-throughput/

DES java源代码如下:

[java] view plaincopy
  1. import java.security.InvalidKeyException;
  2. import java.security.NoSuchAlgorithmException;
  3. import java.security.SecureRandom;
  4. import java.security.spec.InvalidKeySpecException;
  5. import javax.crypto.BadPaddingException;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.IllegalBlockSizeException;
  8. import javax.crypto.KeyGenerator;
  9. import javax.crypto.NoSuchPaddingException;
  10. import javax.crypto.SecretKey;
  11. import javax.crypto.SecretKeyFactory;
  12. import javax.crypto.spec.DESKeySpec;
  13. public class DESEncryptTest {
  14. private static final String DES_ALGORITHM = "DES";
  15. /**
  16. * DES加密
  17. * @param plainData
  18. * @param secretKey
  19. * @return
  20. * @throws Exception
  21. */
  22. public String encryption(String plainData, String secretKey) throws Exception{
  23. Cipher cipher = null;
  24. try {
  25. cipher = Cipher.getInstance(DES_ALGORITHM);
  26. cipher.init(Cipher.ENCRYPT_MODE, generateKey(secretKey));
  27. } catch (NoSuchAlgorithmException e) {
  28. e.printStackTrace();
  29. } catch (NoSuchPaddingException e) {
  30. e.printStackTrace();
  31. }catch(InvalidKeyException e){
  32. }
  33. try {
  34. // 为了防止解密时报javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher异常,
  35. // 不能把加密后的字节数组直接转换成字符串
  36. byte[] buf = cipher.doFinal(plainData.getBytes());
  37. return Base64Utils.encode(buf);
  38. } catch (IllegalBlockSizeException e) {
  39. e.printStackTrace();
  40. throw new Exception("IllegalBlockSizeException", e);
  41. } catch (BadPaddingException e) {
  42. e.printStackTrace();
  43. throw new Exception("BadPaddingException", e);
  44. }
  45. }
  46. /**
  47. * DES解密
  48. * @param secretData
  49. * @param secretKey
  50. * @return
  51. * @throws Exception
  52. */
  53. public String decryption(String secretData, String secretKey) throws Exception{
  54. Cipher cipher = null;
  55. try {
  56. cipher = Cipher.getInstance(DES_ALGORITHM);
  57. cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey));
  58. } catch (NoSuchAlgorithmException e) {
  59. e.printStackTrace();
  60. throw new Exception("NoSuchAlgorithmException", e);
  61. } catch (NoSuchPaddingException e) {
  62. e.printStackTrace();
  63. throw new Exception("NoSuchPaddingException", e);
  64. }catch(InvalidKeyException e){
  65. e.printStackTrace();
  66. throw new Exception("InvalidKeyException", e);
  67. }
  68. try {
  69. byte[] buf = cipher.doFinal(Base64Utils.decode(secretData.toCharArray()));
  70. return new String(buf);
  71. } catch (IllegalBlockSizeException e) {
  72. e.printStackTrace();
  73. throw new Exception("IllegalBlockSizeException", e);
  74. } catch (BadPaddingException e) {
  75. e.printStackTrace();
  76. throw new Exception("BadPaddingException", e);
  77. }
  78. }
  79. /**
  80. * 获得秘密密钥
  81. *
  82. * @param secretKey
  83. * @return
  84. * @throws NoSuchAlgorithmException
  85. */
  86. private SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException{
  87. SecureRandom secureRandom = new SecureRandom(secretKey.getBytes());
  88. // 为我们选择的DES算法生成一个KeyGenerator对象
  89. KeyGenerator kg = null;
  90. try {
  91. kg = KeyGenerator.getInstance(DES_ALGORITHM);
  92. } catch (NoSuchAlgorithmException e) {
  93. }
  94. kg.init(secureRandom);
  95. //kg.init(56, secureRandom);
  96. // 生成密钥
  97. return kg.generateKey();
  98. }
  99. public static void main(String[] a) throws Exception{
  100. String input = "cy11Xlbrmzyh:604:301:1353064296";
  101. String key = "37d5aed075525d4fa0fe635231cba447";
  102. DESEncryptTest des = new DESEncryptTest();
  103. String result = des.encryption(input, key);
  104. System.out.println(result);
  105. System.out.println(des.decryption(result, key));
  106. }
  107. static class Base64Utils {
  108. static private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();
  109. static private byte[] codes = new byte[256];
  110. static {
  111. for (int i = 0; i < 256; i++)
  112. codes[i] = -1;
  113. for (int i = 'A'; i <= 'Z'; i++)
  114. codes[i] = (byte) (i - 'A');
  115. for (int i = 'a'; i <= 'z'; i++)
  116. codes[i] = (byte) (26 + i - 'a');
  117. for (int i = '0'; i <= '9'; i++)
  118. codes[i] = (byte) (52 + i - '0');
  119. codes['+'] = 62;
  120. codes['/'] = 63;
  121. }
  122. /**
  123. * 将原始数据编码为base64编码
  124. */
  125. static public String encode(byte[] data) {
  126. char[] out = new char[((data.length + 2) / 3) * 4];
  127. for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
  128. boolean quad = false;
  129. boolean trip = false;
  130. int val = (0xFF & (int) data[i]);
  131. val <<= 8;
  132. if ((i + 1) < data.length) {
  133. val |= (0xFF & (int) data[i + 1]);
  134. trip = true;
  135. }
  136. val <<= 8;
  137. if ((i + 2) < data.length) {
  138. val |= (0xFF & (int) data[i + 2]);
  139. quad = true;
  140. }
  141. out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
  142. val >>= 6;
  143. out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
  144. val >>= 6;
  145. out[index + 1] = alphabet[val & 0x3F];
  146. val >>= 6;
  147. out[index + 0] = alphabet[val & 0x3F];
  148. }
  149. return new String(out);
  150. }
  151. /**
  152. * 将base64编码的数据解码成原始数据
  153. */
  154. static public byte[] decode(char[] data) {
  155. int len = ((data.length + 3) / 4) * 3;
  156. if (data.length > 0 && data[data.length - 1] == '=')
  157. --len;
  158. if (data.length > 1 && data[data.length - 2] == '=')
  159. --len;
  160. byte[] out = new byte[len];
  161. int shift = 0;
  162. int accum = 0;
  163. int index = 0;
  164. for (int ix = 0; ix < data.length; ix++) {
  165. int value = codes[data[ix] & 0xFF];
  166. if (value >= 0) {
  167. accum <<= 6;
  168. shift += 6;
  169. accum |= value;
  170. if (shift >= 8) {
  171. shift -= 8;
  172. out[index++] = (byte) ((accum >> shift) & 0xff);
  173. }
  174. }
  175. }
  176. if (index != out.length)
  177. throw new Error("miscalculated data length!");
  178. return out;
  179. }
  180. }
  181. }

此代码在windows下运行正常,对加密后的密文可以正常解密。运行结果如下:

xl1nEww4mm09EvMy3tETBNg8HSfTFeBoilhNT7uBKBg=
cy11Xlbrmzyh:604:301:1353064296

但是放到linux上运行,则报错,错误信息如下图:

通过图片可以看到,对相同的明文(cy11Xlbrmzyh:604:301:1353064296)进行加密,在linux上加密后的结果和在windows上是不同的;而且在linux上不能对加密之后的密文进行解密,并抛出异常。

原因:
经过检查之后,定位在生成KEY的方法上,即如下红色代码:
[java] view plaincopy
  1. /**
  2. * 获得秘密密钥
  3. *
  4. * @param secretKey
  5. * @return
  6. * @throws NoSuchAlgorithmException
  7. */
  8. private SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException{
  9. <span style="color:#ff0000;">SecureRandom secureRandom = new SecureRandom(secretKey.getBytes()); //主要是此处代码
  10. </span>
  11. // 为我们选择的DES算法生成一个KeyGenerator对象
  12. KeyGenerator kg = null;
  13. try {
  14. kg = KeyGenerator.getInstance(DES_ALGORITHM);
  15. } catch (NoSuchAlgorithmException e) {
  16. }
  17. kg.init(secureRandom);
  18. //kg.init(56, secureRandom);
  19. // 生成密钥
  20. return kg.generateKey();
  21. }

SecureRandom 实现完全随操作系统本身的內部状态,除非调用方在调用 getInstance 方法,然后调用 setSeed 方法;该实现在 windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系统上则不同。关于SecureRandom类的详细介绍,见 http://yangzb.iteye.com/blog/325264

解决办法

方法1:把原来的generateKey方法中红色如下的红色部分:

[java] view plaincopy
  1. /**
  2. * 获得秘密密钥
  3. *
  4. * @param secretKey
  5. * @return
  6. * @throws NoSuchAlgorithmException
  7. */
  8. private SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException{
  9. <span style="color:#ff0000;"><span><code class="comments">//防止linux下 随机生成key</code></span>
  10. SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
  11. secureRandom.setSeed(secretKey.getBytes());
  12. /span>
  13. // 为我们选择的DES算法生成一个KeyGenerator对象
  14. KeyGenerator kg = null;
  15. try {
  16. kg = KeyGenerator.getInstance(DES_ALGORITHM);
  17. } catch (NoSuchAlgorithmException e) {
  18. }
  19. kg.init(secureRandom);
  20. //kg.init(56, secureRandom);
  21. // 生成密钥
  22. return kg.generateKey();
  23. }

方法2:不使用SecureRandom生成SecretKey,而是使用SecretKeyFactory;重新实现方法generateKey,代码如下

[java] view plaincopy
  1. /**
  2. * 获得密钥
  3. *
  4. * @param secretKey
  5. * @return
  6. * @throws NoSuchAlgorithmException
  7. * @throws InvalidKeyException
  8. * @throws InvalidKeySpecException
  9. */
  10. private SecretKey generateKey(String secretKey) throws NoSuchAlgorithmException,InvalidKeyException,InvalidKeySpecException{
  11. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES_ALGORITHM);
  12. DESKeySpec keySpec = new DESKeySpec(secretKey.getBytes());
  13. keyFactory.generateSecret(keySpec);
  14. return keyFactory.generateSecret(keySpec);
  15. }

Linux下运行java DES解密失败,报javax.crypto.BadPaddingException:Given final block not properly padded相关推荐

  1. Linux下运行java DES AES加解密

    2019独角兽企业重金招聘Python工程师标准>>> DES java源代码如下: import java.security.InvalidKeyException; import ...

  2. RSA解密失败:javax.crypto.BadPaddingException : Decryption error

    一.由于项目需要,前端把密码用RSA加密后传输到后端,后端进行RSA解密后再与数据库中的密码进行对比,接受到前端传过来的加密后的密码,在进行解密的时候出现错误了: javax.crypto.BadPa ...

  3. DESUtils 加解密时 Given final block not properly padded bug小记

    事情的经过是这个样子的...... 先说说问题是怎么出现的.根据客户需求,需要完成一个一键登录的功能,于是我的项目中就诞生了DesUtil,但是经过上百次用户测试,发现有一个用户登录就一直报错!难道又 ...

  4. 如何在虚拟机(linux)下运行java程序

    Linux安装jdk环境 1.先通过原本的电脑在jdk官网下载jdk8,然后发送到我windows和linux的共享目录当中. 2.将压缩包解压到home目录下的java文件夹当中 3.使用命令sud ...

  5. DES加解密时 Given final block not properly padded 的解决方案

    事情的经过是这个样子的...... 先说说问题是怎么出现的.根据客户需求,需要完成一个一键登录的功能,于是我的项目中就诞生了DesUtil,但是经过几百次测试,发现有一个登录直接报错!难道又遇到神坑啦 ...

  6. Linux环境AES解密报错:Given final block not properly padded. Such issues can arise if a bad key is used dur

    将代码替换: String charset = "utf-8"; KeyGenerator kg = KeyGenerator.getInstance("AES" ...

  7. 微信一键登录解密手机号出现javax.crypto.BadPaddingException: pad block corrupted错误

    <button class="weui-btn btn-login" open-type="getPhoneNumber" bindgetphonenum ...

  8. linux下能运行python,(转)Linux下运行python

    原文: http://blog.csdn.net/jackywgw/article/details/48847187 在linux命令行下运行python,可以直接输出hello world jack ...

  9. linux的gets函数,Linux 下使用C语言 gets()函数报错

    在Linux下,使用 gets(cmd) 函数报错:warning: the 'gets' function is dangerous and should not be used. 解决办法:采用 ...

最新文章

  1. Effective_STL 学习笔记(八) 永不建立 auto_ptr 的容器
  2. 看了就会的VScode给C++的配置编译环境(Visual Studio Code)
  3. spring中注解的通俗解释
  4. matlab节点导纳阵求逆,关于利用矩阵稀疏技术求解节点导纳矩阵的MATLAB编程
  5. SQL 如何将视图转换成表
  6. 黑苹果相关驱动介绍及其使用方法
  7. android ui设计欣赏,推荐20款最优秀的安卓界面设计
  8. ❤️❤️马上安排!闺女想在游戏里成为【超人】,Python游戏开发模块Pygame系列之【介绍及安装】❤️❤️源码
  9. 5、zookeeper四字监控命令/配置属性
  10. iOS APP 启动页面的使用
  11. C#工控上位机开发-->1、C#快速编程入门
  12. 在linux下搭建私有云
  13. android ndk开发中初始化char数组报错问题
  14. 思科网络学院-网络互连ccna3-第九章
  15. Stata学习笔记|数据处理1
  16. 位运算判断一个数是奇数还是偶数
  17. FP5207外挂MOS 大功率升压芯片
  18. 数据库实验报告【表数据的插入、修改和删除】
  19. 【论文翻译|2019TKDE】EKT: Exercise-aware Knowledge Tracing for Student Performance Prediction
  20. 玄铁RISC-V处理器软件生态

热门文章

  1. sae上部署第一个站
  2. 云网融合 — 云网络的边界
  3. Simulink仿真---SPWM算法
  4. 修改altium designer原理图右下角信息
  5. 用st-link通过stvp给stm8下载程序的坑
  6. socketserver模块用法,多道技术、 基于UDP的简易版QQ
  7. 2019-4-23 plan
  8. 深度学习在搜索业务中的探索与实践
  9. System.Web.Optimization找不到引用
  10. 如何使用Openssl 制作CA证书