Over a million developers have joined DZone.

Script I Wrote To Stream A File And Crypt It

·
// Here is a php script I wrote to stream a file and crypt it with a xor operation on the bytes and with a key :

The encryption works very good but the speed is decrease by 2, it is now 520KiB/s. The user is now asked for a md5 password (instead of keeping it in the code directly). There is some part in French because it's my native language so modify it as you want. 


";
    exit;
}
// Get the key in hex
$keychar = $_SERVER['PHP_AUTH_PW'];

// Convert key and set the size of the key
$key = hex2bin($keychar);
$keylength = count($key);
// Teste si la clé est valide en hex
if ($key == "" || $keylength <= 4) {
    askPassword("Clé incorrecte !");
    //echo "Clé incorrecte !
"; exit(); } // Teste si la clé est de longueur d'une puissance de 2 if ( ($keylength%2) != 0) { askPassword("Clé de longueur incorrecte (multiple de 2 uniquement)"); //echo "Clé de longueur incorrecte (puissance de 2 uniquement)
"; exit(); } // Headers header("Content-Type: application/octet-stream; "); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . filesize($file) ."; "); header("filename=\"".$file."\"; "); flush(); // this doesn't really matter. // Opening the file in read-only $fp = fopen($file, "r"); while (!feof($fp)) { // Read a buffer size of the file $buffer = fread($fp, $bufferlength); $j=0; for ($i=0; $i < $bufferlength; $i++) { // The key is read in loop to crypt the whole file if ($i%$keylength == 0) { $j=0; } // Apply a xor operation between the key and the file to crypt // This operation eats a lots of CPU time (Stream at 1MiB/s on my server; Intel E2180) $tmp = pack("C", $key[$j]); $bufferE = ( $buffer[$i]^$tmp); // <==== Le fameux XOR /* echo "
key[".$j."]: "; var_dump($tmp); echo "
buffer[".$i."]: "; var_dump($buffer[$i]); echo "
bufferE: "; var_dump($bufferE); echo "
"; //*/ // Send the encrypted data echo $bufferE; // Clean the memory $bufferE = ""; $j++; } $buffer = ""; flush(); // this is essential for large downloads /* fclose($fp); exit(); //*/ } // Close the file and it's finished fclose($fp); ?>
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}