digest, true); $iv_length = openssl_cipher_iv_length($this->cipher); $iv = openssl_random_pseudo_bytes($iv_length); return base64_encode($iv . openssl_encrypt($value, $this->cipher, $key, OPENSSL_RAW_DATA, $iv)); } /** * Decrypt * * @param string $key * @param string $value * * @return string */ public function decrypt(string $key, string $value): string { $result = ''; $key = openssl_digest($key, $this->digest, true); $iv_length = openssl_cipher_iv_length($this->cipher); $value = base64_decode($value); $iv = substr($value, 0, $iv_length); $value = substr($value, $iv_length); if (strlen($iv) == $iv_length) { $result = openssl_decrypt($value, $this->cipher, $key, OPENSSL_RAW_DATA, $iv); } return $result; } }