Python3: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
Python3: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
我又在处理MQTT通信了。
我正在编写一个实现此通信的 python 程序。
在发布者脚本中,我正在加密消息(使用 AES 加密)并将其发送给将解密该消息的订阅者。
当我 运行 程序时,我在订阅者部分收到该错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
我已经搜索了解决方案,但没有任何结果!
这是运行良好的发布者脚本
import paho.mqtt.client as mqtt
import time
from os import urandom
from Crypto.Cipher import AES
port = 1883
brocker = "localhost"
message = "MessageToEncrypt"
topic = "Auth"
secret_key = urandom(16)
iv = urandom(16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)
encrypted_text = obj.encrypt(message)
def on_publish(client,userdata,mid):
#the mid value is the message ID
print("on_publish callback mid: "+str(mid))
print("The message published is: " + message)
print("Encrypted msg: "+str(encrypted_text))
print(type(encrypted_text))
def on_connect(client, userdata, flags, rc):
print("Client connected with this connection code: "+str(rc))
def on_disconnect(client,userdata,rc):
print("client disconnected \n")
def main():
try:
clt = mqtt.Client("client1")
print("Client created successfully \n")
clt.on_connect = on_connect
clt.on_publish = on_publish
clt.connect(brocker,port)
print("Client connected \n")
ret = clt.publish(topic,encrypted_text)
time.sleep(4)
print("The publish result is : "+str(ret)+"\n")
clt.on_disconnect = on_disconnect
clt.disconnect()
except Exception as inst:
print("\n Exception found \n")
print(type(inst))
print(inst.args)
print(inst)
print("\n")
main()
这是订阅者脚本:
import paho.mqtt.client as mqtt
import time
from os import urandom
from Crypto.Cipher import AES
port = 1883
brocker = "localhost"
topic = "Auth"
secret_key = urandom(16)
iv = urandom(16)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
def on_connect(client, userdata, flags, rc):
print("Client connected with this connection code: " +str(rc))
def on_message(client, userdata, message):
#print("on_message function")
time.sleep(1)
msg_received = message.payload
print(type(msg_received))
print("the encrypred message rceived is :")
print(msg_received)
decrypted_text = rev_obj.decrypt(msg_received)
print("The decrypted text", decrypted_text.decode('utf-8'))
def main():
try:
clt = mqtt.Client()
print(" Client created successfully \n")
clt.on_connect = on_connect
clt.on_message = on_message
clt.connect(brocker,port)
clt.loop_start()
clt.subscribe(topic)
time.sleep(4)
#print("subscribtion: successful \n")
clt.loop_stop()
except Exception as inst:
print("\n Exception found \n")
print(type(inst))
print(inst.args)
print(inst)
print("\n")
main()
这是我 运行 发布者脚本
时的输出
Client created successfully
Client connected
on_publish callback mid: 1
The message published is: MessageToEncrypt
Encrypted msg: b'c\xf1\x9b\xfca\x081\x8e\xa1+\xe9t\x96\xdei\xdb'
<class 'bytes'>
The publish result is : (0, 1)
client disconnected
如果您有任何可以帮助的建议,请不要犹豫。
提前谢谢你
您的发布者和订阅者生成不同的随机密钥 + iv
因此您的订阅者无法成功解密消息,因为它没有正确的密钥 + iv
所以你从解密函数中得到的只是垃圾随机噪声,无法保证它是有效的 utf-8
订阅者需要与发布者拥有相同的密钥+iv。
这与MQTT无关
我又在处理MQTT通信了。 我正在编写一个实现此通信的 python 程序。 在发布者脚本中,我正在加密消息(使用 AES 加密)并将其发送给将解密该消息的订阅者。
当我 运行 程序时,我在订阅者部分收到该错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte
我已经搜索了解决方案,但没有任何结果!
这是运行良好的发布者脚本
import paho.mqtt.client as mqtt
import time
from os import urandom
from Crypto.Cipher import AES
port = 1883
brocker = "localhost"
message = "MessageToEncrypt"
topic = "Auth"
secret_key = urandom(16)
iv = urandom(16)
obj = AES.new(secret_key, AES.MODE_CBC, iv)
encrypted_text = obj.encrypt(message)
def on_publish(client,userdata,mid):
#the mid value is the message ID
print("on_publish callback mid: "+str(mid))
print("The message published is: " + message)
print("Encrypted msg: "+str(encrypted_text))
print(type(encrypted_text))
def on_connect(client, userdata, flags, rc):
print("Client connected with this connection code: "+str(rc))
def on_disconnect(client,userdata,rc):
print("client disconnected \n")
def main():
try:
clt = mqtt.Client("client1")
print("Client created successfully \n")
clt.on_connect = on_connect
clt.on_publish = on_publish
clt.connect(brocker,port)
print("Client connected \n")
ret = clt.publish(topic,encrypted_text)
time.sleep(4)
print("The publish result is : "+str(ret)+"\n")
clt.on_disconnect = on_disconnect
clt.disconnect()
except Exception as inst:
print("\n Exception found \n")
print(type(inst))
print(inst.args)
print(inst)
print("\n")
main()
这是订阅者脚本:
import paho.mqtt.client as mqtt
import time
from os import urandom
from Crypto.Cipher import AES
port = 1883
brocker = "localhost"
topic = "Auth"
secret_key = urandom(16)
iv = urandom(16)
rev_obj = AES.new(secret_key, AES.MODE_CBC, iv)
def on_connect(client, userdata, flags, rc):
print("Client connected with this connection code: " +str(rc))
def on_message(client, userdata, message):
#print("on_message function")
time.sleep(1)
msg_received = message.payload
print(type(msg_received))
print("the encrypred message rceived is :")
print(msg_received)
decrypted_text = rev_obj.decrypt(msg_received)
print("The decrypted text", decrypted_text.decode('utf-8'))
def main():
try:
clt = mqtt.Client()
print(" Client created successfully \n")
clt.on_connect = on_connect
clt.on_message = on_message
clt.connect(brocker,port)
clt.loop_start()
clt.subscribe(topic)
time.sleep(4)
#print("subscribtion: successful \n")
clt.loop_stop()
except Exception as inst:
print("\n Exception found \n")
print(type(inst))
print(inst.args)
print(inst)
print("\n")
main()
这是我 运行 发布者脚本
时的输出Client created successfully
Client connected
on_publish callback mid: 1
The message published is: MessageToEncrypt
Encrypted msg: b'c\xf1\x9b\xfca\x081\x8e\xa1+\xe9t\x96\xdei\xdb'
<class 'bytes'>
The publish result is : (0, 1)
client disconnected
如果您有任何可以帮助的建议,请不要犹豫。 提前谢谢你
您的发布者和订阅者生成不同的随机密钥 + iv
因此您的订阅者无法成功解密消息,因为它没有正确的密钥 + iv
所以你从解密函数中得到的只是垃圾随机噪声,无法保证它是有效的 utf-8
订阅者需要与发布者拥有相同的密钥+iv。
这与MQTT无关