<?php
/**
 * Encryption.php — Matches what the modded native lib decrypts
 *
 * Encryption Pipeline (applied to the JSON wrapper):
 *   1. Input string → convert to hex (bin2hex)
 *   2. Reverse the hex string (strrev)
 *   3. Output the reversed hex
 *
 * The native lib reverses this:
 *   1. Reverse the string
 *   2. Hex decode to get the JSON wrapper
 *   3. Extract "portals" field (encoded key + encrypted data)
 *   4. Decode first 128 chars as the 32-digit key (4 hex chars per digit)
 *   5. Decrypt remainder with AES-256-CBC using that key and fixed IV
 */
class Encryption {
    /**
     * Final obfuscation layer: hex encode then reverse
     */
    public static function run(string $input): string {
        $hex = bin2hex($input);
        return strrev($hex);
    }

    /**
     * Generate fake random data for decoy JSON fields
     */
    public static function runfake(int $length = 500): string {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        $len = strlen($characters);
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[random_int(0, $len - 1)];
        }
        return $randomString;
    }
}
