决Linux操作系统下AES解密失败的问题

javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
  at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:991)
  at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:847)
  at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
  at javax.crypto.Cipher.doFinal(Cipher.java:2164)
  at com.jugan.utils.AesUtil.decrypt(AesUtil.java:62)
  at com.jugan.utils.AuthUtil.getAesPropertyValue(AuthUtil.java:48)
  at com.jugan.utils.AuthUtil.initApiClient(AuthUtil.java:25)
  at com.jugan.utils.NbIotDeviceUtil.startRefreshTokenTimer(NbIotDeviceUtil.java:21)
  at com.jugan.JuganApplication$MyRunner.run(JuganApplication.java:24)
  at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800)
  at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:338)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
  at com.jugan.JuganApplication.main(JuganApplication.java:17)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
  at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
  at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
  at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
2018-11-13 13:27:03.425  INFO 27033 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-11-13 13:27:03.431 ERROR 27033 --- [           main] o.s.boot.SpringApplication               : Application run failed

JAVA的AES加密解密在windows上测试一切正常,上传到空间上在解密时就出现错误。空间是Linux系统

查看日志发现出现此异常

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

解决方案:

/**
     * 编码格式
     */
    private static final String ENCODING = "UTF-8";
   /**
     * 加密算法
    */
    public static final String KEY_ALGORITHM = "AES";
   /**
     * 签名算法
      */
    public static final String SIGN_ALGORITHMS = "SHA1PRNG"; 

/**
     * 解密AES加密过的字符串
     * 
     * @param content AES加密过过的内容
     * @param password 加密时的密码
     * @return 明文
     */
    public static byte[] decrypt(byte[] content, String password) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(KEY_ALGORITHM);// 创建AES的Key生产者
// //防止linux下 随机生成key 
            SecureRandom random = SecureRandom.getInstance(SIGN_ALGORITHMS);
           random.setSeed(password.getBytes(ENCODING));
          kgen.init(128, random);
//            kgen.init(128, new SecureRandom(password.getBytes()));
            SecretKey secretKey = kgen.generateKey();// 根据用户密码,生成一个密钥
            byte[] enCodeFormat = secretKey.getEncoded();// 返回基本编码格式的密钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 转换为AES专用密钥
            Cipher cipher = Cipher.getInstance("AES");// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
            byte[] result = cipher.doFinal(content);
            return result; // 明文
            
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

参考:https://www.cnblogs.com/Darlin356230410/p/8602674.html

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值