1

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 !

Community
  • 1
  • 1
Jean
  • 59
  • 1
  • 8

4 Answers4

2

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?

Renaud C.
  • 535
  • 2
  • 14
2

$ciphertext will never be === to $plaintext, just a heads up.

To get the right namespace, I had to use:

$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);
Lucius
  • 1,246
  • 1
  • 8
  • 21
TheRealMrCrowley
  • 976
  • 7
  • 24
1

I literally had the same problem and just fixed it.

If you download "defuse-crypto.phar" from the GitHub repository ( https://github.com/defuse/php-encryption/releases ), then use

require_once("defuse-crypto.phar");

then use

use Defuse\Crypto\whichever_one_you_want_to_load

This works, and I have been trying to get this working for quite some time as well. It's a late answer but anyone who tries this, it does work.

You are probably not supposed to do it this way, but if I find a better way that you are supposed to do then I will edit this answer.

  • Thanks--this also seemingly solved my issue after trying to use it installed with Composer. Your method of just plopping the `.phar` in your project directory and `require`-ing it is actually an officially supported [and seemingly-preferred] alternative to the Composer installation: https://github.com/defuse/php-encryption/blob/master/docs/InstallingAndVerifying.md – velkoon Apr 20 '21 at 05:43
0

The SO question referenced by the asker was apparently written against an older version of the package prior to its integration with Composer. [The referenced answer has since been rewritten]

Replace:

require ("Crypto.php");

With:

require('vendor/autoload.php');

And remember, take SO answers with a grain of salt. Just because the answer is accepted doesn't necessarily mean it's either correct or current.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • Hi, unfortunately I obtain the same result and I don't understand why : Fatal error: Class 'Crypto' not found in /home/zideesdubj/www/_tests2enfants/_test_session/php-encryption-master/test.php on line 10 – Jean Jul 01 '15 at 15:25
  • "Whoever wrote that SO answer you referenced doesn't know how to use Composer properly" No, that was written before it was moved to namespaces and integrated with Composer. Check out the v1.2.1 tag and you'll see what I saw when I wrote it. Please don't accuse me of ignorance. – Scott Arciszewski Jul 27 '15 at 18:37
  • 1
    @ScottArciszewski Decreased the sass by at least 50% – Sammitch Jul 27 '15 at 21:02