在 PHP 中使用函数 mcrypt_cbc() 出现错误

发布于 2024-10-19 05:02:19 字数 690 浏览 4 评论 0原文

我正在使用我创建的这个函数来加密数据:

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!zNg9l85pgnww
amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风不识月 2024-10-26 05:02:19

当您发现它们时,应该注意弃用警告。

也就是说, 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) to substr($key,32,8) and it ought to work.

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

装纯掩盖桑 2024-10-26 05:02:19

块大小以及 IV 大小均为 8 字节。密钥大小 24 字节。

您可以使用 mcrypt_get_iv_sizemcrypt_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 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.

芸娘子的小脾气 2024-10-26 05:02:19

您正在使用的函数似乎已过时,请参阅 https://www .php.net/manual/en/function.mcrypt-cbc.php

不应再使用此函数,请参阅 mcrypt_generic() 和 mdecrypt_generic() 进行替换。

尝试使用这些函数,看看是否仍然出现错误

The function you are using seems to be depricated, see https://www.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

可爱暴击 2024-10-26 05:02:19

感谢您的所有帮助。我稍后会修复 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`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文