google.protobuf.message.DecodeError: Wrong wire type in tag Error in Protocol Buffer

Viewed 1450

I trying decrypt my data using google protocol buffer in python

sample.proto file:-

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

My Python code below:-

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

What i am doing wrong. can anyone help?

1 Answers

Your data is not "encrypted", it's just base64-encoded. If you use your example code and inspect your data variable, then you get:

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. 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. So you first need to define what you want: what proto message would you expect this JSON object to represent?

Related