headerutf php_PHP: header - Manual

这段PHP脚本用于读取文件并使用XOR运算进行加密,同时通过MD5密码进行身份验证。加密效果良好,但速度降低到520KiB/s。用户现在需要输入MD5密码而不是直接在代码中硬编码。脚本包含了错误检查,如无效或长度不符的密钥,并设置合适的缓冲区大小和读取文件的方式。
摘要由CSDN通过智能技术生成

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.

// Settings

// -- File to stream$file="FILE_out";// -- Reading buffer$bufferlength=3840;// -- Key in hex

//$keychar = "9cdfb439c7876e703e307864c9167a15";

// Function: Convertion hex key in a string into binaryfunctionhex2bin($h) {

if (!is_string($h)) returnnull;$r= array();

for ($a=0; ($a*2)

}

return$r;

}// Function to send the auth headersfunctionaskPassword($text="Enter the password") {header('WWW-Authenticate: Basic realm="'.utf8_decode($text) .'"');header('HTTP/1.0 401 Unauthorized');

return1;

}// Key is asked at the first startif (!isset($_SERVER['PHP_AUTH_PW'])) {askPassword();

echo"Une clé est nécessaire !
";

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 hexif ($key==""||$keylength<=4) {askPassword("Clé incorrecte !");//echo "Clé incorrecte !
";exit();

}// Teste si la clé est de longueur d'une puissance de 2if ( ($keylength%2) !=0) {askPassword("Clé de longueur incorrecte (multiple de 2 uniquement)");//echo "Clé de longueur incorrecte (puissance de 2 uniquement)
";exit();

}// Headersheader("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

}// 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 dataecho$bufferE;// Clean the memory$bufferE="";$j++;

}$buffer="";flush();// this is essential for large downloads

/*

fclose($fp);

exit();

//*/}// Close the file and it's finishedfclose($fp);?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值