Search code examples
phpwhitespacemcryptstrlen

Removing whitespaces from encrypted data


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?


Solution

  • 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);