/** * https://tools.ietf.org/html/rfc7518#section-5.2.2.1 * @param string $plain_text * @param string $key * @param string $iv * @param string $aad * @return \string[] * @throws InvalidKeyLengthAlgorithmException */ public function encrypt($plain_text, $key, $iv, $aad) { $key_len = strlen($key); if ($this->getMinKeyLen() > ByteUtil::bitLength($key_len)) { throw new InvalidKeyLengthAlgorithmException(); } $enc_key_len = $key_len / 2; // ENC_KEY = final ENC_KEY_LEN octets of K $enc_key = substr($key, $enc_key_len); $this->aes->setKey($enc_key); $this->aes->setIV($iv); $cypher_text = $this->aes->encrypt($plain_text); $tag = $this->calculateAuthenticationTag($cypher_text, $key, $iv, $aad); return array($cypher_text, $tag); }
/** * @return int */ public function getBitLength() { return ByteUtil::bitLength(strlen($this->secret)); }