HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux Bradford-Sitios 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: www-data (33)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/api_matriculas/app/Services/EncryptionService.php
<?php

namespace App\Services;

use Exception;
use Illuminate\Support\Facades\Log;

class EncryptionService
{
    private $key;
    private $iv;

    public function __construct()
    {
        $this->key = env('ENCRYPTION_KEY');
        $this->iv  = env('ENCRYPTION_IV');
    }


    /**
     * Cifra el texto usando AES-256-CBC y lo devuelve en base64.
     */
    public function encrypt($plainText)
    {

        $cipher = 'AES-256-CBC';
        // Usar OPENSSL_RAW_DATA para obtener los datos en bruto
        $encrypted = openssl_encrypt($plainText, $cipher, $this->key, OPENSSL_RAW_DATA, $this->iv);

        return base64_encode($encrypted);
    }



    /**
     * Descifra el texto cifrado en base64.
     */
    public function decrypt($cipherText)
    {
        try {
            // Verifica si el texto es válido antes de intentar descifrarlo
            if (empty($cipherText) || !$this->isBase64($cipherText)) {
                return $cipherText;
            }

            $cipher = 'AES-256-CBC';
            // Usamos OPENSSL_RAW_DATA para trabajar con datos en crudo
            return openssl_decrypt(base64_decode($cipherText), $cipher, $this->key, OPENSSL_RAW_DATA, $this->iv);
        } catch (Exception $e) {
            Log::error("Error al descifrar: " . $e->getMessage());
            return $cipherText;
        }
    }

    /**
     * Verifica si una cadena es válida en base64.
     */
    private function isBase64($string)
    {
        if (empty($string) || strlen($string) % 4 !== 0) {
            return false;
        }

        return base64_encode(base64_decode($string, true)) === $string;
    }
}