<?php

namespace App\Http\Service;

class AESUtilService
{
    private $key;

    public function __construct()
    {
        // Define your encryption key
        $this->key = 'fuckinginthebedm';
    }

    public function encrypt(string $cleartext): string
    {
        $ivSize = openssl_cipher_iv_length('AES-128-CBC');
        $iv = openssl_random_pseudo_bytes($ivSize);
        $encrypted = openssl_encrypt($cleartext, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $iv);
        return bin2hex($iv . $encrypted);
    }

    public function decrypt(string $encrypted): string
    {
        $ivSize = openssl_cipher_iv_length('AES-128-CBC');
        $encrypted = hex2bin($encrypted);
        $iv = substr($encrypted, 0, $ivSize);
        $encrypted = substr($encrypted, $ivSize);
        return openssl_decrypt($encrypted, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $iv);
    }
}
