0

Using the same (basic) code from here but switching to MCRYPT_RIJNDAEL_128, I'm now getting the correct data out of php mcrypt_decrypt, except in certain circumstances, it's padding out the end of my message a newline with seemingly random binary characters.

The following, for example, came back from a request to decrypt:

{"messageData":{"identity":"test","msg_id":0,"token":"fakeToken58586"},"messageName":"fetchSavedData_request"}\n\b\b\b\b\b\b\b\b

I would think this is a padding issue, but even if I pad out my string to the correct length, I occasionally get garbage, and strings that would normally need to be padded, do occasionally come back correct.

What's going on here?

Community
  • 1
  • 1
Jeff
  • 5,746
  • 4
  • 33
  • 40
  • Code that's sending to mcrypt is done through crypto++. – Jeff Aug 30 '12 at 16:33
  • I've seen this before in the manual I think; the decoded string is not always right padded with only \0 ... are you using the latest php version? http://au2.php.net/mdecrypt_generic – Ja͢ck Aug 30 '12 at 17:07
  • Hard to say unfortunately. I grabbed the php5-mcrypt package for ubuntu. – Jeff Aug 30 '12 at 17:49

1 Answers1

0

As @Jack pointed out in the comments, this is apparently a common problem. The code here works to remove control characters at the end of encrypted data.

Reproduced here for posterity:

$data = preg_replace( "/\p{Cc}*$/u", "", $data);
Jeff
  • 5,746
  • 4
  • 33
  • 40