Method to encrypt a data string.

@param   string $data The data string to encrypt.
@param   string $key  The private key.
@param   string $iv   The public key.

@return  string  The encrypted data string.

@since   2.0
@throws  \InvalidArgumentException
17363
public function encrypt($data, $key = null, $iv = null)
    {
        $iv = $iv ?: $this->getRandomKey(static::DEFAULT_RANDOM_BYTE_LENGTH);

        $key = sha1($iv . $key);

        return base64_encode($iv . $this->doEncrypt($data, $key));
    }
1059877
public function decryptData($encryptedData, $iv, &$data)
    {
        if (strlen($this->sessionKey) != 24) {
            return ErrorCode::$IllegalAesKey;
        }
        $aesKey = base64_decode($this->sessionKey);

        if (strlen($iv) != 24) {
            return ErrorCode::$IllegalIv;
        }
        $aesIV = base64_decode($iv);

        $aesCipher = base64_decode($encryptedData);

        $pc = new Prpcrypt($aesKey);
        $result = $pc->decrypt($aesCipher, $aesIV);

        if ($result[0] != 0) {
            return $result[0];
        }

        $dataObj = json_decode($result[1]);
        if ($dataObj == NULL) {
            return ErrorCode::$IllegalBuffer;
        }
        if ($dataObj->watermark->appid != $this->appid) {
            return ErrorCode::$IllegalBuffer;
        }
        $data = $result[1];
        return ErrorCode::$OK;
    }
363376
public byte[] encrypt(byte[] iv, byte[] data) throws IntegrityException {
        if (data.length % blockSize != 0) {
            throw new IntegrityException("Incorrect data size");
        }
        if (iv.length != blockSize) {
            throw new IntegrityException("Incorrect iv size");
        }

        byte[] currentBlock = new byte[blockSize];
        byte[] res = new byte[data.length];

        byte[] currentIV = new byte[blockSize];
        for (int i = 0; i < blockSize; i++) {
            currentIV[i] = iv[i];
        }

        int count = data.length / blockSize;
        for (int i = 0; i < count; i++) {

            for (int j = 0; j < blockSize; j++) {
                currentBlock[j] = (byte) ((data[i * blockSize + j] & 0xFF) ^ (currentIV[j] & 0xFF));
            }

            blockCipher.encryptBlock(currentBlock, 0, res, i * blockSize);

            for (int j = 0; j < blockSize; j++) {
                currentIV[j] = res[i * blockSize + j];
            }
        }

        return res;
    }
730256
protected function encrypt_3DES($data, $key)
    {
        $iv = "\0\0\0\0\0\0\0\0";
        $data_padded = $data;

        if (strlen($data_padded) % 8) {
            $data_padded = str_pad($data_padded, strlen($data_padded) + 8 - strlen($data_padded) % 8, "\0");
        }

        return openssl_encrypt($data_padded, "DES-EDE3-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);
    }
627842
public static String encrypt(String data, byte[] key)
    {
        try
        {
            Key k = toSecretKey(key);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, k);
            byte[] toByteArray = data.getBytes();
            byte[] encrypted = cipher.doFinal(toByteArray);
            return Hex.encodeHexString(encrypted);
        }
        catch (GeneralSecurityException e)
        {
            throw new RuntimeException("Failed to encrypt.", e);
        }
    }
1163898
public function encrypt($data)
	{
		if (!$this->encEnabled) return $data; // no encryption
		switch ($this->encMethod) {
			case 'SIMPLE':
				return $this->encryptSimple($data);
			case 'INTEGER':
				return $this->encryptInteger($data);
		}
		if ($this->iv) {
			$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->encMethod));
		} else {
			$iv=substr(md5($data,true),0,openssl_cipher_iv_length($this->encMethod));
		}
		$encrypted_string = bin2hex($iv) . openssl_encrypt($this->encSalt . $data, $this->encMethod, $this->encPassword, 0, $iv);
		return str_replace(array('+', '/'), array('-', '_'),base64_encode($encrypted_string));
	}
1239988
public static function encrypt($data, $key, array $setting = [])
    {
        $data = (string) $data;
        if(!is_string($data)){
            throw  new Exception(Exception::CODE_PARAM_ERROR_MSG,Exception::CODE_PARAM_VALUE_ERROR);
        }

        $setting = array_merge(Utils::CRYPT_DEFAULT, $setting);
        $encrypted = openssl_encrypt($data,$setting['cipher'],$key,$setting['options'],$setting['iv']);
        if($encrypted === false){
            throw new Exception('Can not encrypt data',Exception::CODE_PARAM_VALUE_ERROR);
        }
        return base64_encode($encrypted);
    }
207698
public function encrypt($message, $key, $iv)
    {
        if (!empty($message)) {
            mcrypt_generic_init($this->module, $key, $iv);
            $message = mcrypt_generic($this->module, $message);
            mcrypt_generic_deinit($this->module);
        }

        return $message;
    }
251683
public static function encrypt($data, $adapter = null)
    {
        ($adapter !== null) ? self::setAdapter($adapter) : self::setAdapter(__config()->encryption->driver);

        return self::$adapter->encrypt($data);
    }
839485
function encrypt(key, iv, data) {
    if(!Buffer.isBuffer(key)) {
        assert(typeof key === 'string');
        key = Buffer.from(key, 'binary')
    }

    if(!Buffer.isBuffer(iv)) {
        assert(typeof iv === 'string');
        iv = Buffer.from(iv, 'binary')
    }

    if(!Buffer.isBuffer(data)) {
        assert(typeof data === 'string');
        data = Buffer.from(data, 'binary')
    }

    var crypted = aes.encipher(data, key, iv);

    crypted = Buffer.from(crypted, 'binary');
    
    let ret = Buffer.alloc(crypted.length*2);
    for(let i = 0; i < crypted.length; i++){
        ret[2*i] = (crypted[i] >> 4) + 97;
        ret[2*i+1] = (crypted[i] & 0x0F) + 97;
    }
    return ret.toString('binary');
}
1207766
public static function encrypt(string $data, string $password): string
    {
        // Generate IV of appropriate size.
        $iv = self::generateIv();
        // Derive key from password
        $key = self::hash($iv, $password);
        // Encrypt the given data
        $cyphertext = openssl_encrypt($data, self::CIPHER, $key, OPENSSL_RAW_DATA, $iv);

        if ($cyphertext === false) {
            // @codeCoverageIgnoreStart
            throw new RuntimeException('Encryption library: Encryption (symmetric) of content failed: ' . openssl_error_string());
            // @codeCoverageIgnoreEnd
        }
        // Checksum : Create a keyed hash for the encrypted data
        $checksum = self::hash($iv . $cyphertext, $key);
        // concat all the elements in the final encrypted string
        $encrypted = $iv . $checksum . $cyphertext;

        return $encrypted;
    }
721823
public function encrypt($data)
    {
        $this->checkClosed();

        $data = self::convertToString($data);

        if (strlen($data) == 0) {
            throw new \InvalidArgumentException('Encryption doesn\'t support empty string');
        }

        //Encryption
        if ($this->randomInitializationVector) {
            $initializationVector = mcrypt_create_iv($this->initializationVectorSize, $this->mcryptRandomMethod);
        } else {
            $initializationVector = $this->fixedInitializationVector;
        }
        mcrypt_generic_init($this->module, $this->key, $initializationVector);
        $encryptedData = mcrypt_generic($this->module, (string) $data);
        if ($this->randomInitializationVector) {
            $encryptedData = $initializationVector . $encryptedData;
        }

        //Base64
        if ($this->base64) {
            $encryptedData = base64_encode($encryptedData);

            //Url safe
            if ($this->base64UrlSafe) {
                $encryptedData = strtr($encryptedData, self::$BASE64_URL_SAFE_REPLACE[0], self::$BASE64_URL_SAFE_REPLACE[1]);
            }

            $encryptedData = rtrim($encryptedData, '='); //Remove '=' at the end (it's useless)
        }

        return $encryptedData;
    }
1328321
public static function decrypt($data, $key = null)
    {
        is_null($key) && $key = Config::get('auth_key');
        return unserialize(self::cry($data, 2, $key));
    }
1000273
public static function decrypt($data, $key, $iv) {
        $output = false;
        $encrypt_method = "AES-256-CBC";
        // hash
        $key = hash_hmac('sha256', $data, $iv);

        // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
        $iv = substr($iv, 0, 16);
        $output = openssl_decrypt(base64_decode($data), $encrypt_method, $key, 0, $iv);

        return $output;
    }
1108690
public static function ctrCrypt($data, $key, $initializationVector)
    {
        Validate::isString($data, 'data');
        Validate::isString($key, 'key');
        Validate::isString($initializationVector, 'initializationVector');

        Validate::isTrue(
            (strlen($key) == 16 || strlen($key) == 24 || strlen($key) == 32),
            sprintf(Resources::INVALID_STRING_LENGTH, 'key', '16, 24, 32')
        );

        Validate::isTrue(
            (strlen($initializationVector) == 16),
            sprintf(Resources::INVALID_STRING_LENGTH, 'initializationVector', '16')
        );

        return openssl_encrypt(
            $data,
            'aes-256-ctr',
            $key,
            OPENSSL_RAW_DATA,
            $initializationVector
        );
    }
1255056
public function encryptByPrivateKey_data($data, $privatekey = '')
	{
		$RSACrypt = new RSACrypt();

		if ($privatekey != "") {
			$RSACrypt->privkey = $privatekey;
		}
		$crypt_res = "";
		for ($i = 0; $i < ((strlen($data) - strlen($data) % 117) / 117 + 1); $i++) {
			$crypt_res = $crypt_res . ($RSACrypt->encryptByPrivateKey(mb_strcut($data, $i * 117, 117, 'utf-8')));
		}
		return $crypt_res;
	}
592870
public function encrypt($data, Key $key)
	{
		// Validate key.
		if ($key->type != 'simple')
		{
			throw new \InvalidArgumentException('Invalid key of type: ' . $key->type . '.  Expected simple.');
		}

		$encrypted = '';
		$tmp = $key->private;

		// Split up the input into a character array and get the number of characters.
		$chars = preg_split('//', $data, -1, PREG_SPLIT_NO_EMPTY);
		$charCount = count($chars);

		// Repeat the key as many times as necessary to ensure that the key is at least as long as the input.
		for ($i = 0; $i < $charCount; $i = strlen($tmp))
		{
			$tmp = $tmp . $tmp;
		}

		// Get the XOR values between the ASCII values of the input and key characters for all input offsets.
		for ($i = 0; $i < $charCount; $i++)
		{
			$encrypted .= $this->_intToHex(ord($tmp[$i]) ^ ord($chars[$i]));
		}

		return $encrypted;
	}