簡體   English   中英

javax.crypto.BadPaddingException,因為我嘗試將十六進制字符串轉換為字節數組。 我為什么得到它?

[英]javax.crypto.BadPaddingException as I try to convert hex string to byte array. Why do I get it?

當我嘗試將hex string轉換為byte array此異常:

Aug 15, 2013 10:17:32 PM Tester main
SEVERE: null
javax.crypto.BadPaddingException: Given final block not properly padded
        at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
        at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
        at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(BlowfishCipher.java:319)
        at javax.crypto.Cipher.doFinal(Cipher.java:1978)
        at Tester.main(Tester.java:21)

以下是嘗試這樣做的代碼:

try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
        SecretKey secretKey = keyGenerator.generateKey();
        Cipher cipher = Cipher.getInstance("Blowfish"); 
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        String decryptSt = new String(cipher.doFinal(DatatypeConverter.parseHexBinary("f250d7a040859d66541e2ab4a83eb2225d4fff880f7d2506")));
        System.out.println(decryptSt);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
    }

問題是什么 ? 我為什么要例外?

您永遠無法使用隨機密鑰解密。 如果這樣做,您將獲得由隨機字節組成的純文本。 但是,該密碼嘗試解封郵件。 由於找不到有效的填充,您將收到此異常。 請注意-通過“運氣”,大約每256個一次-填充可能是正確的,在這種情況下,您只需將隨機字節檢索為純文本即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM