Jack魏 2019-11-12 01:01 采纳率: 0%
浏览 903

DES解密问题:通过密钥、偏移量加密正常,解密出错?

DES解密问题:通过密钥、偏移量加密正常,解密出错:Given final block not properly padded.那位大神遇到过这种问题。在线等。

问题

  1. package com.whiteink.pms.util;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.SecretKeyFactory;
  5. import javax.crypto.spec.DESedeKeySpec;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import java.security.Key;
  8. import java.util.Base64;
  9. public class DESUtil {
  10. static String key = "693207DB8148FBC9D8410179";
  11. static String iv = "20190225";
  12. static String encoding = "gb2312";
  13. static String data = "{\"parkCode\":\"1\",\"plateNo\":\"京A11113\"}";
  14. /**
  15. * <一句话功能描述>:加密
  16. * <功能详细描述>: 将明文加密
  17. *
  18. * @Param: key 密钥
  19. * @Param keyiv 自定义偏移量(8字节)
  20. * @Param data 明文
  21. * @Return: byte[]
  22. */
  23. public static String des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {
  24. Key deskey = null;
  25. DESedeKeySpec spec = new DESedeKeySpec(key);
  26. SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
  27. deskey = keyfactory.generateSecret(spec);
  28. Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
  29. IvParameterSpec ips = new IvParameterSpec(keyiv);
  30. cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
  31. return Base64.getEncoder().encodeToString(cipher.doFinal(data));
  32. }
  33. /**
  34. * <一句话功能描述>:解密
  35. * <功能详细描述>: 将密文转成明文
  36. *
  37. * @Param: key 密钥
  38. * @Param keyiv 偏移量
  39. * @Param data 明文
  40. * @Return: byte[]
  41. */
  42. public static String des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {
  43. System.out.println(new String(data));
  44. DESedeKeySpec dks = new DESedeKeySpec(key);
  45. SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede");
  46. SecretKey securekey = keyFactory.generateSecret(dks);
  47. // 解密
  48. Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
  49. IvParameterSpec ivp = new IvParameterSpec(keyiv);
  50. cipher.init(Cipher.DECRYPT_MODE, securekey, ivp);
  51. return Base64.getEncoder().encodeToString(cipher.doFinal(data));
  52. }
  53. public static void main(String[] args) throws Exception{
  54. String data = "{\"parkCode\":\"1\",\"plateNo\":\"京A11113\"}";
  55. String mw = des3EncodeCBC(key.getBytes(), iv.getBytes(), data.getBytes("GB2312"));
  56. System.out.println(des3DecodeCBC(key.getBytes(), iv.getBytes(), mw.getBytes()));
  57. }
  58. }

解决方案

===============解决方案===============

  1. package com.whiteink.pms.util;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.spec.IvParameterSpec;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import java.util.Base64;
  7. public class DESUtil {
  8. private static String encoding = "gb2312";
  9. private static String data = "{\"parkCode\":\"1\",\"plateNo\":\"京A11113\"}";
  10. public static String encrypt(String key, String iv, String data) throws Exception {
  11. SecretKey secretKey = new SecretKeySpec(key.getBytes(), "desede");
  12. Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
  13. IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
  14. cipher.init(Cipher.ENCRYPT_MODE, secretKey, ips);
  15. byte[] encryptData = cipher.doFinal(data.getBytes(encoding));
  16. return Base64.getEncoder().encodeToString(encryptData);
  17. }
  18. public static void main(String[] args) throws Exception {
  19. String key = "693207DB8148FBC9D8410179";
  20. String iv = "20190225";
  21. String encryption = encrypt(key, iv, data);
  22. System.out.println(encryption);
  23. String decryption = decrypt(key, iv, encryption);
  24. System.out.println(decryption);
  25. }
  26. public static String decrypt(String key, String iv, String data) throws Exception {
  27. SecretKey secretKey = new SecretKeySpec(key.getBytes(), "desede");
  28. Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
  29. IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
  30. cipher.init(Cipher.DECRYPT_MODE, secretKey, ips);
  31. byte[] decryptData = cipher.doFinal(Base64.getDecoder().decode(data.getBytes(encoding)));
  32. return new String(decryptData, "GB2312");
  33. }
  34. }

展开全部

  • 写回答

1条回答 默认 最新

  • rightnow1980 2019-11-12 01:34
    关注

    看看这个能不能帮你解决,3个文章,https://blog.51cto.com/boytnt/1350441

    评论
    编辑
    预览

    报告相同问题?

    悬赏问题

    • ¥15 PADS Logic 原理图
    • ¥15 PADS Logic 图标
    • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
    • ¥20 气象站点数据求取中~
    • ¥15 如何获取APP内弹出的网址链接
    • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部