[英]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.