1

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

1
  • 1
    trim() is your friend Commented Jul 28, 2013 at 23:55

1 Answer 1

1

Try this

echo( strcmp(trim($text),trim($decrypttext)) );
Sign up to request clarification or add additional context in comments.

2 Comments

That worked! Thanks, but I still wonder why? anyone can explain?
There is a comment on the mcrypt_decrypt php page that mentions that this function adds whitespace. I am not sure why it does this myself. link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.