I am trying to come up with a way to have PHP encrypt a file. I used to just use a PHP system call to run a script that encoded the file:
#!/bin/sh
/usr/bin/openssl aes-256-cbc -a -salt -k $1 -in $2
Argument 1 was the password to use and argument 2 is the data. I then use a second script on a computer to de-crypt the file.
#!/bin/sh
/usr/bin/openssl aes-256-cbc -a -d -salt -k $1 -in $2
This method of encrypting will not work on a production host as the PHP system call is disabled. I also would prefer not the change the decode function if at all possible.
Is there a way to replicate the above encrypt function using only PHP?
As stated above in the comments padding is necessary to make this work. The function below will make a file that can be decrypted on the command line like this:
The test.txt file is created from the output of the ssl_encrypt function below.
Example Usage:
References: http://juan.boxfi.com/2010/03/16/write-openssl-files-in-php/