getMessage()); } } /** * Decrypt using a custom key (independent of APP_KEY) */ public static function decryptWithCustomKey(string $encryptedString, string $key): ?string { try { $cipher = 'AES-256-CBC'; $ivLength = openssl_cipher_iv_length($cipher); // Decodificar y separar IV + encrypted data $data = base64_decode($encryptedString); if ($data === false) { return null; } $iv = substr($data, 0, $ivLength); $encrypted = substr($data, $ivLength); $decrypted = openssl_decrypt($encrypted, $cipher, $key, 0, $iv); if ($decrypted === false) { Log::error('Error al desencriptar con clave personalizada'); return null; } return $decrypted; } catch (\Exception $e) { Log::error('Error inesperado al desencriptar con clave personalizada: ' . $e->getMessage()); return null; } } /** * Verify if a plain value matches a value encrypted with custom key */ public static function verifyWithCustomKey(string $plainValue, string $encryptedValue, string $key): bool { try { $decrypted = self::decryptWithCustomKey($encryptedValue, $key); return $decrypted === $plainValue; } catch (\Exception $e) { return false; } } }