網絡通信、文件存儲中經常需要交換數據,為了減少網絡通信流量、文件存儲大小以及加密通信規則,經常需要對數據進行雙向加解密以保證數據的安全。
PHP中實現此功能主要需要使用的函數主要是pack及unpack函數
pack
壓縮資料到位字符串之中。
語法: string pack(string format, mixed [args]...);
返回值: 字符串
本函數用來將資料壓縮打包到位的字符串之中。
a - NUL- 字符串填滿[padded string] 將字符串空白以 NULL 字符填滿
A - SPACE- 字符串填滿[padded string]
h – 十六進制字符串,低“四位元”[low nibble first] (低位在前)
H - 十六進制字符串,高“四位元”[high nibble first](高位在前)
c – 帶有符號的字符
C – 不帶有符號的字符
s – 帶有符號的短模式[short](通常是16位,按機器字節順序)
S – 不帶有符號的短模式[short](通常是16位,按機器字節排序)
n -不帶有符號的短模式[short](通常是16位,按大endian字節排序)
v -不帶有符號的短模式[short](通常是16位,按小endian字節排序)
i – 帶有符號的整數(由大小和字節順序決定)
I – 不帶有符號的整數(由大小和字節順序決定)
l– 帶有符號的長模式[long](通常是32位,按機器字節順序)
L – 不帶有符號的長模式[long](通常是32位,按機器字節順序)
N – 不帶有符號的長模式[long](通常是32位,按大edian字節順序)
V– 不帶有符號的長模式[long](通常是32位,按小edian字節順序)
f –浮點(由大小和字節順序決定)
d – 雙精度(由大小和字節順序決定)
x – 空字節[NUL byte]
X- 後面一個字節[Back up one byte](倒回一位)
unpack
解壓縮位字符串資料。
語法: string pack(string format, mixed [args]...);
返回值: 數組
本函數用來將位的字符串的資料解壓縮。本函數和 Perl 的同名函數功能用法完全相同。
案例一、pack實現縮減文件數據存儲大小
[php]
<?php
//存儲整數1234567890
file_put_contents("test.txt", 1234567890);
<?php
//存儲整數1234567890
file_put_contents("test.txt", 1234567890);此時test.txt的文件大小是10byte。注意此時文件大小是10字節,實際占用空間大小是1KB。
上面存儲的整數實際是以字符串形式存儲於文件test.txt中。
但如果以整數的二進制字符串存jy儲,將會縮減至4byte。
[php]
<?php
print_r(unpack("i", file_get_contents("test.txt")));
<?php
print_r(unpack("i", file_get_contents("test.txt")));
案例二、數據加密
以字符串形式存儲一段有意義數據,7-110-abcdefg-117。
字符"-"分割後,第一位表示字符串長度,第二位表示存儲位置,第三位表示實際存儲的字符串,第四位表示結尾位置。
[php]
<?php
file_put_contents("test.txt", "7-110-abcdefg-117");
<?php
file_put_contents("test.txt", "7-110-abcdefg-117");
上述方法缺點:
一、數據存儲大小
二、數據以明文方式存儲,如果是任何敏感信息,都可能造成不安全訪問。
三、文件存儲大小,以不規則方式遞增。
加密:
[php]
<?php
file_put_contents("test.txt", pack("i2a7i1", 7, 110, "abcdefg", 117));
<?php
file_put_contents("test.txt", pack("i2a7i1", 7, 110, "abcdefg", 117));
存儲一段數據,加密格式為:整數2位長度字符串10位長度整數1位長度。
優點:
一、數據大小最優化
二、在不知道"i2a7i1"這樣的壓縮格式時,即使拿到文件,也無法正確讀出二進制文件轉化為明文。
三、數據增加時,文件存儲大小是等量遞增。每次都是以19byte遞增。
案例三、key-value型文件存儲www.2cto.com
存儲生成的文件為兩個:索引文件,數據文件
文件中數據存儲的格式如下圖:
代碼實現:
[php]
<?php
error_reporting(E_ALL);
class fileCacheException extends Exception{
}
//Key-Value型文件存儲
class fileCache{
private $_file_header_size = 14;
private $_file_index_name;
private $_file_data_name;
private $_file_index;//索引文件句柄
private $_file_data;//數據文件句柄
private $_node_struct;//索引結點結構體
private $_inx_node_size = 36;//索引結點大小
public function __construct($file_index="filecache_index.dat", $file_data="filecache_data.dat"){
$this->_node_struct = array(
'next'=>array(1, 'V'),
'prev'=>array(1, 'V'),
'data_offset'=>array(1,'V'),//數據存儲起始位置
'data_size'=>array(1,'V'),//數據長度
'ref_count'=>array(1,'V'),//引用此處,模仿PHP的引用計數銷毀模式
'key'=>array(16,'H*'),//存儲KEY
);
$this->_file_index_name = $file_index;
$this->_file_data_name = $file_data;
if(!file_exists($this->_file_index_name)){
$this->_create_index();
}else{
$this->_file_index = fopen($this->_file_index_name, "rb+");
}
if(!file_exists($this->_file_data_name)){
$this->_create_data();
}else{
$this->_file_data = fopen($this->_file_data_name, "rb+");//二進制存儲需要使用b
}
}
//創建索引文件
private function _create_index(){
$this->_file_index = fopen($this->_file_index_name, "wb+");//二進制存儲需要使用b
if(!$this->_file_index)
throw new fileCacheException("Could't open index file:".$this->_file_index_name);
$this->_index_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php標記防止下載
$this->_index_puts($this->_file_header_size, pack("V1", 0));
}
//創建存儲文件
private function _create_data(){
$this->_file_data = fopen($this->_file_data_name, "wb+");//二進制存儲需要使用b
if(!$this->_file_index)
throw new fileCacheException("Could't open index file:".$this->_file_data_name);
$this->_data_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php標記防止下載
}
private function _index_puts($offset, $data, $length=false){
fseek($this->_file_index, $offset);
if($length)
fputs($this->_file_index, $data, $length);
else
fputs($this->_file_index, $data);
}
private function _data_puts($offset, $data, $length=false){
fseek($this->_file_data, $offset);
if($length)
fputs($this->_file_data, $data, $length);
else
fputs($this->_file_data, $data);
}
/**
* 文件鎖
* @param $is_block 是否獨占、阻塞鎖
*/
private function _lock($file_res, $is_block=true){
flock($file_res, $is_block ? LOCK_EX : LOCK_EX|LOCK_NB);
}
private function _unlock($file_res){
flock($file_res, LOCK_UN);
}
public function add($key, $value){
$key = md5($key);
$value = serialize($value);
$this->_lock($this->_file_index, true);
$this->_lock($this->_file_data, true);
fseek($this->_file_index, $this->_file_header_size);
list(, $index_count) = unpack('V1', fread($this->_file_index, 4));
$data_size = filesize($this->_file_data_name);
fseek($this->_file_data, $data_size);
$value_size = strlen($value);
$this->_data_puts(filesize($this->_file_data_name), $value);
$node_data =
pack("V1V1V1V1V1H32", ($index_count==0) ? 0 : $index_count*$this->_inx_node_size, 0, filesize($this->_file_data_name), strlen($value), 0, $key);
$index_count++;
$this->_index_puts($this->_file_header_size, $index_count, 4);
$this->_index_puts($this->get_new_node_pos($index_count), $node_data);
$this->_unlock($this->_file_data);
$this->_unlock($this->_file_index);
}
public function get_new_node_pos($index_count){
return $this->_file_header_size + 4 + $this->_inx_node_size * ($index_count-1);
}
public function get_node($key){
$key = md5($key);
fseek($this->_file_index, $this->_file_header_size);
$index_count = fread($this->_file_index, 4);
if($index_count>0) {
for ($i=0; $i < $index_count ; $i++) {
fseek($this->_file_index, $this->_file_header_size + 4 + $this->_inx_node_size * $i);
$data = fread($this->_file_index, $this->_inx_node_size);
$node = unpack("V1next/V1prev/V1data_offset/V1data_size/V1ref_count/H32key", $data);
if($key == $node['key']){
return $node;
}
}
}else{
return null;
}
}
public function get_data($offset, $length){
fseek($this->_file_data, $offset);
return unserialize(fread($this->_file_data, $length));
}
}
//使用方法
$cache = new fileCache();
$cache->add('abcdefg' , 'testabc');
$data = $cache->get_node('abcdefg');
print_r($data);
echo $cache->get_data($data['data_offset'], $data['data_size']);
<?php
error_reporting(E_ALL);
class fileCacheException extends Exception{
}
//Key-Value型文件存儲
class fileCache{
private $_file_header_size = 14;
private $_file_index_name;
private $_file_data_name;
private $_file_index;//索引文件句柄
private $_file_data;//數據文件句柄
private $_node_struct;//索引結點結構體
private $_inx_node_size = 36;//索引結點大小
public function __construct($file_index="filecache_index.dat", $file_data="filecache_data.dat"){
$this->_node_struct = array(
'next'=>array(1, 'V'),
'prev'=>array(1, 'V'),
'data_offset'=>array(1,'V'),//數據存儲起始位置
'data_size'=>array(1,'V'),//數據長度
'ref_count'=>array(1,'V'),//引用此處,模仿PHP的引用計數銷毀模式
'key'=>array(16,'H*'),//存儲KEY
);
$this->_file_index_name = $file_index;
$this->_file_data_name = $file_data;
if(!file_exists($this->_file_index_name)){
$this->_create_index();
}else{
$this->_file_index = fopen($this->_file_index_name, "rb+");
}
if(!file_exists($this->_file_data_name)){
$this->_create_data();
}else{
$this->_file_data = fopen($this->_file_data_name, "rb+");//二進制存儲需要使用b
}
}
//創建索引文件
private function _create_index(){
$this->_file_index = fopen($this->_file_index_name, "wb+");//二進制存儲需要使用b
if(!$this->_file_index)
throw new fileCacheException("Could't open index file:".$this->_file_index_name);
$this->_index_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php標記防止下載
$this->_index_puts($this->_file_header_size, pack("V1", 0));
}
//創建存儲文件
private function _create_data(){
$this->_file_data = fopen($this->_file_data_name, "wb+");//二進制存儲需要使用b
if(!$this->_file_index)
throw new fileCacheException("Could't open index file:".$this->_file_data_name);
$this->_data_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php標記防止下載
}
private function _index_puts($offset, $data, $length=false){
fseek($this->_file_index, $offset);
if($length)
fputs($this->_file_index, $data, $length);
else
fputs($this->_file_index, $data);
}
private function _data_puts($offset, $data, $length=false){
fseek($this->_file_data, $offset);
if($length)
fputs($this->_file_data, $data, $length);
else
fputs($this->_file_data, $data);
}
/**
* 文件鎖
* @param $is_block 是否獨占、阻塞鎖
*/
private function _lock($file_res, $is_block=true){
flock($file_res, $is_block ? LOCK_EX : LOCK_EX|LOCK_NB);
}
private function _unlock($file_res){
flock($file_res, LOCK_UN);
}
public function add($key, $value){
$key = md5($key);
$value = serialize($value);
$this->_lock($this->_file_index, true);
$this->_lock($this->_file_data, true);
fseek($this->_file_index, $this->_file_header_size);
list(, $index_count) = unpack('V1', fread($this->_file_index, 4));
$data_size = filesize($this->_file_data_name);
fseek($this->_file_data, $data_size);
$value_size = strlen($value);
$this->_data_puts(filesize($this->_file_data_name), $value);
$node_data =
pack("V1V1V1V1V1H32", ($index_count==0) ? 0 : $index_count*$this->_inx_node_size, 0, filesize($this->_file_data_name), strlen($value), 0, $key);
$index_count++;
$this->_index_puts($this->_file_header_size, $index_count, 4);
$this->_index_puts($this->get_new_node_pos($index_count), $node_data);
$this->_unlock($this->_file_data);
$this->_unlock($this->_file_index);
}
public function get_new_node_pos($index_count){
return $this->_file_header_size + 4 + $this->_inx_node_size * ($index_count-1);
}
public function get_node($key){
$key = md5($key);
fseek($this->_file_index, $this->_file_header_size);
$index_count = fread($this->_file_index, 4);
if($index_count>0) {
for ($i=0; $i < $index_count ; $i++) {
fseek($this->_file_index, $this->_file_header_size + 4 + $this->_inx_node_size * $i);
$data = fread($this->_file_index, $this->_inx_node_size);
$node = unpack("V1next/V1prev/V1data_offset/V1data_size/V1ref_count/H32key", $data);
if($key == $node['key']){
return $node;
}
}
}else{
return null;
}
}
public function get_data($offset, $length){
fseek($this->_file_data, $offset);
return unserialize(fread($this->_file_data, $length));
}
}
//使用方法
$cache = new fileCache();
$cache->add('abcdefg' , 'testabc');
$data = $cache->get_node('abcdefg');
print_r($data);
echo $cache->get_data($data['data_offset'], $data['data_size']);
案例四、socket通信加密
通信雙方都定義好加密格式:
例如:
[php]
LOGIN = array(
'COMMAND'=>array('a30', 'LOGIN'),
'DATA'=>array('a30', 'HELLO')
);
$LOGOUT = array(
'COMMAND'=>array('a30', 'LOGOUT'),
'DATA'=>array('a30', 'GOOD BYE')
);
$LOGIN_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', 1)
);
$LOGOUT_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', time())
);
$LOGIN = array(
'COMMAND'=>array('a30', 'LOGIN'),
'DATA'=>array('a30', 'HELLO')
);
$LOGOUT = array(
'COMMAND'=>array('a30', 'LOGOUT'),
'DATA'=>array('a30', 'GOOD BYE')
);
$LOGIN_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', 1)
);
$LOGOUT_SUCCESS = array(
'COMMAND'=>array('a30', 'LOGIN_SUCCESS'),
'DATA'=>array('V1', time())
);服務器端與客戶端根據解析COMMAND格式,找到對應的DATA解碼方式,得到正確的數據