Tạo và mã hóa file với phép xor theo bytes và một khóa.
Phương pháp mã hóa tốt nhưng tốc độ giảm 2 lần, chỉ 520KB/s.
Sử dụng hàm băm md5 password.
Mã
<?php
// Stream files and encrypt the data on-the-fly
// Settings
// -- File to stream
$file = "FILE_out";
// -- 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++) {
$ta = hexdec($h[2*$a]);
$tb = hexdec($h[(2*$a+1)]);
$r[$a] = (int) (($ta << 4) + $tb);
}
return $r;
}
// Function to send the auth headers
function askPassword($text="Enter the password") {
header('WWW-Authenticate: Basic realm="'. utf8_decode($text) .'"');
header('HTTP/1.0 401 Unauthorized');
return 1;
}
// Key is asked at the first start
if (!isset($_SERVER['PHP_AUTH_PW'])) {
askPassword();
echo "Une clé est nécessaire !<br />";
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 !<br />";
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)<br />";
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 "<br />key[".$j."]: ";
var_dump($tmp);
echo "<br />buffer[".$i."]: ";
var_dump($buffer[$i]);
echo "<br />bufferE: ";
var_dump($bufferE);
echo "<br />";
//*/
// 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);
?>
Nguồn php.net
| Hàm PHP: substr_compare < Lùi | Tiếp theo > Tìm hiểu hàm Function Header trong PHP |
|---|
Tin mới hơn:
- 05/07/2009 16:13 - Đăng nhập vào Yahoo sử dụng giao thức YMSG
- 28/06/2009 21:15 - Viết chương trình download manager đơn giản
- 10/06/2009 16:00 - Lập trình PHP - Đếm số người trực tuyến
- 09/06/2009 23:50 - Chuỗi (xâu) và các hàm xử lý chuỗi trong PHP
- 09/06/2009 16:53 - Hàm PHP: substr_compare
Tin cũ hơn:
- 08/06/2009 22:18 - Tìm hiểu hàm Function Header trong PHP
- 03/06/2009 23:50 - Lập trình PHP, MySQL - Tạo trang đăng nhập Login
- 03/06/2009 22:00 - Lập trình PHP - Tạo 1 shoutbox đơn giản
- 03/06/2009 18:24 - Chuyển hướng Redirection với Drop Down Menu
- 02/06/2009 18:02 - Class: PHP Ajax Voting system
Download Game bóng đá PES8 Việt Nam
Tải game bóng đá FiFa 09
Tải game Dragon ball Z - 7 viên ngọc rồng
Tải Game Bóng Đá Pro Evolution Soccer 6 Việt Nam
Download PES 2011 patch Việt Nam PESVN
Tải Game Bóng Đá FIFA 2009 RIP 1 CD
Download Pro Evolution Soccer 2011
Download Game THE SIMS 3
Tải Game bóng đá PES 2009
Download PES 2011 demo
Thay đổi màn hình đăng nhập Windows 7 bằng tay
Một số hàm hay dùng trong JavaScript
Kiểm soát hoàn toàn trỏ chuột bằng bàn phím với NeatMouse
Choáng với concept của Windows Phone 8
Những đặc điểm ưu việt của Windows Phone
Ứng dụng gọi điện VoIP Viber sắp có mặt trên Windows Phone
Game Diablo 3 sắp có mặt trên Windows Phone Marketplace
37 trang css & xhtml web template miễn phí tuyệt đẹp
Những điều cơ bản cần biết về bàn phím chơi game - Gaming Keyboard
L.A. Noire: The Complete Edition - THETA [Full ISO/Action/2011] [13.37GB]
Download 7554 demo
Download Game PC, cập nhật liên tục (torrent)
PES 2012 ra mắt bản mở rộng đầu tiên vào 11/10
PES2012 - Full version - Ngôn ngữ và bình luận tiếng Anh
PES2012 - Blurry disable làm mịn đồ họa
PES 2011 PATCH 4.1 - TRANSFER UPDATE - RELEASED
Bộ sưu tập free PrestaShop Themes phần 3
Bộ sưu tập theme Prestashop miễn phí phần 2
Top 10 Free PrestaShop Themes
Download Prestashop 1.5
Download PrestaShop 1.4
Bổ sung Memcached vào PrestaShop 1.4
Hướng dẫn tích hợp Ngân Lượng vào PrestaShop
Sửa lỗi invalid carrier or no carrier selected trong PrestaShop
Cài đặt PrestaShop
Thiết kế giao diện PrestaShop Theme
Hướng dẫn cài đặt themes trong PrestaShop
Hướng dẫn cách thêm ngôn ngữ cho PrestaShop
Bộ sưu tập Giao diện Premium PrestaShop Themes chuyên nghiệp
Giới thiệu về website thương mại điện tử 2.0 Prestashop
Hướng dẫn chơi Grand Theft Auto: Vice City phần 1
Nero Burning Rom 9.4.26 Portable - Phần mềm ghi đĩa tốt nhất


