简体   繁体   中英

php strcmp failing with encrypted / decrypted text.. Encoding issue?

I am stumbling on a problem I cannot understand. Just try the following code:

$key = "This is a very secret key";
$text = "This is a very secret message";
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB);
$decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB);

echo( $text. "\r\n" );
echo( $decrypttext . "\r\n" );
echo( md5( $text ) . "\r\n" );
echo( md5( $decrypttext ) . "\r\n" );
echo( strcmp($text,$decrypttext) );

The strcmp() output should give 0 since both strings are equal, but somehow there's something, due to character encoding, that makes this comparison fail..

How can I get that comparison to work, I've tried converting to utf8, deconverting, casting as strings etc. nothing makes this comparison work.It's really a character encoding/decoding issue somewhere, because if you process the md5 of each string, they are different though they look the same to us..

尝试这个

echo( strcmp(trim($text),trim($decrypttext)) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM