使用Ajax

时间:2019-02-08 05:24:24

标签: php ajax encryption cryptography libsodium

我使用libsodium对我的所有核心文件进行加密,但是我的问题是如何在解密状态下读取php文件,例如使用自动解密之类的ajax调用该文件。

我不确定是否可能。

类似于this

对不起,我仍在探索这个图书馆

我以前work on this,但方法错误,并告诉我使用libsodium。

希望你能帮助我。

加密

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

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

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

        if(is_file($tmp.$file))

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


        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;

                $password = 'password';
                $chunk_size = 4096;

                $alg = SODIUM_CRYPTO_PWHASH_ALG_DEFAULT;
                $opslimit = SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE;
                $memlimit = SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE;
                $salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES);

                $secret_key = sodium_crypto_pwhash(SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES,
                                                $password, $salt, $opslimit, $memlimit, $alg);

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

                fwrite($fd_out, pack('V', $alg));
                fwrite($fd_out, pack('V', $opslimit));
                fwrite($fd_out, pack('V', $memlimit));
                fwrite($fd_out, $salt);

                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);

            }
        }
    }
?>

解密

<?php

$password = 'password';
$encrypted_file = 'enc/inc.php';
$decrypted_file = 'dec/dec.php';
$chunk_size = 4096;

$fd_in = fopen($encrypted_file, 'rb');
$fd_out = fopen($decrypted_file, 'wb');

$alg = unpack('V', fread($fd_in, 4))[1];
$opslimit = unpack('V', fread($fd_in, 4))[1];
$memlimit = unpack('V', fread($fd_in, 4))[1];
$salt = fread($fd_in, SODIUM_CRYPTO_PWHASH_SALTBYTES);

echo $alg. ' alg';
echo $opslimit. 'ops';
echo $memlimit. 'mem';

$header = fread($fd_in, SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES);

$secret_key = sodium_crypto_pwhash(SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES,
                                   $password, $salt, $opslimit, $memlimit, $alg);

$stream = sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $secret_key);
do {
    $chunk = fread($fd_in, $chunk_size + SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES);
    $res = sodium_crypto_secretstream_xchacha20poly1305_pull($stream, $chunk);
    if ($res === FALSE) {
       break;
    }
    list($decrypted_chunk, $tag) = $res;
    fwrite($fd_out, $decrypted_chunk);
} while (!feof($fd_in) && $tag !== SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL);
$ok = feof($fd_in);

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

if (!$ok) {
    die('Invalid/corrupted input');
}

0 个答案:

没有答案