【发布时间】:2019-09-04 03:11:09
【问题描述】:
我第一次尝试使用 Libsodium 进行加密,但我遇到了错误!我在 XAMPP 上运行所有东西,钠位于 /ext/ 文件夹中,我已将 extension=sodium 添加到 php.ini
这是我的代码:
<?php
// 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();
抛出此错误:
<br />
<b>Warning</b>: 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 <b>D:\xampp\htdocs\php\crypttest.php</b> on line <b>6</b><br />
<br />
<b>Fatal error</b>: Uncaught TypeError: random_bytes() expects parameter 1 to be integer, string given in D:\xampp\htdocs\php\crypttest.php:6
我使用的是 PHP 7.2.10
更新
运行 get_loaded_extensions 并且未安装钠,我不知道为什么。 php_sodium.dll 存在于 ext 文件夹中,我在 php.ini 中添加了 extension=sodium,以及其他可以正确加载的扩展。
【问题讨论】:
-
更改 php.ini 后是否重新启动了 Apache?
-
@JiriHrazdil 是的,我还在重新加载之前从扩展列表中删除了 curl,所以我知道扩展列表“有效”。为什么 php.ini 中的某些扩展名有分号?
标签: php cryptography libsodium sodium