Here is my encryption method(value is an input parameter):
byte key_bytes[] = "12345678".getBytes();
SecretKeySpec _keyspec = new SecretKeySpec(key_bytes, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); // Yes, I know I shouldn't use DES
cipher.init(Cipher.ENCRYPT_MODE, _keyspec);
byte[] utf8 = value.getBytes("UTF8");
byte[] enc = cipher.doFinal(utf8); // Encrypt
String encrypted = new String(new Base64().encode(enc));
return URLEncoder.encode(encrypted, "UTF-8");
Here is my decryption method(value is an input parameter):
byte key_bytes[] = "12345678".getBytes();
SecretKeySpec _keyspec = new SecretKeySpec(key_bytes, "DES");
Cipher dcipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
dcipher.init(Cipher.DECRYPT_MODE, _keyspec);
byte[] dec = new Base64().decode(value);
byte[] utf8 = dcipher.doFinal(dec); // Decrypt, throws exception
return new String(utf8, "UTF8");
And I get an Exception:
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
I've read different topics, so I figured out, that this exception occurs, when there is no padding and there is another cipher mode. So, what's wrong?