mcrypt_generic 与 mcrypt_crypt

发布于 2024-08-31 13:07:39 字数 58 浏览 9 评论 0原文

有谁知道 PHP 加密时 mcrypt_generic 和 mcrypt_encrypt 之间的区别?

Does anyone know the difference between mcrypt_generic and mcrypt_encrypt when it comes to encryption in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

终弃我 2024-09-07 13:07:39

mcrypt_encrypt() 结合了多种方法的功能,而 mcrypt_generic() 必须在其他 mcrypt_* 调用的特定序列中调用。如果您需要较低级别 API 的灵活性,则可以使用 mcrypt_generic(),而 mcrypt_encrypt() 则充当较高级别的实用程序。

PHP 文档中的这个示例很好地比较了两种 API 方法。它指的是 mcrypt_ecb(),但出于比较的目的,您可以认为它与 mcrypt_encrypt() 类似。

来自 http://us.php.net/manual/en/mcrypt.examples。 php

$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";

$encrypted_data = mcrypt_ecb (MCRYPT_3DES, $key, $input, MCRYPT_ENCRYPT);

或者:

$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";

$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

mcrypt_encrypt() combines the functionality of several methods, whereas mcrypt_generic() must be called within a certain sequence of other mcrypt_* calls. You would use mcrypt_generic() if you needed the flexibility of the lower-level API, whereas mcrypt_encrypt() acts as a higher-level utility.

This example in the PHP documentation shows a good comparison between the two API approaches. It refers to mcrypt_ecb(), but for the purposes of this comparison you can consider it to be similar to mcrypt_encrypt().

From http://us.php.net/manual/en/mcrypt.examples.php

$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";

$encrypted_data = mcrypt_ecb (MCRYPT_3DES, $key, $input, MCRYPT_ENCRYPT);

Or:

$key = "this is a secret key";
$input = "Let us meet at 9 o'clock at the secret place.";

$td = mcrypt_module_open('tripledes', '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$encrypted_data = mcrypt_generic($td, $input);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文