简体   繁体   English

google.protobuf.message.DecodeError:协议缓冲区中的标记错误中的线类型错误

[英]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.

相关问题 使用 tensorflow: google.protobuf.message.DecodeError: Wrong wire type in tag - using tensorflow: google.protobuf.message.DecodeError: Wrong wire type in tag google.protobuf.message.DecodeError:解析消息时出错 - google.protobuf.message.DecodeError: Error parsing message google.protobuf.message.DecodeError:解析类型为“tensorflow.GraphDef”的消息时出错 - google.protobuf.message.DecodeError: Error parsing message with type 'tensorflow.GraphDef' 解析 Google 字体 METADATA.pb - google.protobuf.message.DecodeError: Error parsing message - Parsing Google Fonts METADATA.pb - google.protobuf.message.DecodeError: Error parsing message 如何修复:创建 tensorflow 文本摘要时出现“google.protobuf.message.DecodeError: Error parsing message” - How to Fix: "google.protobuf.message.DecodeError: Error parsing message" when creating tensorflow text summary 在运行optimize_for_inference.py时,如何修复“ google.protobuf.message.DecodeError:错误解析消息” - How to fix 'google.protobuf.message.DecodeError: Error parsing message' while running optimize_for_inference.py 使用Python解码protobuf文件时出现一些错误,错误为'google.protobuf.message.DecodeError:解码varint时太多字节。' - I have some errors when decode the protobuf file with Python,error as ‘google.protobuf.message.DecodeError: Too many bytes when decoding varint.’ Google Protobuf-C ++和Python之间的UDP通信-google.protobuf.message.DecodeError:解压缩需要长度为4的字符串参数 - Google Protobuf - UDP Communication Between C++ and Python - google.protobuf.message.DecodeError: unpack requires a string argument of length 4 python gevent websocket客户端收到回显协议缓冲区消息的错误“ DecodeError:截断的消息” - python gevent websocket client getting error “DecodeError: Truncated message” for echoed back protocol buffer message 错误消息:py2neo套接字的协议错误类型 - Error message: Protocol wrong type for socket with py2neo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM