PHP and JAVA encryption and decryption procedures

I have the following PHP procedure to encrypt my message with a client:

public static function encrypt($input, $key) {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); 
        $input = AES::pkcs5_pad($input, $size); 
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); 
        $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
        mcrypt_generic_init($td, $key, $iv); 
        $data = mcrypt_generic($td, $input); 
        mcrypt_generic_deinit($td); 
        mcrypt_module_close($td); 
        $data = base64_encode($data); 
        return $data; 
} 

and the following Java rotuine to decrypt my server message:

public static String decrypt(String input, String key) {
        byte[] output = null;
        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skey);
            output = cipher.doFinal(Base64.decode(input,Base64.NO_WRAP));
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return new String(output);
}

Why does the following decryption procedure throw an exception?

java.lang.IllegalArgumentException: bad base-64

Edit: after hard-coding a string into code, I get a BadPaddingException.

my input for the decryption function:

wrmRa2hAoseNOev6/ascapxkLQRGX/GW3DQm3ETwBH7gJm1NetkgGFzgY4kZTE10Tv45YIcy/xoodq/GumSY5hsao1s4bkuKXZeim/IDTVr3elrqX13b81/XE5iB3iJrAqny2dQ5SsWso0lUcAZGS2Wls/lTeQiIKXEaOh7iZZ3xOtM6633iNcoiFxEnX5A0dMrdRNEOkmQ3UnQmuIGTSv0RLKuPv5r5dplGZ3N2LMMpoB0AMu3DSXFEdiD4XN49
+4
source share
1 answer

Maybe you need to use:

Base64.decode(input,Base64.NO_WRAP|Base64.URL_SAFE)

You may need another combination of flags.

, base64. "+" "", URL- -.

+1

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


All Articles