Python 只能将字节字符串传递给C代码。巨蟒帕拉米科
我有一个使用python Paramiko的简单客户机-服务器程序 客户端向服务器发送一个加密字符串,服务器需要对其进行解密以进行处理 这是客户:Python 只能将字节字符串传递给C代码。巨蟒帕拉米科,python,ssh,aes,paramiko,pycryptodome,Python,Ssh,Aes,Paramiko,Pycryptodome,我有一个使用python Paramiko的简单客户机-服务器程序 客户端向服务器发送一个加密字符串,服务器需要对其进行解密以进行处理 这是客户: def collect_stats(): try: cpu = psutil.cpu_percent(interval=1) memory = psutil.virtual_memory().percent disk = psutil.disk_usage('/').percent
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)
服务器拾取字符串并尝试对其进行解码(在这个代码剪报的底部):
但是,服务器中的解码方法引发以下错误:
FDSDSFDSF:只能将字节字符串传递给C代码
但是,传递给该方法的是:
b'\xb5\xf7\xbc\xd5\xfc\xff;\x83\xd3\xab\xb1mc\xc3'
已编码。您的
密文是一个字符串,包含字节对象的名称。我假设您的服务器和客户机运行在Python3上,以此类推
print(str_to_send_back)
在中,客户端仅将字节对象的表示形式打印到标准输出。尝试对字节使用一些字符串友好的编码,例如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))
您的ciphertext
是一个包含字节对象的字符串。我假设您的服务器和客户机运行在Python3上,以此类推
print(str_to_send_back)
在中,客户端仅将字节对象的表示形式打印到标准输出。尝试对字节使用一些字符串友好的编码,例如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))
将您使用的Python主要版本添加到问题中,并将打印(键入(密文))
添加到解密中。我怀疑你的数据不是你想象的那样。通常情况下,最好在问题中包含完整的回溯,而不仅仅是异常对象的字符串表示。它返回str。我将它放在如下字节中:print(bytes(ciphertext,encoding=“utf8”)),但现在它返回:b“b”\\xb5\\xf2\\x81\\x89\\x99\\xe9\\xbd\\xbb\\x84\\xec\\xc3r\\xa4'\r\n如果不捕获异常并对其进行格式化,则可能会提供更多的信息;您确定它在obj2.decrypt
或AES.new
上失败了吗?如果print(ciphertext)
正在打印b'\x...
它实际上可能是一个字符串“b'\\x...”
尝试print(repr(ciphertext))
@NickT它会打印以下内容:“b”\\xb5\\xf4\\xe1\\\x18=\\xb3cJ\\xfe\\xd3u\\x9f\\xd6”\r\n“添加您使用的Python的主要版本并打印(键入(密文))
到解密
。我怀疑你的数据不是你想象的那样。通常情况下,最好在问题中包含完整的回溯,而不仅仅是异常对象的字符串表示。它返回str。我将它放在如下字节中:print(bytes(ciphertext,encoding=“utf8”)),但现在它返回:b“b”\\xb5\\xf2\\x81\\x89\\x99\\xe9\\xbd\\xbb\\x84\\xec\\xc3r\\xa4'\r\n如果不捕获异常并对其进行格式化,则可能会提供更多的信息;您确定它在obj2.decrypt
或AES.new
上失败了吗?如果print(ciphertext)
正在打印b'\x...
它实际上可能是一个字符串“b'\\x...”
Tryprint(repr(ciphertext))
@NickT,它会打印以下内容:“b”\\xb5\\xf4\\xe1\'\\x18=\\xb3cJ\\xfe\\xd3u\\x9f\\xd6”\r\n