I know this question might be asked multiple times and there is also an example for capturing IV and TAG before decryption but when I use it, it does not work!
I'm trying to use "decrypt" function in separate file, and I when i try to use this function, i see no output. Below messy code is the method that I am trying to use for encryption/decryption but why i see no output for decryption?
If anyone knows the answer please help me. Thank you very much.
<?php
class encryption{
private $KEY;
private $cipher = "aes-256-gcm";
private $ivlen;
private $IV;
private $options;
private $tag;
private $output = "";
private function Key(){
$this->KEY = openssl_random_pseudo_bytes (16);
return $this->KEY;
}
private function Iv(){
$this->IV = openssl_random_pseudo_bytes (16);
$this->IV = base64_encode($this->IV);
return $this->IV;
}
public function encrypt($string){
$ivlen = openssl_cipher_iv_length($this->cipher);
if (in_array($this->cipher, openssl_get_cipher_methods())){
$this->output = openssl_encrypt($string, $this->cipher, $this->Key(), $options=0, $this->Iv(), $this->tag);
$this->tag = base64_encode($this->tag);
}
return $this->output;
}
public function decrypt($string){
$this->tag = base64_decode($this->tag);
$dec_iv = base64_decode($this->Iv());
if (in_array($this->cipher, openssl_get_cipher_methods())){
$this->output = openssl_decrypt($string, $this->cipher, $this->Key(), $options=0, $dec_iv, $this->tag);
}
return $this->output;
}
}
?>