File size: 941B
<?PHP
class OSSL {
/* 256 битный ключ для AES-256GCM */
private $encryptionKey;
private $iv;
public function __construct($key) {
/* 256 бит - это 32 байта */
$this->encryptionKey = $key;
$this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-gcm'));
}
public function Encrypt($data) {
$cipherText = openssl_encrypt($data, 'aes-256-gcm', $this->encryptionKey, 0, $this->iv, $tag);
/*
Возвращаем:
- зашифрованные данные;
- тег для аутентификации
- инициализационный вектор
*/
return ['cipherText' => $cipherText, 'tag' => $tag, 'iv' => $this->iv];
}
public function Decrypt($cipherText, $tag, $iv) {
$decryptedData = openssl_decrypt($cipherText, 'aes-256-gcm', $this->encryptionKey, 0, $iv, $tag);
return $decryptedData;
}
}