[英]javax.crypto.BadPaddingException as I try to convert hex string to byte array. Why do I get it?
As I try to convert a hex string
to a byte array
I get this exception : 当我尝试将
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)
Following is the code that attempted so : 以下是尝试这样做的代码:
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);
}
What is the problem ? 问题是什么 ? Why am I getting an exception ?
我为什么要例外?
You can never decrypt with a random key. 您永远无法使用随机密钥解密。 If you do, you will get plaintext consisting of random bytes.
如果这样做,您将获得由随机字节组成的纯文本。 The cipher however tries to unpad the message.
但是,该密码尝试解封邮件。 As it does not find a valid padding, you will get this exception.
由于找不到有效的填充,您将收到此异常。 Note that - by "luck", about once in 256 - the padding may be correct, in which case you simply retrieve random bytes as plaintext.
请注意-通过“运气”,大约每256个一次-填充可能是正确的,在这种情况下,您只需将随机字节检索为纯文本即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.