Encrypt a pdf file

@param string $data raw pdf data
@throws \Cake\Core\Exception\Exception
@return string raw pdf data
491736
public function encrypt($data)
    {
        /** @var string $binary */
        $binary = $this->config('binary');

        if ($binary) {
            $this->_binary = $binary;
        }

        if (!is_executable($this->_binary)) {
            throw new Exception(sprintf('pdftk binary is not found or not executable: %s', $this->_binary));
        }

        $arguments = [];

        $ownerPassword = $this->_Pdf->ownerPassword();
        if ($ownerPassword !== null) {
            $arguments['owner_pw'] = escapeshellarg($ownerPassword);
        }

        $userPassword = $this->_Pdf->userPassword();
        if ($userPassword !== null) {
            $arguments['user_pw'] = escapeshellarg($userPassword);
        }

        $allowed = $this->_buildPermissionsArgument();
        if ($allowed) {
            $arguments['allow'] = $allowed;
        }

        if (!$ownerPassword && !$userPassword) {
            throw new Exception('Crypto: Required to configure atleast an ownerPassword or userPassword');
        }

        if ($ownerPassword == $userPassword) {
            throw new Exception('Crypto: ownerPassword and userPassword cannot be the same');
        }

        $command = sprintf('%s - output - %s', $this->_binary, $this->_buildArguments($arguments));

        $descriptorspec = [
            0 => ['pipe', 'r'], // feed stdin of process from this file descriptor
            1 => ['pipe', 'w'], // Note you can also grab stdout from a pipe, no need for temp file
            2 => ['pipe', 'w'], // stderr
        ];

        $prochandle = proc_open($command, $descriptorspec, $pipes);

        fwrite($pipes[0], $data);
        fclose($pipes[0]);

        $stdout = stream_get_contents($pipes[1]);
        fclose($pipes[1]);
        $stderr = stream_get_contents($pipes[2]);
        fclose($pipes[2]);

        $exitcode = proc_close($prochandle);

        if ($exitcode !== 0) {
            throw new Exception(sprintf('Crypto: Unknown error (exit code %d)', $exitcode));
        }

        return $stdout;
    }
904545
public function encrypt($data, $key) {
        if ($data === '') {
            return $data;
        }

        $cipher = $this->createCipher($key);

        $encrypted = mcrypt_generic($cipher, $data);

        $this->clearCipher($cipher);

        return $encrypted;
    }
1143769
public function encrypt(string $data): string
    {
        try {
            return Crypto::encrypt($data, $this->key);
        } catch(EnvironmentIsBrokenException $e) {
            throw new CryptoException($e->getMessage(), $e->getCode(), $e);
        }
    }
900245
public function decrypt(string $data)
    {
        if (!$this->password)
        {
            return JSON::decode($data);
        }

        $decrypt = $this->security()
            ->decrypt($data);

        return JSON::decode($decrypt);
    }
330407
public function fromRawData($data)
    {
        $secretLength = VarInt::readUnsignedVarInt($data);
        $data = substr($data, $secretLength->getDataLength());

        $secretData = substr($data, 0, $secretLength->getValue());
        $data = substr($data, $secretLength->getValue());

        $verifyLength = VarInt::readUnsignedVarInt($data);
        $data = substr($data, $verifyLength->getDataLength());

        $verifyData = substr($data, 0, $verifyLength->getValue());

        $this->setToken($verifyData)->setSecret($secretData);
    }
786410
public function decrypt(string $data) {
        $keyHash = md5(Config::getConfig()->encrypt->key);
        $key = substr($keyHash, 0, mcrypt_get_key_size(self::$cipher, self::$mode));
        $iv = substr($keyHash, 0, mcrypt_get_block_size(self::$cipher, self::$mode));
        return mcrypt_decrypt(self::$cipher, $key, base64_decode($data), self::$mode, $iv);
    }
800361
public static function json_encode(array $data)
    {
        $json = @json_encode($data);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \Exception(
                sprintf('An error occurred while trying to encode data to JSON [code "%s"]!', json_last_error())
            );
        } else {
            $data = $json;
        }
        return $data;
    }
527511
public function decrypt($data)
	{
		if (!$this->encEnabled) return $data; // no encryption
		switch ($this->encMethod) {
			case 'SIMPLE':
				return $this->decryptSimple($data);
			case 'INTEGER':
				return $this->decryptInteger($data);
		}
		$data=base64_decode(str_replace(array('-', '_'),array('+', '/'),$data));
		$iv_strlen = 2 * openssl_cipher_iv_length($this->encMethod);
		if (preg_match("/^(.{" . $iv_strlen . "})(.+)$/", $data, $regs)) {
			try {
				list(, $iv, $crypted_string) = $regs;
				$decrypted_string = openssl_decrypt($crypted_string, $this->encMethod, $this->encPassword, 0, hex2bin($iv));
				return substr($decrypted_string, strlen($this->encSalt));
			} catch(Exception $ex) {
				return false;
			}
		} else {
			return false;
		}
	}
1029911
public function encrypt($data)
    {
        // Get module
        $module = mcrypt_module_open($this->cipher, '', $this->mode, '');

        // Create initialization vector
        $vector = mcrypt_create_iv(mcrypt_enc_get_iv_size($module), MCRYPT_DEV_URANDOM);

        // Validate key length
        $this->validateKeyLength($this->key, $module);

        // Initialize encryption
        $initResult = mcrypt_generic_init($module, $this->key, $vector);

        if (!is_null($initResult)) {
            $this->throwInitError($initResult, 'encrypt');
        }

        // Encrypt
        $encryptedData = mcrypt_generic($module, $data);

        // Deinitialize encryption
        mcrypt_generic_deinit($module);

        // Ensure integrity of encrypted data with HMAC hash
        $hmac = $this->getHmac($encryptedData);

        return implode('|', array(base64_encode($vector), base64_encode($encryptedData), $hmac));
    }
278853
public function decrypt(string $data): string
    {
        $this->clearErrors();

        $vector = $this->getIV();
        if (empty($vector) === true) {
            $ivLength = $this->openSslIvLength($this->getMethod());
            $vector   = $this->readIV($data, $ivLength);
            $data     = $this->extractData($data, $ivLength);
        }

        if ($this->isUseAuthentication() === true) {
            $tagLength = $this->getTagLength();
            $tag       = $this->readTag($data, $tagLength);
            $data      = $this->extractData($data, $tagLength);

            $decrypted = $this->openSslDecryptAuthenticated(
                $data,
                $this->getMethod(),
                $this->getPassword(),
                $this->getOptions(),
                $vector,
                $this->getAdditionalAuthenticationData(),
                $tag
            );
        } else {
            $decrypted = $this->openSslDecrypt(
                $data,
                $this->getMethod(),
                $this->getPassword(),
                $this->getOptions(),
                $vector
            );
        }

        return $decrypted;
    }
1264236
public function decode($data)
    {
        return openssl_decrypt($data, $this->method, $this->key, 0, $this->iv);
    }
1035660
protected function decrypt($data)
    {
        $data = base64_decode($data);
        $salt = substr($data, 0, 16);
        $data = substr($data, 16);

        $salted = hash('sha512', $this->key . $salt, true);
        $key    = substr($salted, 0, 32);
        $iv     = substr($salted, 32, 16);

        return openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
    }
336545
protected function prepareForWrite(string $data) : string
    {
        if ($this->usesEncryption) {
            if ($this->encrypter === null) {
                throw new LogicException('Encrypter not set in session handler');
            }

            try {
                return $this->encrypter->encrypt($data);
            } catch (SessionEncryptionException $ex) {
                return '';
            }
        } else {
            return $data;
        }
    }
1081834
public function crypt(string $data) {
        $keyHash = md5(Config::getConfig()->encrypt->key);
        $key = substr($keyHash, 0, mcrypt_get_key_size(self::$cipher, self::$mode));
        $iv = substr($keyHash, 0, mcrypt_get_block_size(self::$cipher, self::$mode));
        return base64_encode(mcrypt_encrypt(self::$cipher, $key, $data, self::$mode, $iv));
    }
721922
public function encrypt($data) {
        $data = trim($data);
        $str = $this->pkcs7Pad($data, $this->blockSize);
        $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128,MCRYPT_MODE_CBC),1);
        $encrypt_str =  mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->key, $str, MCRYPT_MODE_CBC);
        return base64_encode($encrypt_str);
    }
708973
public  function encryptByPublicKey($data) {
		$pu_key  = openssl_pkey_get_public($this->pubkey);
		$encrypted="";
		openssl_public_encrypt($data,$encrypted,$pu_key,OPENSSL_PKCS1_PADDING);//公钥加密
		$encrypted = self::urlsafe_b64encode($encrypted);//加密后的内容通常含有特殊字符，需要编码转换下，在网络间通过url传输时要注意base64编码是否是url安全的
		return $encrypted;
	}
910024
public function encrypt($data)
    {
        if (!$this->iv || $this->autoIvUpdate) {
            $this->generateIv();
        }

        $output = openssl_encrypt($data, $this->cipher, $this->secret, OPENSSL_RAW_DATA, $this->iv);

        if ($this->formatter) {
            $output = $this->formatter->format($this->iv, $output);
        }

        return $output;
    }