开发者

Getting an error in PHP with function mcrypt_cbc()

I am using this function I made to encrypt data:

function encryptCredentia开发者_StackOverflow社区l($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?


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.


The block size, and so the IV size, is 8 bytes. The key size 24 bytes.

You can get this information with mcrypt_get_iv_size and mcrypt_get_key_size.

In CBC mode the IV must be unique and unpredictable for each encrypted message. Use mcrypt_create_iv(8) to create a suitable one. It needn't be secret, so it can be stored with the encrypted message.


The function you are using seems to be depricated, see http://se2.php.net/manual/en/function.mcrypt-cbc.php

This function should not be used anymore, see mcrypt_generic() and mdecrypt_generic() for replacements.

Try those functions instead and see if you still get the error


Thanks for all the help. I will fix the IV later but here are my new functions for anyone seeing this page and needing them:

`//start encryptCredential function function encryptCredential($data) { $key = '9cqkTFHOfOmKn8kt&NSlIK*XMRWWx*tNY$azRdEvm2to*AQOll%8tP18g35H!zNg9l85pgnww$&q6y@1WrWZhKhx&23acq^*FWf*xdnmI%7aWwM6JQLm%tzYG^*8PIh1zD@D5QKa98Gg'; $cipher = mcrypt_module_open(MCRYPT_blowfish, '', 'cbc', ''); mcrypt_generic_init($cipher, substr($key,8,56), substr($key,32,8)); $encrypted = mcrypt_generic($cipher, pad($data)); mcrypt_generic_deinit($cipher); return base64_encode($encrypted); } //end encryptCredential function

//start decryptCredential function function decryptCredential($data) { $encryptedData = base64_decode($data); $key = '9cqkTFHOfOmKn8kt&NSlIK*XMRWWx*tNY$azRdEvm2to*AQOll%8tP18g35H!zNg9l85pgnww$&q6y@1WrWZhKhx&23acq^*FWf*xdnmI%7aWwM6JQLm%tzYG^*8PIh1zD@D5QKa98Gg'; $cipher = mcrypt_module_open(MCRYPT_blowfish, '', 'cbc', ''); mcrypt_generic_init($cipher, substr($key,8,56), substr($key,32,8)); $decrypted = unpad(mdecrypt_generic($cipher, $encryptedData)); mcrypt_generic_deinit($cipher); return $decrypted; } //end decryptCredential function`

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜