Search code examples
phpencryptionmcrypt

Getting an error in PHP with function mcrypt_cbc()


I am using this function I made to encrypt data:

function encryptCredential($data) {
$key = '9cqkTFHOfOmKn8kt&NSlIK*XMRWWx*tNY$azRdEvm2to*AQOll%8tP18g35H!zNg9l85pgnww$&q6y@1WrWZhKhx&23acq^*FWf*xdnmI%7aWwM6JQLm%tzYG^*8PIh1zD@D5QKa98Gg';
$encryptedData = mcrypt_cbc(MCRYPT_TripleDES, substr($key,0,32), pad($data), MCRYPT_ENCRYPT, substr($key,32,16));
return base64_encode($encryptedData);
}

PHP then gives me this warning:

PHP Warning:  mcrypt_cbc() [<a href='function.mcrypt-cbc'>function.mcrypt-cbc</a>]: The IV parameter must be as long as the blocksize in /home/xxxxx/public_html/libraries/global.inc.php on line xx

Is my key too long? How many characters should it be?


Solution

  • You should heed deprecation warnings when you find them.

    That said, the block size of TripleDES is 8 bytes, but you're supplying 16 bytes for the IV. Change your substr($key,32,16) to substr($key,32,8) and it ought to work.

    But I'd still recommend moving to the new API.