<?php

class Crypto
{
    const CAES256 = "H5m4454pFjh201dp54Ddd8gP5Hf6GVFd";

    static function criptografarAES256(String $value, String $key)
    {
        $iv = bin2hex(openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-128-cbc') / 2));
        $encrypted = openssl_encrypt($value, "AES-256-CBC", $key, 0, $iv);

        return base64_encode($encrypted . '::' . $iv);
    }

    static function descriptografarAES256( $value,  $key)
    {
        if(base64_decode($value) <> ''){
            list($encrypted_data, $iv) = explode('::', base64_decode($value), 2);
            if($encrypted_data <> '' AND $iv <> null)
                return openssl_decrypt($encrypted_data, 'aes-256-cbc', $key, 0, $iv);
            else return false;
        }else return false;

    }
}