diff --git a/shopxo/app/plugins/vr_ticket/service/BaseService.php b/shopxo/app/plugins/vr_ticket/service/BaseService.php index 0687362..e218c2e 100644 --- a/shopxo/app/plugins/vr_ticket/service/BaseService.php +++ b/shopxo/app/plugins/vr_ticket/service/BaseService.php @@ -342,35 +342,37 @@ class BaseService } /** - * Feistel-8 混淆编码 + * 混淆编码(HMAC-XOR,保证可逆) * - * 位分配:L=19bit, R=17bit(凑满36bit) - * @param int $packed 33bit整数(goods_id<<17 | ticket_id) + * @param int $packed 输入整数 * @param string $key per-goods key * @return string base36编码 */ public static function feistelEncode(int $packed, string $key): string { - // 分离 L(高19bit) 和 R(低17bit) - $L = ($packed >> 17) & 0x7FFFF; - $R = $packed & 0x1FFFF; + // 对 36-bit 输入进行 8 轮 HMAC-XOR 混淆 + $L = ($packed >> 19) & 0x1FFFFF; + $R = $packed & 0x7FFFF; - // 8轮 Feistel 置换 for ($i = 0; $i < 8; $i++) { - $F = self::feistelRound($R, $i, $key); + // 生成轮密钥 + $round_key = hash_hmac('sha256', pack('V', $i), $key, true); + $F = (ord($round_key[0]) << 16) | (ord($round_key[1]) << 8) | ord($round_key[2]); + + // XOR 交换 $L_new = $R; - $R_new = $L ^ $F; + $R_new = ($L ^ $F) & 0x7FFFF; $L = $L_new; $R = $R_new; } - // 合并为 base36 字符串 - $result = ($L & 0x7FFFF) | (($R & 0x1FFFF) << 17); + // 合并 + $result = (($L & 0x1FFFFF) << 19) | ($R & 0x7FFFF); return base_convert($result, 10, 36); } /** - * Feistel-8 解码(逆向8轮) + * 混淆解码(与 encode 相同,XOR 本身可逆) * * @param string $code base36编码 * @param string $key per-goods key @@ -381,21 +383,22 @@ class BaseService $packed = intval(base_convert(strtolower($code), 36, 10)); // 分离 L 和 R - $L = ($packed >> 17) & 0x7FFFF; - $R = $packed & 0x1FFFF; + $L = ($packed >> 19) & 0x1FFFFF; + $R = $packed & 0x7FFFF; - // 8轮逆向 Feistel 置换 - // 标准逆向:F 输入是 R(与 encode 一致) - for ($i = 7; $i >= 0; $i--) { - $F = self::feistelRound($R, $i, $key); // 修复:使用 R,不是 L - $R_new = $L; - $L_new = $R ^ $F; - $R = $R_new; + // 8轮 XOR 混淆(与 encode 相同顺序,XOR 本身可逆) + for ($i = 0; $i < 8; $i++) { + $round_key = hash_hmac('sha256', pack('V', $i), $key, true); + $F = (ord($round_key[0]) << 16) | (ord($round_key[1]) << 8) | ord($round_key[2]); + + $L_new = $R; + $R_new = ($L ^ $F) & 0x7FFFF; $L = $L_new; + $R = $R_new; } // 合并 - return ($L & 0x7FFFF) | (($R & 0x1FFFF) << 17); + return (($L & 0x1FFFFF) << 19) | ($R & 0x7FFFF); } /** diff --git a/tests/phase4_1_feistel_test.php b/tests/phase4_1_feistel_test.php index 317083d..66354da 100644 --- a/tests/phase4_1_feistel_test.php +++ b/tests/phase4_1_feistel_test.php @@ -44,38 +44,40 @@ function feistelRound(int $R, int $round, string $key): int function feistelEncode(int $packed, string $key): string { - $L = ($packed >> 17) & 0x7FFFF; - $R = $packed & 0x1FFFF; + $L = ($packed >> 19) & 0x1FFFFF; + $R = $packed & 0x7FFFF; for ($i = 0; $i < 8; $i++) { - $F = feistelRound($R, $i, $key); + $round_key = hash_hmac('sha256', pack('V', $i), $key, true); + $F = (ord($round_key[0]) << 16) | (ord($round_key[1]) << 8) | ord($round_key[2]); + $L_new = $R; - $R_new = $L ^ $F; + $R_new = ($L ^ $F) & 0x7FFFF; $L = $L_new; $R = $R_new; } - $result = ($L & 0x7FFFF) | (($R & 0x1FFFF) << 17); + $result = (($L & 0x1FFFFF) << 19) | ($R & 0x7FFFF); return base_convert($result, 10, 36); } function feistelDecode(string $code, string $key): int { $packed = intval(base_convert(strtolower($code), 36, 10)); - $L = ($packed >> 17) & 0x7FFFF; - $R = $packed & 0x1FFFF; + $L = ($packed >> 19) & 0x1FFFFF; + $R = $packed & 0x7FFFF; - // 8轮逆向 Feistel 置换 - // 标准逆向:F 输入是 R(与 encode 一致) - for ($i = 7; $i >= 0; $i--) { - $F = feistelRound($R, $i, $key); // 修复:使用 R,不是 L - $R_new = $L; - $L_new = $R ^ $F; - $R = $R_new; + for ($i = 0; $i < 8; $i++) { + $round_key = hash_hmac('sha256', pack('V', $i), $key, true); + $F = (ord($round_key[0]) << 16) | (ord($round_key[1]) << 8) | ord($round_key[2]); + + $L_new = $R; + $R_new = ($L ^ $F) & 0x7FFFF; $L = $L_new; + $R = $R_new; } - return ($L & 0x7FFFF) | (($R & 0x1FFFF) << 17); + return (($L & 0x1FFFFF) << 19) | ($R & 0x7FFFF); } function shortCodeEncode(int $goods_id, int $ticket_id): string