cipher = self::CONF_CIPHER_CRYPT; } else { $this->cipher = $cipher; } $this->key = $key; $this->option = $option; $this->tag = $tag; $this->openssl_crypt(); } public function encrypt($message = null) { $this->setMessage($message); $encrypt = openssl_encrypt($this->message, $this->cipher, $this->key, $this->option, $this->iv); $hashcode = hash_hmac('sha256', $encrypt, $this->key, true); $this->textcrypt = base64_encode($this->iv . $hashcode . $encrypt); return $this->textcrypt; } public function decrypt($textcrypt = null) { $c = base64_decode($textcrypt); $this->iv = substr($c, 0, $this->ivlen); $hmac = substr($c, $this->ivlen, 32); $ciphertext_raw = substr($c, $this->ivlen + 32); $original_plaintext = openssl_decrypt($ciphertext_raw, $this->cipher, $this->key, $options = OPENSSL_RAW_DATA, $this->iv); $calcmac = hash_hmac('sha256', $ciphertext_raw, $this->key, true); if (hash_equals($hmac, $calcmac)) { return $original_plaintext; } } private function openssl_crypt() { if (in_array(strtolower($this->cipher), openssl_get_cipher_methods())) { $this->ivlen = openssl_cipher_iv_length($this->cipher); $this->iv = openssl_random_pseudo_bytes($this->ivlen); } } public function setKey($key) { $this->key = $key; } public function setOption($option) { $this->option = $option; } public function setTag($tag) { $this->tag = $tag; } public function setMessage($message) { $this->message = $message; } }