In development, we often use encryption algorithms to process data. One powerful function in PHP—pack()—is commonly used for formatting binary data. While it’s not a traditional encryption function, pack() can be cleverly applied to help us build the “packaging logic” for custom encryption algorithms.
The pack() function is used to pack data into a binary string according to a specified format. Its basic syntax is as follows:
string pack ( string $format , mixed $values [, mixed $... ] )
Here, $format is a format string used to specify how to pack the subsequent $values parameters. For example:
$binary = pack("N", 123456); // Pack the number as a 32-bit unsigned long (big-endian)
We can pack data into binary form and then “encrypt” it through base64 encoding or custom conversion rules. While this method doesn’t compare to professional encryption algorithms like AES or RSA, it offers a lightweight protection strategy in certain non-critical data scenarios.
Suppose the data we want to encrypt consists of the following parts:
User ID (integer)
Timestamp (integer)
Operation type (1-byte identifier)
Fixed checksum (string)
Here’s how we can use pack() to pack this data:
function custom_encrypt($userId, $actionType) {
$timestamp = time();
$checkCode = 'm66'; // Fixed checksum
$binaryData = pack("NNCa3", $userId, $timestamp, $actionType, $checkCode);
// Use base64 encoding to generate the final "encrypted" string
return base64_encode($binaryData);
}
The format string "NNCa3" means:
N: 32-bit unsigned long (userId)
N: 32-bit unsigned long (timestamp)
C: 8-bit unsigned char (actionType)
a3: 3-byte string (checkCode)
We can test this function:
echo custom_encrypt(1024, 5);
The output will be a packed and encoded string, like:
AAAAAAgAAZ8ZziUAbTY2AA==
The encrypted data can be unpacked back to its original content using the unpack() function:
function custom_decrypt($encoded) {
$binaryData = base64_decode($encoded);
$data = unpack("NuserId/Ntimestamp/CactionType/a3checkCode", $binaryData);
}
Usage example:
$encoded = custom_encrypt(2048, 3);
$decoded = custom_decrypt($encoded);
print_r($decoded);
The output will look like:
Array
(
[userId] => 2048
[timestamp] => 1716821234
[actionType] => 3
[checkCode] => m66
)
Since the final result is a base64 encoded string, we can safely attach it to a URL:
$url = "https://m66.net/api/receive.php?token=" . urlencode(custom_encrypt(2048, 1));
The receiving end can decrypt it similarly:
$token = $_GET['token'];
$data = custom_decrypt($token);
pack() is a powerful but often overlooked tool in PHP. It provides precise control over low-level binary data. In scenarios where lightweight encryption or data encapsulation is needed, using it in conjunction with base64_encode() and unpack() can quickly help build custom encryption transmission logic.
However, this approach is not suitable for handling sensitive information. It is more of a lightweight encoding/obfuscation solution rather than a true security encryption method. For handling sensitive data like user passwords or financial information, please use standard encryption algorithms like OpenSSL or libsodium.
By mastering the various formatting symbols of pack(), you can even simulate the packaging process of network protocols and apply it effectively in custom API designs.