防止敏感資料 (用戶 ID、Token、偏好設定) 以明文寫入 Cookie,被竄改或竊取。採用 AES-256-ECB 模式加密,Base64 編碼安全傳輸。
<?php
class SecureCookie {
// 從配置取得 32 位元組金鑰 (建議從環境變數讀取)
private static function key(): string {
return Config::get('cookie_encryption_key'); // 32 bytes binary
}
/** 加密並 Base64 */
private static function encrypt(string $plain): string {
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); // ECB 模式 IV 不使用但必須提供
$cipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, self::key(), $plain, MCRYPT_MODE_ECB, $iv);
return base64_encode($cipher);
}
/** 解密 */
private static function decrypt(string $cipher): string {
$raw = base64_decode($cipher);
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND);
$plain = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, self::key(), $raw, MCRYPT_MODE_ECB, $iv);
return rtrim($plain, "\0"); // 移除 PKCS#7 padding 殘留
}
/** 設定加密 Cookie */
public static function set(string $name, string $value, int $expire = 0, string $path = '/', string $domain = '', bool $secure = false, bool $httponly = true): bool {
$encrypted = self::encrypt($value);
return setcookie($name, $encrypted, $expire ? time() + $expire : 0, $path, $domain, $secure, $httponly);
}
/** 讀取並解密 */
public static function get(string $name): ?string {
return isset($_COOKIE[$name]) ? self::decrypt($_COOKIE[$name]) : null;
}
/** 刪除 */
public static function delete(string $name, string $path = '/', string $domain = ''): bool {
if (!isset($_COOKIE[$name])) return true;
return setcookie($name, '', time() - 86400, $path, $domain);
}
}
// 使用範例
SecureCookie::set('user_token', 'abc123', 86400*30); // 30 天
$token = SecureCookie::get('user_token');
<?php
class ModernCookie {
private const CIPHER = 'aes-256-gcm'; // AEAD 模式,自帶完整性驗證
private static function key(): string {
// 從環境變數或配置讀取 32 位元組二進位金鑰
return base64_decode(Config::get('cookie_encryption_key_base64'));
}
public static function set(string $name, string $value, int $ttl = 2592000, string $path = '/', string $domain = ''): bool {
$iv = random_bytes(12); // GCM 建議 12 bytes
$tag = '';
$cipher = openssl_encrypt($value, self::CIPHER, self::key(), OPENSSL_RAW_DATA, $iv, $tag);
// 組合: iv(12) + tag(16) + ciphertext
$payload = base64_encode($iv . $tag . $cipher);
return setcookie($name, $payload, time() + $ttl, $path, $domain, true, true); // Secure, HttpOnly
}
public static function get(string $name): ?string {
if (!isset($_COOKIE[$name])) return null;
$data = base64_decode($_COOKIE[$name]);
if (strlen($data) < 28) return null; // iv(12)+tag(16)
$iv = substr($data, 0, 12);
$tag = substr($data, 12, 16);
$cipher = substr($data, 28);
$plain = openssl_decrypt($cipher, self::CIPHER, self::key(), OPENSSL_RAW_DATA, $iv, $tag);
return $plain !== false ? $plain : null;
}
public static function delete(string $name, string $path = '/', string $domain = ''): bool {
return setcookie($name, '', time() - 86400, $path, $domain, true, true);
}
}
random_bytes(32) 生成金鑰,Base64 編碼存入環境變數 COOKIE_ENC_KEY。Secure (HTTPS only) 與 HttpOnly (防 XSS 竊取)。SameSite=Lax 或 Strict 防 CSRF。