Reputation: 1554
Why I get this warning message "Warning: mcrypt_decrypt(): Received initialization vector of size 44, but size 32 is required for this encryption mode in..." with this code?
$sessionKey = "Secr3t_Sess1on!Key_4t6ydv98*w8ds";
$data = "clear text";
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$keySize = mcrypt_get_key_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
// Encode data
$iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM);
$key = mb_substr (hash('sha256', $sessionKey), 0, $keySize);
$encryptedData = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv);
$encryptedB64Data = base64_encode($iv.$encryptedData);
// Decode data
$data = base64_decode($encryptedB64Data, true);
$key = mb_substr (hash('sha256', $sessionKey), 0, $keySize);
$iv = mb_substr ($data, 0, $ivSize);
$data = mb_substr ($data, $ivSize);
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv);
$decodedData = rtrim($data, "\0");
Upvotes: 0
Views: 6976
Reputation: 179
I think the Problem is the mb_substr method.
$iv = mb_substr ($data, 0, $ivSize);
$data is treated as a multi byte string. That's why multiple chars are counted as one multi byte character. Just use the normal substr function should work.
Upvotes: 1