搜尋
首頁後端開發php教程詳解PHP資料壓縮、加解密(pack, unpack)

網路通訊、檔案儲存中經常需要交換數據,為了減少網路通訊流量、檔案儲存大小以及加密通訊規則,經常需要對資料進行雙向加解密以確保資料的安全。
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– 帶有符號的長模式[長](通常是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 
//存储整数1234567890 
file_put_contents("test.txt", 1234567890);

此時test.txt的檔案大小是10byte。注意此時檔案大小是10字節,實際佔用空間大小是1KB。
上面儲存的整數實際上是以字串形式儲存於檔案test.txt中。
但如果以整數的二進位字串存jy儲,將會縮減至4byte。

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


案例二、資料加密
以字串形式儲存一段有意義數據,7-110-abcdefg-117。
字元"-"分割後,第一位表示字串長度,第二位表示儲存位置,第三位表示實際儲存的字串,第四位表示結尾位置。

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

上述方法缺點:
一、資料儲存大小
二、資料以明文方式存儲,如果是任何敏感資訊,都可能造成不安全訪問。
三、檔案儲存大小,以不規則方式遞增。
加密:

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

儲存一段數據,加密格式為:整數2位元長度字串10位元長度整數1位元長度。
優點:
一、資料大小最優化
二、在不知道"i2a7i1"這樣的壓縮格式時,即使拿到文件,也無法正確讀出二進位轉換為明文。
三、資料增加時,檔案儲存大小是等量遞增。每次都是以19byte遞增。

案例三、key-value型文件儲存
。儲存產生的檔案為兩個:索引文件,資料檔案
檔案中資料儲存的格式如下圖:

詳解PHP資料壓縮、加解密(pack, unpack)
程式碼實作:

<?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( 
        &#39;next&#39;=>array(1, &#39;V&#39;), 
        &#39;prev&#39;=>array(1, &#39;V&#39;), 
       &#39;data_offset&#39;=>array(1,&#39;V&#39;),//数据存储起始位置 
       &#39;data_size&#39;=>array(1,&#39;V&#39;),//数据长度 
       &#39;ref_count&#39;=>array(1,&#39;V&#39;),//引用此处,模仿PHP的引用计数销毁模式 
       &#39;key&#39;=>array(16,&#39;H*&#39;),//存储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&#39;t open index file:".$this->_file_index_name); 
 
     $this->_index_puts(0, &#39;<&#39;.&#39;?php exit()?&#39;.&#39;>&#39;);//定位文件流至起始位置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&#39;t open index file:".$this->_file_data_name); 
 
     $this->_data_puts(0, &#39;<&#39;.&#39;?php exit()?&#39;.&#39;>&#39;);//定位文件流至起始位置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(&#39;V1&#39;, 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[&#39;key&#39;]){ 
             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(&#39;abcdefg&#39; , &#39;testabc&#39;); 
$data = $cache->get_node(&#39;abcdefg&#39;); 
print_r($data); 
echo $cache->get_data($data[&#39;data_offset&#39;], $data[&#39;data_size&#39;]);

案例四、socket通訊加密
通訊雙方都定義好加密格式:
例如:

$LOGIN = array( 
   &#39;COMMAND&#39;=>array(&#39;a30&#39;, &#39;LOGIN&#39;), 
   &#39;DATA&#39;=>array(&#39;a30&#39;, &#39;HELLO&#39;) 
); 
 
$LOGOUT = array( 
   &#39;COMMAND&#39;=>array(&#39;a30&#39;, &#39;LOGOUT&#39;), 
   &#39;DATA&#39;=>array(&#39;a30&#39;, &#39;GOOD BYE&#39;) 
); 
 
$LOGIN_SUCCESS = array( 
   &#39;COMMAND&#39;=>array(&#39;a30&#39;, &#39;LOGIN_SUCCESS&#39;), 
   &#39;DATA&#39;=>array(&#39;V1&#39;, 1) 
); 
 
$LOGOUT_SUCCESS = array( 
   &#39;COMMAND&#39;=>array(&#39;a30&#39;, &#39;LOGIN_SUCCESS&#39;), 
   &#39;DATA&#39;=>array(&#39;V1&#39;, time()) 
);

伺服器端與客戶端根據解析COMMAND格式,找到對應的DATA解碼方式,得到正確的資料

 以上就是詳解PHP資料壓縮、加解密(pack, unpack) 的內容,請多相關內容請關注PHP中文網(www.php.cn)!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
PHP電子郵件:分步發送指南PHP電子郵件:分步發送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自動化intifications andMarketingCampaigns.1)設置設置yourphpenvenvironnvironnvironmentwithaweberswithawebserverserververandphp,確保themailfunctionisenabled.2)useabasicscruct

如何通過PHP發送電子郵件:示例和代碼如何通過PHP發送電子郵件:示例和代碼May 09, 2025 am 12:13 AM

發送電子郵件的最佳方法是使用PHPMailer庫。 1)使用mail()函數簡單但不可靠,可能導致郵件進入垃圾郵件或無法送達。 2)PHPMailer提供更好的控制和可靠性,支持HTML郵件、附件和SMTP認證。 3)確保正確配置SMTP設置並使用加密(如STARTTLS或SSL/TLS)以增強安全性。 4)對於大量郵件,考慮使用郵件隊列系統來優化性能。

高級PHP電子郵件:自定義標題和功能高級PHP電子郵件:自定義標題和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP發送電子郵件的指南使用PHP和SMTP發送電子郵件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP發送郵件可以通過PHPMailer庫實現。 1)安裝並配置PHPMailer,2)設置SMTP服務器細節,3)定義郵件內容,4)發送郵件並處理錯誤。使用此方法可以確保郵件的可靠性和安全性。

使用PHP發送電子郵件的最佳方法是什麼?使用PHP發送電子郵件的最佳方法是什麼?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依賴注入的最佳實踐PHP中依賴注入的最佳實踐May 08, 2025 am 12:21 AM

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

PHP性能調整技巧和技巧PHP性能調整技巧和技巧May 08, 2025 am 12:20 AM

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

PHP電子郵件安全性:發送電子郵件的最佳實踐PHP電子郵件安全性:發送電子郵件的最佳實踐May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版