Python3:UnicodeDecodeError:“utf-8”编解码器无法解码位置 1 中的字节 0x83:起始字节无效

发布于 2025-01-19 05:26:05 字数 3271 浏览 2 评论 0原文

我再次处理MQTT通信。 我正在编写一个实施此通信的Python程序。 在发布者脚本中,我正在加密消息(使用AES加密),并将其发送给将解密该消息的订户。

当我运行程序时,IAM在订户部分中遇到该错误:

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 

如果您有任何建议可以帮助您,请随时不要犹豫。 先感谢您

I am dealing with MQTT communication again.
I am writting a python program which implement this communication.
In the publisher script, I am encrypting a message (using AES encryption) and sending it to the subscriber who will decrypt that message.

When I run the program, Iam getting that error in the subscriber part:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x83 in position 1: invalid start byte

I've searched for the solution but without any result !

This is the publisher script which works fine

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()

and this is the subscriber script:

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()

and this is the output when I run the publisher script

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 

Please don't hesitate if you have any suggestion that can help.
Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2025-01-26 05:26:05

您的发布者和订阅者都生成不同的随机密钥 + iv

因此您的订阅者无法成功解密消息,因为它没有正确的密钥 + iv

所以您从解密函数中得到的只是垃圾随机噪声无法保证是有效的 utf-8

订阅者需要与发布者具有相同的密钥 + iv。

这与MQTT无关

Both your publisher and subscriber are generating different random key + iv

So there is no way your subscriber can successfully decrypt the message as it doesn't have the right key + iv

So what you get out of the decrypt function is just garbage random noise which is no way guaranteed to be valid utf-8

The subscriber needs to have the same key + iv as the publisher.

This has nothing to do with MQTT

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文