리 눅 스 운영 체제 에서 AES 복호화 에 실패 한 문 제 를 해결 합 니 다.

4308 단어 자바
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 암호 화 해 제 는 윈도 우즈 에서 모든 것 이 정상적으로 테스트 되 며, 공간 에 업로드 되 어 해 제 될 때 오류 가 발생 합 니 다.공간 은 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
 
 

좋은 웹페이지 즐겨찾기