我在哪里放置 defuse 库,以避免调用未定义的方法?
Where do I place the defuse library, to avoid the Call to undefined method?
我很想尝试双向加密的 defuse 库。我已将 defuse 目录和子目录复制到我的网站根目录,并将 .phar 文件复制到 /var/www/lib/defuse-crypto.phar,按照 InstallAndVerify.md 的指示。在我的测试脚本中,我包含了
require_once('/var/www/lib/defuse-crypto.phar');
但是我收到以下错误。
Fatal error: Uncaught Error:
Call to undefined method Defuse\Crypto\Crypto::CreateNewRandomKey() in /var/www/encryptTest.php:12
Stack trace: #0 {main} thrown in /var/www/encryptTest.php on line 12
我已经使用了示例脚本之一
$key = \Defuse\Crypto\Crypto::CreateNewRandomKey();
$message = 'We are all living in a yellow submarine';
$ciphertext = \Defuse\Crypto\Crypto::Encrypt($message, $key);
$plaintext = \Defuse\Crypto\Crypto::Decrypt($ciphertext, $key);
echo "cipher is $ciphertext<br/> plaintext is $plaintext";
我也试过使用
$key = Crypto::CreateNewRandomKey();
等等,没有更完整的路径,我试过了
use Defuse\Crypto\Crypto;
我收到相同的错误消息。这似乎是一个简单的目录路径问题,但 .phar 的东西超出了我的范围!如果有人能把我推向正确的方向,我将不胜感激。我没有使用作曲家,我真的不想。谢谢。
经过多次试验,我找到了答案,有三个错误,这是工作代码。除其他外,文档教程 (tutorial.md) 省略了教程示例中的 require_once( /...phar) 行,这误导了我。该脚本可以放在网站根目录或任何子目录中,命名空间 类 仍然会被正确调用。原来如此。
<?php
use Defuse\Crypto\Key;
use Defuse\Crypto\Crypto;
require_once "{$_SERVER['DOCUMENT_ROOT']}/defuse/defuse-crypto.phar";
$key = Key::CreateNewRandomKey();
$message = 'Whosebug is great';
$ciphertext = Crypto::Encrypt($message, $key);
$plaintext = Crypto::Decrypt($ciphertext, $key);
echo "cipher is $ciphertext<br/> plaintext is $plaintext";
?>
您需要添加
use Defuse\Crypto\KeyProtectedByPassword;
如果您想使用已存储且受密码保护的密钥,并且
use Defuse\Crypto\File;
如果你想加密文件。作者建议将 .phar 文件放在 /var/www/lib 中,但我已将其与 /var/www/defuse/src 中的源文件一起放在 /var/www/defuse 中。只要在 require_once() 语句中正确引用了 .phar 文件,它就不会影响命名空间的调用方式。
我很想尝试双向加密的 defuse 库。我已将 defuse 目录和子目录复制到我的网站根目录,并将 .phar 文件复制到 /var/www/lib/defuse-crypto.phar,按照 InstallAndVerify.md 的指示。在我的测试脚本中,我包含了
require_once('/var/www/lib/defuse-crypto.phar');
但是我收到以下错误。
Fatal error: Uncaught Error:
Call to undefined method Defuse\Crypto\Crypto::CreateNewRandomKey() in /var/www/encryptTest.php:12
Stack trace: #0 {main} thrown in /var/www/encryptTest.php on line 12
我已经使用了示例脚本之一
$key = \Defuse\Crypto\Crypto::CreateNewRandomKey();
$message = 'We are all living in a yellow submarine';
$ciphertext = \Defuse\Crypto\Crypto::Encrypt($message, $key);
$plaintext = \Defuse\Crypto\Crypto::Decrypt($ciphertext, $key);
echo "cipher is $ciphertext<br/> plaintext is $plaintext";
我也试过使用
$key = Crypto::CreateNewRandomKey();
等等,没有更完整的路径,我试过了
use Defuse\Crypto\Crypto;
我收到相同的错误消息。这似乎是一个简单的目录路径问题,但 .phar 的东西超出了我的范围!如果有人能把我推向正确的方向,我将不胜感激。我没有使用作曲家,我真的不想。谢谢。
经过多次试验,我找到了答案,有三个错误,这是工作代码。除其他外,文档教程 (tutorial.md) 省略了教程示例中的 require_once( /...phar) 行,这误导了我。该脚本可以放在网站根目录或任何子目录中,命名空间 类 仍然会被正确调用。原来如此。
<?php
use Defuse\Crypto\Key;
use Defuse\Crypto\Crypto;
require_once "{$_SERVER['DOCUMENT_ROOT']}/defuse/defuse-crypto.phar";
$key = Key::CreateNewRandomKey();
$message = 'Whosebug is great';
$ciphertext = Crypto::Encrypt($message, $key);
$plaintext = Crypto::Decrypt($ciphertext, $key);
echo "cipher is $ciphertext<br/> plaintext is $plaintext";
?>
您需要添加
use Defuse\Crypto\KeyProtectedByPassword;
如果您想使用已存储且受密码保护的密钥,并且
use Defuse\Crypto\File;
如果你想加密文件。作者建议将 .phar 文件放在 /var/www/lib 中,但我已将其与 /var/www/defuse/src 中的源文件一起放在 /var/www/defuse 中。只要在 require_once() 语句中正确引用了 .phar 文件,它就不会影响命名空间的调用方式。