I am using these two functions below to encrypt and decrypt a string. I left the example as clean as possible.
Function for encript
$key = '123';
$iv = md5( md5( 'key' ) );
mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);
Function for decript
$key = '123';
$iv = md5( md5( 'key' ) );
mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, $iv);
I do not really know the function, and I'm having trouble using json encode / decode. I can use encode without problem, but decode does not run the decrypted string .
I used as an example a simple array array( 123 )
and the decrypted output is exactly equal to json_encode( array( 123 ) )
, the difference is the size, but no spaces appear before or at the end.
Dump
decrypt( $argument ) : string(32) "[123]"
json_encode( array( 123 ) ) : string(5) "[123]"
I found a question that reports the same problem as mine and the proposed solution was the use of rtrim( $decrypted , "
. It worked for the author and for me, but the explanation is superficial:
" )trim
It says that the ENCRYPT / DECRYPT function adds garbage to be the correct size, and should remove the nulls at the end of the string.
I also created null values at the end of the string but did not affect the size in the dump. I can not understand how the string size does not match.