OpenSSL decrypt returns false

I tried to encrypt the password in Java and decrypt it in PHP using OpenSLL. I only get bool(false).

Here is my Java code:

private String encryptAES(String text) throws Exception
{
    String key = "something-random";
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(text.getBytes());

    String encrypttext = Base64.encodeToString(encrypted, Base64.URL_SAFE|Base64.NO_WRAP);

    Log.v("ENCRYPTED", encrypttext); // 6sAfStQJ2zNUJLdRgXZsTA==

    return encrypttext;
}

Trying to decrypt a password in PHP:

$output = openssl_decrypt("6sAfStQJ2zNUJLdRgXZsTA==", "AES-256-ECB", "something-random");
var_dump($output); // bool(false)

With a given openSLL error:

error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

Does anyone know what happened? I tried to find the error, but all about node.js


Based on @RandomSeed's answer, I changed the PHP code to the following:

$output = openssl_decrypt(base64_decode("6sAfStQJ2zNUJLdRgXZsTA=="), "AES-256-ECB", "something-random");

This results in an error:

error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length

This edited decryption function also returns bool(false)

+4
source share
1 answer

Your string encrypttextwas Base64 encoded after AES encryption. You need base64_decode()your input line before decryption.

0
source

Source: https://habr.com/ru/post/1616518/


All Articles