Im trying to make a crypt/decrypt routine using mcrypt but it seems to mess the data im trying to encrypt.
Heres the code:
$data = 'Some important data'; $key = "mycryptKey"; $cipher = "rijndael-128"; $encryptedData = mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_ECB); $decryptedData = mcrypt_decrypt($cipher, $key, $encryptedData, MCRYPT_MODE_ECB); var_dump($data); //string 'Some important data' (length=19) var_dump($encryptedData); //string '™ì{*¾xv-&n5’Œü½Ýëc®n)mSƒ7]' (length=32) var_dump($decryptedData); //string 'Some important data�������������' (length=32) It keeps adding those characters at the end of the original string. I saw an example at How do you Encrypt and Decrypt a PHP String? but it didn't work
Thats the actual test. The key and data I'm using are the same posted here
Edit
I realized, after @jonhopkins comment, that mcrypt was padding some "\0" characters after $data content, so i clean it up after decryption using 'strtok':
$decryptedData = \strtok( mcrypt_decrypt($cipher, $key, $encryptedData, MCRYPT_MODE_ECB), "\0" ); var_dump($decryptedData); //string 'Some important data' (length=19)
mcrypt_encryptis padding with 0s up to 32 bytes, but I supposemcrypt_decryptis unable to determine the original length so it is leaving those extra bytes.