本文提供兩個可直接複製使用的 PHP 加解密函數 encrypt() 與 authcode(),包含完整參數說明、呼叫範例與輸出展示,適合快速整合至專案中進行參數加密、API 簽名驗證等場景。
/*********************************************************************
* 函數名稱: encrypt
* 功能: 加密/解密字串
* 使用方法:
* 加密: encrypt('str', 'E', 'secret_key');
* 解密: encrypt('encrypted_str', 'D', 'secret_key');
* 參數說明:
* $string : 待處理字串
* $operation: 'E' 加密, 'D' 解密
* $key : 密鑰字串
*********************************************************************/
function encrypt($string, $operation, $key = '') {
$key = md5($key);
$key_length = strlen($key);
$string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
$string_length = strlen($string);
$rndkey = $box = []; $result = '';
for ($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($key[$i % $key_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if ($operation == 'D') {
return (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) ? substr($result, 8) : '';
} else {
return str_replace('=', '', base64_encode($result));
}
}
$id = 132; $key = 'a'; $token = encrypt($id, 'E', $key); echo "加密: $token
"; $decrypted = encrypt($token, 'D', $key); echo "解密: $decrypted
"; // 輸出: // 加密: xxxx // 解密: 132
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : $GLOBALS['discuz_auth_key']);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya . md5($keya . $keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE'
? base64_decode(substr($string, $ckey_length))
: sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = [];
for ($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for ($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if ($operation == 'DECODE') {
if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0)
&& substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
return substr($result, 26);
}
return '';
} else {
return $keyc . str_replace('=', '', base64_encode($result));
}
}
| 參數 | 說明 |
|---|---|
| $string | 明文(加密)或密文(解密) |
| $operation | 'ENCODE' 加密, 'DECODE' 解密 |
| $key | 密鑰,留空則用 $GLOBALS['discuz_auth_key'] |
| $expiry | 有效秒數,0=永久 |
$str = 'abcdef'; $key = 'www.example.com'; // 加密 $encrypted = authcode($str, 'ENCODE', $key, 3600); // 1小時有效 echo "加密: $encrypted
"; // 解密 $decrypted = authcode($encrypted, 'DECODE', $key); echo "解密: $decrypted
"; // 輸出: abcdef // 測試過期 $expired = authcode($str, 'ENCODE', $key, 1); // 1秒 sleep(2); var_dump(authcode($expired, 'DECODE', $key)); // string(0) ""
function encrypt($string, $operation, $key = '') {
$key = md5($key);
$key_length = strlen($key);
$string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
$string_length = strlen($string);
$rndkey = $box = []; $result = '';
for ($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($key[$i % $key_length]);
$box[$i] = $i;
}
for ($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp;
}
for ($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if ($operation == 'D') {
return (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) ? substr($result, 8) : '';
} else {
return str_replace('=', '', base64_encode($result));
}
}
$str = 'abc'; $key = 'www.example.com'; $token = encrypt($str, 'E', $key); echo "加密: $token
"; echo "解密: " . encrypt($token, 'D', $key) . "
";
| 需求 | 推薦函數 | 理由 |
|---|---|---|
| 需支援過期時間、同明文產生不同密文 | authcode() | 含動態金鑰、時間戳、完整性校驗 |
| 簡單雙向加密、相容舊系統 | encrypt() (第一版) | 參數直觀、代碼精簡 |
| 新專案、高安全性要求 | openssl_encrypt() AES-256-GCM | 現代標準、硬體加速、AEAD 認證 |
openssl_encrypt() 或 sodium_* 系列提供三個版本的加解密函數,涵蓋簡易加密、Discuz 經典 authcode(含過期機制)與參數順序變體。可直接複製整合,但請依專案安全等級選擇適當演算法,新專案優先採用現代 AEAD 加密。