如何使用'defuse / php-encryption'? [英] How to use 'defuse/php-encryption'?

查看:902
本文介绍了如何使用'defuse / php-encryption'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从GitHub下载了'defuse / php-encryption'。



我尝试了这里创建的例子:如何加密和解密PHP字符串?但是我没有成功。



test.php与Crypto.php在同一个文件夹中:

 <?php 
//这需要https://github.com/defuse/php-encryption
// php composer.phar require defuse / php-encryption
//注意:Crypto :: Encrypt()返回原始二进制文件,因此您可能需要使用base64_encode()和base64_decode()来存储/传输密文以防止编码错误。
ini_set('display_errors',1);使用error_reporting(E_ALL);

require(Crypto.php);

//这样做一次,然后以某种方式存储:
$ key = Crypto :: CreateNewRandomKey();

$ message ='我们都生活在一个黄色的潜艇';

$ ciphertext = Crypto :: Encrypt($ message,$ key);
$ plaintext = Crypto :: Decrypt($ ciphertext,$ key);


if($ ciphertext === $ plaintext)
{echo< br>< font color = green>操作cryp - décryptOK !!!!< ; / font>< br>< br>< br> $ ciphertext === $ plaintext< br>< br>;}
else
{echo< br> ; font color = red>操作cryp - décryptNOK !!!!< / font>< br>< br> $ ciphertext === $ plaintext< br>< br>;}

?>

结果是:


致命错误:类别'Crypto'未在
/home/zideesdubj/www/_tests2enfants/_test_session/php-encryption-master/php-encryption-master/src/test.php找到第10行

在您的帮助下,我替换了require(Crypto.php);通过require('php-encryption-master / autoload.php');
但是结果相同:


致命错误:在第10行/home/zideesdubj/www/_tests2enfants/_test_session/php-encryption-master/test.php中找不到类'Crypto'


你有没有任何基本的例子来使用'defuse / php-encryption'?



我明白这是一个虚拟的问题,但我不在网上找到任何答案或教程。



谢谢!

解决方案

加密类命名空间,您是否尝试添加

 使用\Defuse\Crypto\Crypto; 

在PHP脚本的开头使用正确的命名空间?


I've dowloaded 'defuse/php-encryption' from GitHub.

I tried the exemple founded here : How do you Encrypt and Decrypt a PHP String? but I didn't succeed.

"test.php" is in the same folder with "Crypto.php" :

<?php
// This requires https://github.com/defuse/php-encryption
// php composer.phar require defuse/php-encryption
// Note: Crypto::Encrypt() returns raw binary, so you may want to use base64_encode() and base64_decode()for storing/transmitting ciphertexts to prevent encoding bugs.
ini_set('display_errors', 1); error_reporting(E_ALL);

require ("Crypto.php");

// Do this once then store it somehow:
$key = Crypto::CreateNewRandomKey();

$message = 'We are all living in a yellow submarine';

$ciphertext = Crypto::Encrypt($message, $key);
$plaintext = Crypto::Decrypt($ciphertext, $key);


if ($ciphertext === $plaintext)
 {echo "<br><font color=green>Operation cryp - décrypt OK !!!!</font><br><br><br>$ciphertext === $plaintext<br><br>";}
else
 {echo "<br><font color=red>Operation cryp - décrypt NOK !!!!</font><br><br>$ciphertext === $plaintext<br><br>";}

?>

The result is :

Fatal error: Class 'Crypto' not found in /home/zideesdubj/www/_tests2enfants/_test_session/php-encryption-master/php-encryption-master/src/test.php on line 10

With your help, I replaced "require ("Crypto.php");" by "require('php-encryption-master/autoload.php'); But it's the same result :

Fatal error: Class 'Crypto' not found in /home/zideesdubj/www/_tests2enfants/_test_session/php-encryption-master/test.php on line 10

Have you got any basic example to use 'defuse/php-encryption' ?

I understand this is a question from a dummy but I don't find any answer or tutorial on the web.

Thanks !

解决方案

The class Crypto is namespaced. Have you tried adding

use \Defuse\Crypto\Crypto;

at the start of your php script to use the correct namespace?

这篇关于如何使用'defuse / php-encryption'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆