private function encryptPrivateKey(PKIPrivateKey $pkiKey)
    {
        // Generate the symmetric key and iv to encrypt the users' key
        $iv = $this->cryptographyProvider->generateIV(CryptographyProviderInterface::PRIVATE_KEY_ENCRYPTION);
        $symmetricKey = $this->cryptographyProvider->generateSecureKey();
        $keyData = new KeyData($symmetricKey, $iv);

        // The private key of the user that we want to encrypt
        $userPrivateKey = $pkiKey->getPrivateKey();
        $encryptedUserPrivateKey = $this->cryptographyProvider->encrypt($userPrivateKey, $keyData);

        if ($encryptedUserPrivateKey) {
            $pkiKey->setPrivateKey($encryptedUserPrivateKey);
            $pkiKey->setEncrypted(true);

            // Now we have to encrypt the symmetric key with the public master key
            $publicMasterKey = $this->getPublicMasterKey();
            $encryptedKey = base64_encode($this->cryptographyProvider->encryptWithPublicKey($symmetricKey, $publicMasterKey));
            $pkiKey->setKey($encryptedKey);
            $pkiKey->setIv($iv);
        }
        else {
            $pkiKey->setKey(null);
            $pkiKey->setIv(null);
            $pkiKey->setEncrypted(false);
        }
    }