成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁(yè) ? 技術(shù)教程 ? 詳解PHP數(shù)據(jù)壓縮、加解密(pack, unpack)
全站頭部文字 我要出現(xiàn)在這里
詳解PHP數(shù)據(jù)壓縮、加解密(pack, unpack) 703 2024-01-18   

網(wǎng)絡(luò)通信、文件存儲(chǔ)中經(jīng)常需要交換數(shù)據(jù),為了減少網(wǎng)絡(luò)通信流量、文件存儲(chǔ)大小以及加密通信規(guī)則,經(jīng)常需要對(duì)數(shù)據(jù)進(jìn)行雙向加解密以保證數(shù)據(jù)的安全。

PHP中實(shí)現(xiàn)此功能主要需要使用的函數(shù)主要是pack及unpack函數(shù)

pack

壓縮資料到位字符串之中。

語(yǔ)法: string pack(string format, mixed [args]...);

返回值: 字符串

本函數(shù)用來(lái)將資料壓縮打包到位的字符串之中。

a - NUL- 字符串填滿[padded string] 將字符串空白以 NULL 字符填滿

A - SPACE- 字符串填滿[padded string]

h ? 十六進(jìn)制字符串,低“四位元”[low nibble first] (低位在前)

H - 十六進(jìn)制字符串,高“四位元”[high nibble first](高位在前)

c ? 帶有符號(hào)的字符

C ? 不帶有符號(hào)的字符

s ? 帶有符號(hào)的短模式[short](通常是16位,按機(jī)器字節(jié)順序)

S ? 不帶有符號(hào)的短模式[short](通常是16位,按機(jī)器字節(jié)排序)

n -不帶有符號(hào)的短模式[short](通常是16位,按大endian字節(jié)排序)

v -不帶有符號(hào)的短模式[short](通常是16位,按小endian字節(jié)排序)

i ? 帶有符號(hào)的整數(shù)(由大小和字節(jié)順序決定)

I ? 不帶有符號(hào)的整數(shù)(由大小和字節(jié)順序決定)

l? 帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按機(jī)器字節(jié)順序)

L ? 不帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按機(jī)器字節(jié)順序)

N ? 不帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按大edian字節(jié)順序)

V? 不帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按小edian字節(jié)順序)

f ?浮點(diǎn)(由大小和字節(jié)順序決定)

d ? 雙精度(由大小和字節(jié)順序決定)

x ? 空字節(jié)[NUL byte]

X- 后面一個(gè)字節(jié)[Back up one byte](倒回一位)

unpack

解壓縮位字符串資料。

語(yǔ)法: string pack(string format, mixed [args]...);

返回值: 數(shù)組

本函數(shù)用來(lái)將位的字符串的資料解壓縮。本函數(shù)和 Perl 的同名函數(shù)功能用法完全相同。

案例一、pack實(shí)現(xiàn)縮減文件數(shù)據(jù)存儲(chǔ)大小

<?php 
//存儲(chǔ)整數(shù)1234567890 
file_put_contents("test.txt", 1234567890); 

此時(shí)test.txt的文件大小是10byte。注意此時(shí)文件大小是10字節(jié),實(shí)際占用空間大小是1KB。

上面存儲(chǔ)的整數(shù)實(shí)際是以字符串形式存儲(chǔ)于文件test.txt中。

但如果以整數(shù)的二進(jìn)制字符串存jy儲(chǔ),將會(huì)縮減至4byte。

<?php 
print_r(unpack("i", file_get_contents("test.txt"))); 

案例二、數(shù)據(jù)加密

以字符串形式存儲(chǔ)一段有意義數(shù)據(jù),7-110-abcdefg-117。

字符"-"分割后,第一位表示字符串長(zhǎng)度,第二位表示存儲(chǔ)位置,第三位表示實(shí)際存儲(chǔ)的字符串,第四位表示結(jié)尾位置。

<?php 
file_put_contents("test.txt", "7-110-abcdefg-117"); 

上述方法缺點(diǎn):

一、數(shù)據(jù)存儲(chǔ)大小

二、數(shù)據(jù)以明文方式存儲(chǔ),如果是任何敏感信息,都可能造成不安全訪問(wèn)。

三、文件存儲(chǔ)大小,以不規(guī)則方式遞增。

加密:

<?php 
file_put_contents("test.txt", pack("i2a7i1", 7, 110, "abcdefg", 117)); 

存儲(chǔ)一段數(shù)據(jù),加密格式為:整數(shù)2位長(zhǎng)度字符串10位長(zhǎng)度整數(shù)1位長(zhǎng)度。

優(yōu)點(diǎn):

一、數(shù)據(jù)大小最優(yōu)化

二、在不知道"i2a7i1"這樣的壓縮格式時(shí),即使拿到文件,也無(wú)法正確讀出二進(jìn)制文件轉(zhuǎn)化為明文。

三、數(shù)據(jù)增加時(shí),文件存儲(chǔ)大小是等量遞增。每次都是以19byte遞增。

案例三、key-value型文件存儲(chǔ)

存儲(chǔ)生成的文件為兩個(gè):索引文件,數(shù)據(jù)文件

文件中數(shù)據(jù)存儲(chǔ)的格式如下圖:

查看圖片

代碼實(shí)現(xiàn):

<?php 
error_reporting(E_ALL); 
 
class fileCacheException extends Exception{ 
 
} 
 
//Key-Value型文件存儲(chǔ) 
class fileCache{ 
   private $_file_header_size = 14; 
   private $_file_index_name; 
   private $_file_data_name; 
   private $_file_index;//索引文件句柄 
   private $_file_data;//數(shù)據(jù)文件句柄 
   private $_node_struct;//索引結(jié)點(diǎn)結(jié)構(gòu)體 
   private $_inx_node_size = 36;//索引結(jié)點(diǎn)大小 
 
   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'),//數(shù)據(jù)存儲(chǔ)起始位置 
       'data_size'=>array(1,'V'),//數(shù)據(jù)長(zhǎng)度 
       'ref_count'=>array(1,'V'),//引用此處,模仿PHP的引用計(jì)數(shù)銷毀模式 
       'key'=>array(16,'H*'),//存儲(chǔ)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+");//二進(jìn)制存儲(chǔ)需要使用b 
     } 
   } 
 
   //創(chuàng)建索引文件 
   private function _create_index(){ 
     $this->_file_index = fopen($this->_file_index_name, "wb+");//二進(jìn)制存儲(chǔ)需要使用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標(biāo)記防止下載 
     $this->_index_puts($this->_file_header_size, pack("V1", 0)); 
   } 
 
 
   //創(chuàng)建存儲(chǔ)文件 
   private function _create_data(){ 
     $this->_file_data = fopen($this->_file_data_name, "wb+");//二進(jìn)制存儲(chǔ)需要使用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標(biāo)記防止下載 
   } 
 
   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 是否獨(dú)占、阻塞鎖 
   */ 
   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通信加密

通信雙方都定義好加密格式:

例如:

$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()) 
); 

服務(wù)器端與客戶端根據(jù)解析COMMAND格式,找到對(duì)應(yīng)的DATA解碼方式,得到正確的數(shù)據(jù)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持綠夏網(wǎng)。


PHP

主站蜘蛛池模板: 影片 - theav| 周秀娜全部三级视频| 演员于晓光| va在线观看视频| 硅酸钙板厂家联系方式| 城市风云儿| 电影《天才》| 隐藏的歌手中国版全集| 二年级aab词语| 黎明电影| 周超个人资料简介| 张静芝| 荒岛大逃亡电影在线观看| 《最美的青春》演员表| 潘雨辰主演的电视剧大全| 沈月个人详细简历| 徐若晗个人简历资料| 什么是实现碳达峰碳中和目标的基础路径| 新红楼梦电影| 国内自拍99| 猎魔人第一季| 绝对权力全集免费观看| 打男生军人光屁股的网站视频| 孕早期不能吃什么| 记忆碎片剧情解析| 怒放的生命简谱| 诺斯费拉图2024| 欧美喜剧电影| 拔萝卜电影| 黄视频在线播放| 辰巳ゆい| 薛昊婧演过的电视剧| 电影《皮囊》| 苏晓电视剧叫什么名字的| 金粉蝶| 我的丁香结| 夏的词语| 天云山传奇 电影| 黑暗欲望| 小伙捡了一沓钱完整版| 高达uce|

!!!站長(zhǎng)長(zhǎng)期在線接!!!

網(wǎng)站、小程序:定制開(kāi)發(fā)/二次開(kāi)發(fā)/仿制開(kāi)發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長(zhǎng)微信:lxwl520520

站長(zhǎng)QQ:1737366103