0

I am trying to decrypt the data in Java which was encrypted in PHP using AES-256-CBC. Decrypt method of Java cipher.doFinal throwing IllegalBlockSizeException. Can anyone help me to resolve this? Banging my head to fix this from the past 2 days. Please let me know if need more info.

     public static String decrypt(String encryptedResult, String secretKey, String iv) {
        String decrypted;
        try {
            byte[] bytes = new BigInteger(encryptedResult.trim(),16).toByteArray();
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, makeKey(secretKey), makeIv(iv));
            byte[] bytesFinal = cipher.doFinal(bytes);
            decrypted = new String(bytesFinal);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return decrypted;
    }

    static AlgorithmParameterSpec makeIv(String iv) {
        try {
            return new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    static Key makeKey(String secretKey) {
        return new SecretKeySpec(secretKey.getBytes(), "AES");
    }
Arsee
  • 283
  • 5
  • 20
  • The error indicates that size of `byte[] bytes` is not a multiplum of AES' blocksize of 16. What is the length of bytes ? – Ebbe M. Pedersen Mar 09 '20 at 08:44
  • @EbbeM.Pedersen the length of bytes is 33 – Arsee Mar 09 '20 at 08:49
  • That is not a valid length for AES/CBC .. you need to take a look at the encryption. – Ebbe M. Pedersen Mar 09 '20 at 08:52
  • there is no issue in the encryption as far as I know because the same encrypted string works well on other platforms like iOS. Can you pls help me to know if there is anything wrong in this line "byte[] bytes = new BigInteger(encryptedResult.trim(),16).toByteArray();" – Arsee Mar 09 '20 at 08:57
  • Show the encrypted data – Ebbe M. Pedersen Mar 09 '20 at 09:00
  • "cae0340200ffead17f16d7ec7e4fc5206ec30292af958ee713fe45aa5a8f2bde" – Arsee Mar 09 '20 at 09:01
  • Encrypted data seems to be HEX encoded, don't you need to decode them first (to a byte array?) ´IllegalBlockSizeException´ could be returned when having wrong key (or input) resulting in an invalid padding – gusto2 Mar 09 '20 at 09:07
  • 1
    DO NOT TRIM the input, how did you came to that? – gusto2 Mar 09 '20 at 09:22
  • 1
    Don't use BigInteger to decode your hexstring .. with your example it prepends your data with a 0, thus increasing the data to 33 bytes - making it an invalid encryption block. – Ebbe M. Pedersen Mar 09 '20 at 09:27
  • after your helpful comments, I tried to decode like this "byte[] bytes = Base64.getDecoder().decode(encryptedResult)" now my bytes length is 48 and I got this error "javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT" – Arsee Mar 09 '20 at 09:30
  • This just indicates that your decrypt failed .. You properbly provides a wrong key. `secretKey.getBytes()` is definitely not the right way to provide a binary key. – Ebbe M. Pedersen Mar 09 '20 at 09:47
  • I saw a StackOverflow post which is getting binary of a key like this, can you please help me with the better way of doing it? – Arsee Mar 09 '20 at 09:58
  • 1
    Based on the posted radix of `16` and the string `cae0...2bde` the encrypted data seem to be hexadecimal encoded. Therefore a hexadecimal decoding might be more useful than a Base64 decoding, e.g. `byte[] bytes = hexStringToByteArray(encryptedResult);` where `hexStringToByteArray` is from [here](https://stackoverflow.com/a/140861/9014097). – Topaco Mar 09 '20 at 10:04
  • Great!! Its finally working and the length is also 32 using this method "hexStringToByteArray". Thanks to everyone guys, god bless you and have a great day. – Arsee Mar 09 '20 at 10:14

0 Answers0