<?php
class OpenSSLCrypt
{
protected $secret_key;
protected $secret_iv;
public function __construct($secret_key, $secret_iv)
{
$this->secret_iv = substr(hash('sha256', $secret_iv), 0, 16); $this->secret_key = hash('sha256', $secret_key);
}
public function crypt($string) {
return base64_encode(openssl_encrypt
($string, 'AES-256-CBC', $this->secret_key, 0, $this->secret_iv)); }
public function decrypt($string)
{
return openssl_decrypt
(base64_decode($string), 'AES-256-CBC', $this->secret_key, 0, $this->secret_iv); }
}
// Пример использования
// Секретные ключи
$iv = '05978abff10e52b19d32bc3d7d8b7f50';
$key = '322b6748909f1465375b264956807320';
$crypt = new OpenSSLCrypt($key,$iv);
// Шифруем
$r1 = $crypt->crypt('Привет мир');
Дешифруем
$r2 = $crypt->decrypt($r1);
?>