jieter / python-lora

Decrypt LoRa payloads in python.
MIT License
67 stars 18 forks source link

Unable to decode the packet #2

Closed hecko closed 8 years ago

hecko commented 8 years ago

Everyone, can please anyone give me a reference on whetner Im decoding the packet from TTL network correctly?

The original data pushed into LoRaWAN client (TX) module before encryption: 0x0102030405060708 This is the data from the LoRaWAN concentrator (RX): u'data': u'QGyoHrSACgACb3nY9sWjyQG6P/dE', This data is supposed to be encoded in Base64 ( according to section 4 https://github.com/Lora-net/packet_forwarder/blob/master/PROTOCOL.TXT > hash key "data")

Sending from deviceid 0xB41EA86C as packet type U on port 2, AppSessionKey: 82-0E-B5-12-7B-0B-98-C8-CC-0B-7E-E4-32-53-E0-D1, packet IS decoded correctly on TheThingsNetwork backend (frame number 10 (i believe this is the sequence number in TheThingsNetwork terminology (confirm?)).

So LoRaWAN stuff works, but Im not able to use this Python module to decrypt the information:

I prepare payload as such: payload = base64.decodestring(p).encode('hex') but the output data is: [46, 23, 115, 236, 116, 37, 196, 9, 80, 110, 126, 32, 183, 20, 136, 96, 100, 221, 95, 37, 112], which by far does not resemble 0x0102030405060708

#!/usr/bin/python

import base64
from lora.crypto import loramac_decrypt

p = "QGyoHrSACgACb3nY9sWjyQG6P/dE"
sequence_counter = 10

print("p: " + p)
payload = base64.decodestring(p).encode('hex')
print("Payload: " + payload)
key = '820EB5127B0B98C8CC0B7EE43253E0D1'
dev_addr = 'B41EA86C'

out = loramac_decrypt(payload, sequence_counter, key, dev_addr)
print out
p: QGyoHrSACgACb3nY9sWjyQG6P/dE
Payload: 406ca81eb4800a00026f79d8f6c5a3c901ba3ff744
[46, 23, 115, 236, 116, 37, 196, 9, 80, 110, 126, 32, 183, 20, 136, 96, 100, 221, 95, 37, 112]

Expecting: [ 1, 2, 3, 4, 5, 6, 7, 8]

Am I missing something fundamental here? Thanks for ANY leads!

hecko commented 8 years ago

Im an ass. Sorry to bother - realized that the data payload is PHYPayload, not a FRMPayload which this package accepts. :)

jieter commented 8 years ago

No problem, never mind.

jieter commented 8 years ago

Could use a quick line in the readme to clarify this for future cases...

hecko commented 8 years ago

I was actually about to do that and post you a push request, but wasn't sure about your preffered method to do this.

Anyways - the code that works is here:

https://github.com/hecko/lorawan_server/blob/master/test.py

jieter commented 8 years ago

Changed the readme/docstrings here and there: bd7903fb5d784697991b016e859850cf1b3c67ee

Feel free to just open a pull request for future cases.