protected function encrypt($data, $key)
    {
        $keySize = openssl_cipher_iv_length(static::CRYPT_MODE);
        $padding = $keySize - (mb_strlen($data, '8bit') % $keySize);
        /** @noinspection CryptographicallySecureRandomnessInspection */
        $iv   = openssl_random_pseudo_bytes($keySize);
        $key  = $this->generateKey($key);
        $data .= str_repeat(chr($padding), $padding);

        // add in our IV and base64 encode the data
        $data = base64_encode(
            $iv
            . openssl_encrypt(
                $data, static::CRYPT_MODE, $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv
            )
        );

        return $data;
    }