P
P
Pavel2018-08-31 08:09:39
PHP
Pavel, 2018-08-31 08:09:39

How to properly encrypt text in openssl?

Hello.
I need to encrypt a string in php and decrypt in python with regular AES-256-ECB.
here is the code:

function create_hash($var1, $var2, $var3) {

    $key = 'hVyzdDQmDqanzkUDowPtere17cvNN3Lm';
    $input_string = $var1 . ' ' . $var2 . ' ' . $var3;

    $hash = openssl_encrypt($input_string, 'AES-256-ECB', $key);

    //make hash url safety
    $hash = strtr( $hash, '+/', '-_' );

    return $hash;
}

But when decrypting in Python, extra characters are often added to the string:
5b88ccbd37e6f376579821.png
if you add OPENSSL_ZERO_PADDIN to the options for openssl_encrypt, then the extra characters disappear, but another problem appears. Now you can get an error when encrypting
error:0607F08A:digital envelope routines:EVP_EncryptFinal_ex:data not multiple of block length

As far as I understand, so that there would be no problems, the encrypted string must be a multiple of the length? eight? sixteen? How to finish up to multiplicity, pad with spaces?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question