-3

So I'm trying to encrypt and echo a file with openssl. Getting my file contents works, but after I call the function to encrypt, it doesn't echo anything, as if it were a syntax error.

$file = file_get_contents($filename);
//echo $file; // works
$encfile = openssl_encrypt($file, $encmethod, $enckey, 0, $iv);
echo $encfile;

Yes, all my encryption keys and everything are valid. Echoing anything after the encryption doesn't work. If anyone knows what's wrong please let me know.

  • 2
    Check your error log. – zerkms Apr 05 '18 at 00:59
  • Tell us, what are the values of `$encmethod`, and `$iv`. Also, what is the **byte length** of `$enckey`? Also as requested, check your error log. – Matt Clark Apr 05 '18 at 01:04
  • Do you [even have OpenSSL installed with PHP](https://stackoverflow.com/questions/11525524/why-cant-i-use-openssl-encrypt)? – Matt Clark Apr 05 '18 at 01:05
  • @MattClark `$encmethod = "AES-256-CBC";` $iv is 16 bytes $enckey is 32 and theyre based off of time and worked fine with a smaller file previously. –  Apr 05 '18 at 01:06
  • so, what did the logs reveal after what @zerk asked , *anything?* If you don't have access to logs, then set error reporting to catch and display. What version of PHP also? – Funk Forty Niner Apr 05 '18 at 01:22
  • @FunkFortyNiner `PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 14554477 bytes) in /var/www/html/test.php on line 121: /usr/local/cpanel/cgi-sys/ea-php56` So I'm assuming the file size is too large. I guess I will need to allow PHP more memory. –  Apr 05 '18 at 01:29
  • Probably, or you need to find out if there are too many resources running. That, and try increasing it and the max upload file in the .ini files. – Funk Forty Niner Apr 05 '18 at 01:31

2 Answers2

1

I allowed php more memory with

ini_set('memory_limit','512M');

And now it works like a charm.

  • I use `ini_set('memory_limit','3G');` on my server lol, really. But it was only for one time, Ususall PHP only has `1G` but I had to pull 130million rows out of the DB and move them to MongoDB ... lol – ArtisticPhoenix Apr 05 '18 at 01:43
0

I wouldn't add more memory.

I faced a similar issue recently.

How did I fix it, I encrypted chunks of the file at a time a few MB, then I separated the base64 encoded chunks with a : which doesn't appear in base64.

Then when you decode it you read it tell you get the the : decrypt it and move on to the next chunk.

That way you stay well below the memory limit ... :)

See this answer I posted a few days ago KLICK

-note- I use PHPSecLib for AES, we were already using it for sFTP, and I had to update from mycrypt for our Upcoming move to PHP7.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38