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

本文讨论了在Linux系统中遇到的Java DES加密解密异常问题,主要表现为 javax.crypto.BadPaddingException: Given final block not properly padded。问题源于SecureRandom在不同操作系统上的行为差异。提供了解决方案,包括使用特定的SecureRandom实例和SecretKeyFactory来确保跨平台一致性。
摘要由CSDN通过智能技术生成

DES java源代码如下:

  1. import java.security.InvalidKeyException;  
  2. import java.security.NoSuchAlgorithmException;  
  3. import java.security.SecureRandom;  
  4. import java.security.spec.InvalidKeySpecException;  
  5.   
  6. import javax.crypto.BadPaddingException;  
  7. import javax.crypto.Cipher;  
  8. import javax.crypto.IllegalBlockSizeException;  
  9. import javax.crypto.KeyGenerator;  
  10. import javax.crypto.NoSuchPaddingException;  
  11. import javax.crypto.SecretKey;  
  12. import javax.crypto.SecretKeyFactory;  
  13. import javax.crypto.spec.DESKeySpec;  
  14.   
  15. public class DESEncryptTest {  
  16. private static final String DES_ALGORITHM = "DES";  
  17.           
  18.     /** 
  19.      * DES加密 
  20.      * @param plainData 
  21.      * @param secretKey 
  22.      * @return 
  23.      * @throws Exception 
  24.      */  
  25.     public String encryption(String plainData, String secretKey) throws Exception{  
  26.   
  27.         Cipher cipher = null;  
  28.         try {  
  29.             cipher = Cipher.getInstance(DES_ALGORITHM);  
  30.             cipher.init(Cipher.ENCRYPT_MODE, generateKey(secretKey));  
  31.               
  32.         } catch (NoSuchAlgorithmException e) {  
  33.             e.printStackTrace();  
  34.         } catch (NoSuchPaddingException e) {  
  35.             e.printStackTrace();  
  36.         }catch(InvalidKeyException e){  
  37.               
  38.         }  
  39.           
  40.         try {  
  41.             // 为了防止解密时报javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher异常,  
  42.             // 不能把加密后的字节数组直接转换成字符串  
  43.             byte[] buf = cipher.doFinal(plainData.getBytes());  
  44.               
  45.             return Base64Utils.encode(buf);  
  46.               
  47.         } catch (IllegalBlockSizeException e) {  
  48.             e.printStackTrace();  
  49.             throw new Exception("IllegalBlockSizeException", e);  
  50.         } catch (BadPaddingException e) {  
  51.             e.printStackTrace();  
  52.             throw new Exception("BadPaddingException", e);  
  53.         }  
  54.           
  55.     }  
  56.   
  57.     /** 
  58.      * DES解密 
  59.      * @param secretData 
  60.      * @param secretKey 
  61.      * @return 
  62.      * @throws Exception 
  63.      */  
  64.     public String decryption(String secretData, String secretKey) throws Exception{  
  65.           
  66.         Cipher cipher = null;  
  67.         try {  
  68.             cipher = Cipher.getInstance(DES_ALGORITHM);  
  69.             cipher.init(Cipher.DECRYPT_MODE, generateKey(secretKey));  
  70.               
  71.       
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值