Mini Shell
<?php
require_once 'Configuration.php';
class Crypto
{
private $key;
function __construct()
{
$this->key = Configuration::get("key");
$this->method = "AES-128-CBC";
}
public function encrypt($data)
{
$ivlen = openssl_cipher_iv_length($this->method);
$iv = openssl_random_pseudo_bytes($ivlen);
$ciphertext_raw = openssl_encrypt($data, $this->method, $this->key, $options = OPENSSL_RAW_DATA, $iv);
$hmac = hash_hmac('sha256', $ciphertext_raw, $this->key, $as_binary = true);
$ciphertext = base64_encode($iv . $hmac . $ciphertext_raw);
return $ciphertext;
}
public function decrypt($data)
{
$c = base64_decode($data);
$ivlen = openssl_cipher_iv_length($this->method);
$iv = substr($c, 0, $ivlen);
$ciphertext_raw = substr($c, $ivlen + 32);
$plaintext = openssl_decrypt($ciphertext_raw, $this->method, $this->key, $options = OPENSSL_RAW_DATA, $iv);
return $plaintext;
}
}
?>