(self)
Initiate a key exchange with the server that is required when dealing
        with secure strings. This can only be run once the RunspacePool is
        open and if the key has already been exchanged then nothing will
        happen.
Initiate a key exchange with the server that is required when dealing
        with secure strings. This can only be run once the RunspacePool is
        open and if the key has already been exchanged then nothing will
        happen.
Initiate a key exchange with the server that is required when dealing with secure strings . This can only be run once the RunspacePool is open and if the key has already been exchanged then nothing will happen .
def exchange_keys(self):
        """
        Initiate a key exchange with the server that is required when dealing
        with secure strings. This can only be run once the RunspacePool is
        open and if the key has already been exchanged then nothing will
        happen.
        """
        log.info("Starting key exchange with remote host")
        if self._key_exchanged or self._exchange_key is not None:
            # key is already exchanged or we are still in the processes of
            # exchanging it, no need to run again
            return

        # Generate a unique RSA key pair for use in this Pool only
        self._exchange_key = rsa.generate_private_key(
            public_exponent=65537,
            key_size=2048,
            backend=default_backend(),
        )
        public_numbers = self._exchange_key.public_key().public_numbers()
        exponent = struct.pack("<I", public_numbers.e)
        modulus = b""
        for i in range(0, 256):
            byte_value = struct.pack("B", public_numbers.n >> (i * 8) & 0xff)
            modulus += byte_value

        # the public key bytes follow a set structure defined in MS-PSRP
        public_key_bytes = b"\x06\x02\x00\x00\x00\xa4\x00\x00" \
                           b"\x52\x53\x41\x31\x00\x08\x00\x00" + \
                           exponent + modulus
        public_key = base64.b64encode(public_key_bytes)

        msg = PublicKey(public_key=public_key.decode('utf-8'))
        fragments = self._fragmenter.fragment(msg, self.id)
        for fragment in fragments:
            self.shell.send('stdin', fragment)

        start = time.time()
        while not self._key_exchanged:
            elapsed = int((time.time() - start) * 1000)
            if elapsed > self.session_key_timeout_ms:
                raise InvalidPSRPOperation("Timeout while waiting for key "
                                           "exchange")
            self._receive()
def exchange_keys ( self ) : log . info ( "Starting key exchange with remote host" ) if self . _key_exchanged or self . _exchange_key is not None : # key is already exchanged or we are still in the processes of # exchanging it, no need to run again return # Generate a unique RSA key pair for use in this Pool only self . _exchange_key = rsa . generate_private_key ( public_exponent = 65537 , key_size = 2048 , backend = default_backend ( ) , ) public_numbers = self . _exchange_key . public_key ( ) . public_numbers ( ) exponent = struct . pack ( "<I" , public_numbers . e ) modulus = b"" for i in range ( 0 , 256 ) : byte_value = struct . pack ( "B" , public_numbers . n >> ( i * 8 ) & 0xff ) modulus += byte_value # the public key bytes follow a set structure defined in MS-PSRP public_key_bytes = b"\x06\x02\x00\x00\x00\xa4\x00\x00" b"\x52\x53\x41\x31\x00\x08\x00\x00" + exponent + modulus public_key = base64 . b64encode ( public_key_bytes ) msg = PublicKey ( public_key = public_key . decode ( 'utf-8' ) ) fragments = self . _fragmenter . fragment ( msg , self . id ) for fragment in fragments : self . shell . send ( 'stdin' , fragment ) start = time . time ( ) while not self . _key_exchanged : elapsed = int ( ( time . time ( ) - start ) * 1000 ) if elapsed > self . session_key_timeout_ms : raise InvalidPSRPOperation ( "Timeout while waiting for key " "exchange" ) self . _receive ( )