我第一次尝试使用 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
更新
Ran get_loaded_extensions 和钠没有安装,我不知道为什么。php_sodium.dll
存在于 ext 文件夹中,我已extension=sodium
在 php.ini 中添加了其他可以正确加载的扩展。