search
HomeBackend DevelopmentPHP TutorialDetailed explanation of PHP data compression, encryption and decryption (pack, unpack)

Data is often exchanged in network communications and file storage. In order to reduce network communication traffic, file storage size, and encrypted communication rules, it is often necessary to perform two-way encryption and decryption of data to ensure data security.
The main functions needed to implement this function in PHP are mainly the pack and unpack functions

pack
Compress the data into a string.
Syntax: string pack(string format, mixed [args]...);
Return value: String
This function is used to compress and pack the data into a string.
a - NUL - Padded string [padded string] Pad the string blanks with NULL characters
A - SPACE - Padded string [padded string]
h - Hexadecimal string , low "nibble first" (low nibble first)
H - hexadecimal string, high "nibble" [high nibble first] (high nibble first)
c – Signed character
C – Unsigned character
s – Signed short pattern [short] (usually 16 bits, in machine byte order)
S – Without Signed short mode [short] (usually 16 bits, machine byte sorted)
n - Unsigned short mode [short] (usually 16 bits, big endian byte sorted)
v - unsigned short pattern [short] (usually 16 bits, little endian byte sorted)
i - signed integer (determined by size and byte order)
I - not signed integer (determined by size and byte order)
l – signed long pattern [long] (usually 32 bits, in machine byte order)
L – unsigned long mode [long] (usually 32 bits, in machine byte order)
N – unsigned long mode [long] (usually 32 bits, in big edian byte order)
V – unsigned long mode [long] (usually 32 bits, in little edian byte order)
f – floating point (determined by size and byte order)
d – double (determined by size and byte order) Determined by byte order)
x – NULL byte [NUL byte]
X- Back up one byte [Back up one byte]

unpack
Decompress bit string data.
Syntax: string pack(string format, mixed [args]...);
Return value: Array
This function is used to decompress the bit string data. This function has exactly the same functionality and usage as the Perl function of the same name.

Case 1, pack implements reduction of file data storage size

<?php 
//存储整数1234567890 
file_put_contents("test.txt", 1234567890);

At this time, the file size of test.txt is 10byte. Note that the file size is 10 bytes at this time, and the actual space occupied is 1KB.
The integers stored above are actually stored in the file test.txt in the form of strings.
But if it is stored as an integer binary string, it will be reduced to 4 bytes.

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


Case 2, Data Encryption
Storing a meaningful data in the form of a string, 7-110-abcdefg-117.
After the character "-" is divided, the first digit indicates the length of the string, the second digit indicates the storage location, the third digit indicates the actual stored string, and the fourth digit indicates the end position.

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

Disadvantages of the above method:
1. Data storage size
2. Data is stored in clear text. If it is any sensitive information, it may be unsafe. access.
3. The file storage size increases in an irregular manner.
Encryption:

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

Storage a piece of data, the encryption format is: integer 2-bit length string 10-bit length integer 1-bit length.
Advantages:
1. Optimization of data size
2. When the compression format such as "i2a7i1" is not known, even if the file is obtained, the binary file conversion cannot be read correctly For plain text.
3. When data increases, the file storage size increases by the same amount. Each time it is incremented by 19byte.

Case 3, key-value file storage
The storage generated files are two: index file, data file
The format of data storage in the file is as follows:

Detailed explanation of PHP data compression, encryption and decryption (pack, unpack)
Code implementation:

<?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;]);

Case 4, socket communication encryption
Both parties in the communication have defined the encryption format:
For example:

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

The server and client parse the COMMAND format to find the corresponding DATA decoding method and obtain the correct data

The above is a detailed explanation of PHP data compression and processing Decrypt (pack, unpack) content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.