没有安装PHP Sodium?

时间:2019-04-13 15:33:31

标签: php cryptography libsodium sodium

我正在尝试第一次使用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文件夹中,我已经在php.ini中添加了extension=sodium,以及其他可以正确加载的扩展。

1 个答案:

答案 0 :(得分:0)

上周末我遇到了同样的问题。 我全新安装了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”检查钠在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) 

我确实在这个论坛和其他论坛上进行了广泛的研究,以找到一个错误,但是直到我尝试了吉姆(http://www.macincloud.com)的方法之后才找到一个错误。 他确实回到了早期版本的php,但是答案出在他的步骤4中。将libsodium.dll复制到“ apache / bin”文件夹中。 libsodium起作用了! 我完全卸载了Xampp,然后再次尝试。 结果相同。 您所需要做的就是将libsodium.dll从“ xampp / php”文件夹复制到“ xampp / apache / bin”文件夹,并且当然取消对php.ini中扩展名的注释,