php openssl 分段 加密 解密

加密:

public function encrypt($data){
	$crypted = [];
	$data = json_encode($data);
	
	$publicKey = openssl_pkey_get_public(file_get_contents($this->publicKeyPath));
	$dataArray = str_split($data, 117);
	foreach($dataArray as $subData){
		$subCrypted = null;
		openssl_public_encrypt($subData, $subCrypted, $publicKey);
		$crypted[] = $subCrypted;
	}
	$this->log($data);
	return base64_encode(implode('',$crypted));
}

解密:

public function decrypt($data){
	$decrypted = [];
	$data = base64_decode($data);
	
	$privateKey = openssl_pkey_get_private(file_get_contents($this->privateKeyPath));
	$dataArray = str_split($data, 128);
	foreach($dataArray as $subData){
		$subDecrypted = null;
		openssl_private_decrypt($subData, $subDecrypted, $privateKey);
		$decrypted[] = $subDecrypted;
	}
	$decrypted = implode('',$decrypted);
	$this->log($decrypted);
	return json_decode($decrypted, true);
}