Braikar
Braikar

Reputation: 1323

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..

Upvotes: 1

Views: 117

Answers (1)

Jacob
Jacob

Reputation: 920

Try this

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

Upvotes: 1

Related Questions