note 89395 added to function.header
| From: | (axel_at_james-b_dot_ch) | Date: | Thu, 05 Mar 2009 20:23:34 +0000 |
| Subject: | note 89395 added to function.header | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151537@lists.php.net to get a copy of this message | ||
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 :
<?php
/* You can use this code as you want ! Enjoy */
// Stream files and encrypt the data on-the-fly
// Settings
// -- File to stream
$file = "input.mp3";
// -- Reading buffer
$bufferlength = 3840;
// -- Key in hex
$keychar = "9cdfb439c7876e703e307864c9167a15";
// Function: Convertion hex key in a string into binary
function hex2bin($h) {
if (!is_string($h)) return null;
$r = array();
for ($a=0; ($a*2)<strlen($h); $a++) {
//$r.=chr(hexdec($h{$a}.$h{($a+1)}));
$r[$a] = (binary) (hexdec($h[2*$a].$h[(2*$a+1)]));
}
return $r;
}
// Headers
$headers = "";
$headers .= "Content-Type: application/octet-stream; ";
$headers .= "Content-Length: " . filesize($file) ."; ";
$headers .= "filename=\"".$file."\"; ";
header($headers);
flush(); // this doesn't really matter.
// Convert key and set the size of the key
$key = hex2bin($keychar);
$keylength = strlen($key);
// 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)
$bufferE[$i] = $buffer[$i]^$key[$j];
// Send the encrypted data
echo $bufferE[$i];
// Clean the memory
$bufferE[$i] = "";
$j++;
}
flush(); // this is essential for large downloads
}
// Close the file and it's finished
fclose($fp);
?>
I hope this will be helpful to someone.
----
Server IP: 89.31.146.176
Probable Submitter: 62.203.106.67
----
Manual Page -- http://www.php.net/manual/en/function.header.php
Edit -- https://master.php.net/note/edit/89395
Del: integrated -- https://master.php.net/note/delete/89395/integrated
Del: useless -- https://master.php.net/note/delete/89395/useless
Del: bad code -- https://master.php.net/note/delete/89395/bad+code
Del: spam -- https://master.php.net/note/delete/89395/spam
Del: non-english -- https://master.php.net/note/delete/89395/non-english
Del: in docs -- https://master.php.net/note/delete/89395/in+docs
Del: other reasons-- https://master.php.net/note/delete/89395
Reject -- https://master.php.net/note/reject/89395
Search -- https://master.php.net/manage/user-notes.php