Monday, 11 February 2019

Read encrypted php files using ajax

I encrypt all my core files using libsodium, but my problem is how to read the php files in decrypted state like calling the file using ajax like automatic decryption.

I'm not sure if its possible.

Its something like this.

Sorry, I'm still exploring on this library

I work on this before but method is wrong, and told me to use libsodium.

Hope you help me.

CODE FOR ENCRYPTION

<?php
    require_once('function.php');
    if(isset($_FILES)){

        $tmp = "tmp/";
        $tmpFiles = browseDir($tmp);

        foreach($tmpFiles as $file){ // iterate files

        if(is_file($tmp.$file))

            unlink($tmp.$file); // delete file

        }

        $secret_key = sodium_crypto_secretstream_xchacha20poly1305_keygen();

        foreach($_FILES['files']['name'] as $key => $value){

            $file = explode(".", $_FILES['files']['name'][$key]);
            $ext = array("php");

            if(in_array($file[1], $ext)){

                $file_name = $file[0].'.'.$file[1];
                $source = $_FILES['files']['tmp_name'][$key];
                $location = $tmp.$file_name;

                $chunk_size = 4096;

                $fd_in = fopen($source, 'rb');
                $fd_out = fopen($location, 'wb');

                list($stream, $header) = sodium_crypto_secretstream_xchacha20poly1305_init_push($secret_key);

                fwrite($fd_out, $header);

                $tag = SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_MESSAGE;

                do {
                    $chunk = fread($fd_in, $chunk_size);
                    if (feof($fd_in)) {
                        $tag = SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL;
                    }
                    $encrypted_chunk = sodium_crypto_secretstream_xchacha20poly1305_push($stream, $chunk, '', $tag);
                    fwrite($fd_out, $encrypted_chunk);
                } while ($tag !== SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL);

                fclose($fd_out);
                fclose($fd_in);

            }
        }
    }
?>



from Read encrypted php files using ajax

No comments:

Post a Comment