gpt4 book ai didi

未安装 PHP Sodium?

转载 作者:行者123 更新时间:2023-12-05 02:12:35 30 4
gpt4 key购买 nike

我第一次尝试使用 Libsodium 进行加密,但我遇到了一个错误。我在 XAMPP 上运行一切; sodium 位于/ext/文件夹中,我已将 extension=sodium 添加到 php.ini 文件中。

这是我的代码。

// This refers to the previous code block.
require "safeCrypto.php";

// Do this once then store it somehow:
$key = random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
$message = 'We are all living in a yellow submarine';

$ciphertext = safeEncrypt($message, $key);
$plaintext = safeDecrypt($ciphertext, $key);

echo $ciphertext;
echo $plaintext;
echo phpversion();

它抛出这个错误。

Warning: Use of undefined constant SODIUM_CRYPTO_SECRETBOX_KEYBYTES - assumed 'SODIUM_CRYPTO_SECRETBOX_KEYBYTES' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\php\crypttest.php on line 6
Fatal error: Uncaught TypeError: random_bytes() expects parameter 1 to be integer, string given in D:\xampp\htdocs\php\crypttest.php:6

get_loaded_extensions() 显示未安装钠,我不明白为什么。 php_sodium.dll 文件存在于 ext 文件夹中,我在 php.ini 文件中添加了 extension=sodium

我使用的是 PHP 7.2.10。

最佳答案

我上周末遇到了同样的问题。我全新安装了 Windows 10 作为我的新开发机器我还使用 Apache Netbeans 11.1 安装了 JDK以及新安装的 XAMMP (xampp-windows-x64-7.3.11-0-VC15-installer)安装后,我修改了 php.ini 以取消注释“extension=sodium”,并开始使用 xampp shell 中的命令“php -m”检查 sodium 是否在 php 中处于事件状态。在那里我看到钠的模块被加载了。

在我的 test.php 中,我输入了以下行:

     <?php
echo random_bytes(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);
?>

检查它是否有效。可悲的是我得到了一个错误:

 Warning: Use of undefined constant SODIUM_CRYPTO_SECRETBOX_KEYBYTES - assumed 'SODIUM_CRYPTO_SECRETBOX_KEYBYTES' (this will throw an Error in a future version of PHP) 

我在这个论坛和其他论坛上做了扩展研究以找到答案,但直到我尝试了 Jim (https://stackoverflow.com/a/49142981/10509146) 的方法才找到答案他确实回到了早期版本的 php,但答案在他的第 4 步中。将 libsodium.dll 复制到“apache/bin”文件夹。libsodium 起作用了!我完全卸载了 Xampp 并再次尝试。结果相同。您需要做的就是将 libsodium.dll 从“xampp/php”文件夹复制到“xampp/apache/bin”文件夹,当然取消注释 php.ini 中的扩展,您就可以开始了。

关于未安装 PHP Sodium?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55667110/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com