Reputation: 103
I'm not sure how to word this so please bear with me.
(Using: PHP 5.4) I created a PHP Function to encrypt / decrypt php files, right now I can read in a php source file & encrypt it. I can also decrypt the file again back to original PHP source code so up until this point everything works perfect.. BUT..
on 'decrypt' $action, I want to decrypt the file into memory and be able to call the function sayHello(), without writing it to a file (I can already do that) by either using memory or a stream, or something I'm not familiar with. not even a tmp file, or no $_SESSION.
The whole purpose of this, I am making a custom compiler for my php code I want to run decrypt($fileName) and be able to call functions or Constants out of it while in memory, and maybe a function to take it outta memory somehow, I will only be using it on files at most like 2kb
If I use a tmp file then there is no sense encrypting it, because they can just use that, defeating the whole purpose, same with $_SESSION. There has to be a way to read a file into memory, include it somehow, and be able to call functions out of it as if it were a regular php file.
example..
function sayHello() {
echo 'hello world';
}
now the file that does the actual encryption / decryption
function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'faj6mZqjGYlUuj6TA4uQz';
$secret_iv = 'a6ZcFzXKimxogfo6yHBR';
$noExt = basename($string, ".php"); <br />
// hash
$key = hash('sha256', $secret_key); <br />
// iv - encrypt method AES-256-CBC expects 16 bytes - else get warning
$iv = substr(hash('sha256', $secret_iv), 0, 16); <br />
if ($action == 'encrypt') {
$fileName = file_get_contents($string);
$output = openssl_encrypt($fileName, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
$output = file_put_contents($noExt."_ENC.php", $output);
}
else if ($action == 'decrypt') {
$fileName = file_get_contents($string);
$output = openssl_decrypt(base64_decode($fileName), $encrypt_method, $key, 0, $iv);
// Below line works if UnCommented for decrypting to file. (not what i want)
// $output = file_put_contents("test_ENC_DECRYPTED.php", $output);
// <== THIS IS WHERE I WANT TO CALL A FUNCTION IN MEMORY, NOT FROM FILE
}
return $output;
}
*EDIT:
Original Code Located here.. (I modified it to accept files)
http://naveensnayak.wordpress.com/2013/03/12/simple-php-encrypt-and-decrypt/
*NOTE:
I don't want to use any other dependencies or 3rd party plug-ins or no compilers or extensions. I want this to be portable & work on any PHP 5.3+ version. I realize I said I want to run decrypt($fileName) and I didn't show code for that file, that is because I am going to break this function down and
only the decrypter is going to be used after files are encrypted.
Upvotes: 0
Views: 194
Reputation: 781059
Use eval:
eval(encrypt_decrypt('decrypt', $filename));
eval
executes the code in its argument, just as if it were a file that was loaded.
There are some gotchas, though: if it tries to use variables that reference the script filename, they'll get the script that's doing the decrypting, not the file that contained the encrypted or original decrypted script.
Upvotes: 2