Java – OpenSSL decryption returns false

I try to encrypt the password in Java and decrypt it in PHP using opensll I got nothing but Boole

This 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;
}

Attempt to decrypt password in PHP:

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

Given error from opensll:

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

Who knows what's wrong? I tried to look for mistakes, but everything was with node JS related

Based on the answer of @ randomseed, I changed the PHP code to the following:

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

This causes an error:

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

The edited decryption function also returns bool (false)

Solution

After AES encryption, your encrypttext string is Base64 encoded Before decrypting, you need to enter Base64_ Decode() is an input string

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>