php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26733 End of an array created by explode corrupted
Submitted: 2003-12-28 14:01 UTC Modified: 2003-12-28 14:13 UTC
From: n0spam_socrate_omega at hotmail dot com Assigned:
Status: Not a bug Package: mcrypt related
PHP Version: 4.3.4 OS: Windows XP Home/Pro
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: n0spam_socrate_omega at hotmail dot com
New email:
PHP Version: OS:

 

 [2003-12-28 14:01 UTC] n0spam_socrate_omega at hotmail dot com
Description:
------------
I have a string encrypted with mcrypt and encoded in base64. I unbase64 this string and I decrypt it using mcrypt. I got the exact same string but when I try to explode it into an array with the explode() function, I cannot make comparison == with the last element of the newly created array.

Reproduce code:
---------------
$key = "validkey";
$input = base64_decode($txtEncrypted);	
$decrypted = mcrypt_ecb(MCRYPT_RIJNDAEL_128, $key, $input, MCRYPT_DECRYPT);

echo $decrypted."<br>";

$array_data = explode('||', $decrypted);

echo "|".$array_data."|<br>";

if ($array_data[8] == 'end') {
   echo "it works!";
}

Expected result:
----------------
data||ddata||daata||dadta||dasta||datad||daata||datsa||end
|end|
it works!

Actual result:
--------------
data||ddata||daata||dadta||dasta||datad||daata||datsa||end
|end|


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-28 14:12 UTC] derick@php.net
This is not a bug. If you use a block cipher like Rijndael then mcrypt needs to pad your data with zeroes to fill up the whole block to encrypt. If you are decrypting the encrypted string those trailing zeroes are still there, and thus the comparison of the last element fails. 

Before you start asking if we can't trim the string while decrypting; no this is not possible because we don't know if you are encrypting binary data (which might end in a zero) or not. You'll have to take care of this yourself then by using rtrim during comparison.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC