[英]google.protobuf.message.DecodeError: Wrong wire type in tag Error in Protocol Buffer
I trying decrypt my data using google protocol buffer in python我尝试使用 python 中的谷歌协议缓冲区解密我的数据
sample.proto file:- sample.proto 文件:-
syntax = "proto3";
message SimpleMessage {
string deviceID = 1;
string timeStamp = 2;
string data = 3;
}
After that, I have generated python files using the proto command:- protoc --proto_path=./ --python_out=./ simple.proto
之后,我使用 proto 命令生成了 python 文件:- protoc --proto_path=./ --python_out=./ simple.proto
My Python code below:-我的 Python 代码如下:-
import json
import simple_pb2
import base64
encryptedData = 'iOjEuMCwic2VxIjoxODEsInRtcyI6IjIwMjEtMDEtMjJUMTQ6MDY6MzJaIiwiZGlkIjoiUlFI'
t2 = bytes(encryptedData, encoding='utf8')
print(encryptedData)
data = base64.b64decode(encryptedData)
test = simple_pb2.SimpleMessage()
v1 = test.ParseFromString(data)
While executing above code getting error:- google.protobuf.message.DecodeError: Wrong wire type in tag Error执行上述代码时出现错误:- google.protobuf.message.DecodeError: Wrong wire type in tag Error
What i am doing wrong.我做错了什么。 can anyone help?谁能帮忙?
Your data is not "encrypted", it's just base64-encoded.您的数据没有“加密”,它只是 base64 编码。 If you use your example code and inspect your data
variable, then you get:如果您使用您的示例代码并检查您的data
变量,那么您会得到:
import base64
data = base64.b64decode(b'eyJ2ZXIiOjEuMCwic2VxIjoxODEsInRtcyI6IjIwMjEtMDEtMjJUMTQ6MDY6MzJaIiwiZGlkIjoiUlFIVlRKRjAwMDExNzY2IiwiZG9wIjoxLjEwMDAwMDAyMzg0MTg1NzksImVyciI6MCwiZXZ0IjoiVE5UIiwiaWdzIjpmYWxzZSwibGF0IjoyMi45OTI0OTc5OSwibG5nIjo3Mi41Mzg3NDgyOTk5OTk5OTUsInNwZCI6MC4wfQo=')
print(data)
> b'{"ver":1.0,"seq":181,"tms":"2021-01-22T14:06:32Z","did":"RQHVTJF00011766","dop":1.1000000238418579,"err":0,"evt":"TNT","igs":false,"lat":22.99249799,"lng":72.538748299999995,"spd":0.0}\n'
Which is evidently a piece of of JSON data, not a binary-serialized protocol buffer - which is what ParseFromString
expects.这显然是 JSON 数据的一部分,而不是二进制序列化的协议缓冲区 - 这是ParseFromString
期望的。 Also, looking at the names and types of the fields, it looks like this payload just doesn't match the proto definition you've shown.此外,查看字段的名称和类型,看起来此有效负载与您显示的原型定义不匹配。
There are certainly ways to parse a JSON into a proto, and even to control the field names in that transformation, but not even the number of fields match directly.当然有办法将 JSON 解析为原型,甚至可以控制该转换中的字段名称,但甚至字段数量都不能直接匹配。 So you first need to define what you want : what proto message would you expect this JSON object to represent?所以你首先需要定义你想要什么:你希望这个 JSON object 代表什么原始消息?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.