只有字节字符串可以传递给C代码。 python paramiko

问题描述 投票:0回答:1

我有一个使用python Paramiko的简单客户端 - 服务器程序。

客户端将加密的字符串发送到服务器,服务器需要对其进行解密以对其进行处理。

这是客户:

def collect_stats():
    try:
        cpu = psutil.cpu_percent(interval=1)
        memory = psutil.virtual_memory().percent
        disk = psutil.disk_usage('/').percent
        str_to_send_back = "{} {} {}".format(cpu, memory, disk).strip()
        str_to_send_back = str_to_send_back.encode()
        str_to_send_back = encrypt(str_to_send_back)
        print(str_to_send_back)

服务器获取字符串并尝试解码它(在此代码片段的底部):

class server:
    command='cd %temp% && cd' # Necessary to pull client temp directory path
    run_client_script = 'cd %temp% && python client.py' # To run client script

    def __init__(self, client_ip, cliet_port, username, password):
        self.client_ip = client_ip
        self.cliet_port = cliet_port
        self.username = username
        self.password = password
        self.ssh = ssh(self.client_ip, self.username, self.password)

    def upload_script_and_get_stats(self):
        try:
            # Built temp directory path for client machine
            client_temp_dir_location = str(self.ssh.send_command(self.command)).strip()

            # Client script file name
            file_name = "\\client.py"

            # Build absolute path of source file
            source_file_location = os.path.dirname(os.path.abspath(__file__)) + file_name

            # Build absolute path of destination file
            destination_file_location = client_temp_dir_location + file_name

            # Upload client script
            ftp_client = self.ssh.open_sftp(source_file_location, destination_file_location)

            # Run client script and get result
            result = self.ssh.send_command(self.run_client_script)

            # SERVER DECODES STRING HERE
            result = self.decrypt(result)
            return str(result)


        except Exception as e:
            print("Oops this error occured in transfer_files_to_client(): " + str(e))
        finally:
            self.ssh.close()

    def decrypt(self, ciphertext):
        try:
            print(ciphertext)

            obj2 = AES.new(b'This is a key123', AES.MODE_CFB, b'This is an IV456')
            return obj2.decrypt(ciphertext)
        except Exception as e:
            print("FDSDSFDSF: "+str(e))

但是,服务器中的decode方法会抛出此错误:

FDSDSFDSF:只能将字节字符串传递给C代码

但是传递给方法的是:

b'\xb5\xf7\xbc\xd5\xfc\xff;\x83\xd3\xab\xb1mc\xc3'

已编码。

python ssh aes paramiko pycryptodome
1个回答
2
投票

你的ciphertext是一个包含字节对象的string representation的字符串。我假设您的服务器和客户端都在Python 3上运行

print(str_to_send_back)

在客户端只是将bytes对象的表示打印到标准输出。尝试对字节使用一些字符串友好的编码,例如base64:

from base64 import b64encode

def collect_stats():
    try:
        ...
        # str_to_send_back is actually bytes
        # Decode the bytes that contain ASCII text for printing
        print(b64encode(str_to_send_back).decode('ascii'))

并在接收端解码:

from base64 import b64decode

def decrypt(self, ciphertext):
    # Removed the over eager "exception handling". The traceback is a
    # lot more informative and useful. Add *specific* exception handling
    # as necessary
    aes = AES.new(b'This is a key123', AES.MODE_CFB, b'This is an IV456')
    return aes.decrypt(b64decode(ciphertext))
© www.soinside.com 2019 - 2024. All rights reserved.