Reputation: 1
I'm using this code.
It seems to work when all the code is in one try catch within the main method but not when its separated into another class and decrypt is called through the Security object.
Im guessing a class is not getting initialized properly.
Error:
¬Uˆ±‡Qœò|À'Zâ\\SEPERATION\\ javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:934) at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845) at com.sun.crypto.provider.DESCipher.engineDoFinal(DESCipher.java:314) at javax.crypto.Cipher.doFinal(Cipher.java:2165) at Security.decrypt(Security.java:62) at EncryptAndDecrypt.main(EncryptAndDecrypt.java:15) null
Upvotes: 0
Views: 1483
Reputation: 14194
This is likely an issue with the encoding/decoding process. You store DES-encrypted cipher text into String
objects, rather than first encoding the raw byte output of cipher.doFinal()
into a robust encoding scheme like Base64 or hexadecimal. DES encryption will result in each byte containing a value ranging between 0x00
and 0xFF
(0 - 255), but you then store this output into a String
(the character encoding actually isn't specified; it's likely US-ASCII
, UTF-8
, or similar, but this is platform dependent and can be determined by Charset.defaultCharset()
). The issue with storing un-encoded bytes into a String
is that in Unicode, ASCII, etc., not every value from 0x00
- 0xFF
is guaranteed to be a valid character. It could be a control sequence, undefined, etc.
For this reason, first encode the cipher text in hexadecimal or Base64, then store it as a String. When decrypting, reverse the encoding (i.e. decode it), then perform the decryption.
Upvotes: 1