2

I have a function for decrypt earlier decrypted data:

public function Decrypt($encrypedText) {
    $key = "The secret key is";
    $decryptedText = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($encrypedText), MCRYPT_MODE_ECB);

    $trimmedData = rtrim($decryptedText, '\0');

    echo strlen($trimmedData);          // 32

    return $trimmedData;
}

If I put in "Test" in the function, the outcome will be "Test" + 28 white spaces. I got the tips from someone who told me to use "rtrim" as done in the function above to remove the white spaces, but that doesn't seem to work (when I check the length of the outcome it's still 32).

What can I do to remove these white spaces?

holyredbeard
  • 19,619
  • 32
  • 105
  • 171
  • Have you tried a simple `trim()`? – Tchoupi Sep 25 '12 at 19:59
  • 1
    Have you tried the trim() without the second argument? Doing so will strip all whitespace characters and not just the NUL-byte character that you had specified. `$trimmedData = rtrim($decryptedText);`http://php.net/manual/en/function.rtrim.php – Buggabill Sep 25 '12 at 20:06
  • @Buggabill: Convert your comment to an answer (or write a new one) and I'll accept it, cuz it solved my problem. :) – holyredbeard Sep 25 '12 at 20:18

2 Answers2

1

Try calling rtrim() without the second argument. This will strip a host of whitespace characters and not just the NUL-byte character that you had specified..

$trimmedData = rtrim($decryptedText);
Buggabill
  • 13,726
  • 4
  • 42
  • 47
0

Strange, trim() should work. Try regular expression:

$string = preg_replace('~\s+$~', '', $string);
Glavić
  • 42,781
  • 13
  • 77
  • 107