在 PHP 中使用函数 mcrypt_cbc() 出现错误
我正在使用我创建的这个函数来加密数据:
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 给我这个警告:
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
我的密钥太长了吗?应该有多少个字符?
I am using this function I made to encrypt data:
function encryptCredential($data) {
$key = '9cqkTFHOfOmKn8kt&NSlIK*XMRWWx*tNY$azRdEvm2to*AQOll%8tP18g35H!zNg9l85pgnwwamp;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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您发现它们时,应该注意弃用警告。
也就是说, TripleDES 的块大小是 8 个字节,但是您为四.将您的
substr($key,32,16)
更改为substr($key,32,8)
它应该可以工作。但我仍然建议转向新的 API。
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)
tosubstr($key,32,8)
and it ought to work.But I'd still recommend moving to the new API.
块大小以及 IV 大小均为 8 字节。密钥大小 24 字节。
您可以使用
mcrypt_get_iv_size
和mcrypt_get_key_size
获取此信息。在 CBC 模式下,每条加密消息的 IV 必须是唯一且不可预测的。使用
mcrypt_create_iv(8)
创建一个合适的。它不需要保密,因此可以与加密消息一起存储。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
andmcrypt_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.您正在使用的函数似乎已过时,请参阅 https://www .php.net/manual/en/function.mcrypt-cbc.php
尝试使用这些函数,看看是否仍然出现错误
The function you are using seems to be depricated, see https://www.php.net/manual/en/function.mcrypt-cbc.php
Try those functions instead and see if you still get the error
感谢您的所有帮助。我稍后会修复 IV,但以下是我的新函数,供任何看到此页面并需要它们的人使用:
`//start encryptCredential function
函数 encryptCredential($data) {
$key = '9cqkTFHOfOmKn8kt&NSlIK*XMRWWx*tNY$azRdEvm2to*AQOll%8tP18g35H!zNg9l85pgnww$&q6y@1WrWZhKhx&23acq^*FWf*xdnmI%7aWwM6JQLm%tzYG^*8PIh 1zD@D5QKa98Gg';
$cipher = mcrypt_module_open(MCRYPT_blowfish, '', 'cbc', '');
mcrypt_generic_init($cipher, substr($key,8,56), substr($key,32,8));
$加密 = mcrypt_generic($cipher, pad($data));
mcrypt_generic_deinit($cipher);
返回base64_encode($加密);
}
//结束 encryptCredential 函数
//开始解密Credential 函数
函数decryptCredential($data) {
$encryptedData = base64_decode($data);
$key = '9cqkTFHOfOmKn8kt&NSlIK*XMRWWx*tNY$azRdEvm2to*AQOll%8tP18g35H!zNg9l85pgnww$&q6y@1WrWZhKhx&23acq^*FWf*xdnmI%7aWwM6JQLm%tzYG^*8PIh 1zD@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);
返回$解密;
}
//结束decryptCredential函数`
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`