The LoRaWAN protocol has a specific way of encrypting data (page 20, chapter 4.3.3.1), and I'm trying to do it in C#.
First, a sequence A with blocks with a size of 16 bytes each A1..An
is created. Then they are encrypted to get a sequence S
of blocks Si
:
Si = aes128_encrypt(K, Ai) for i = 1..n
S = S1 | S2 | .. | Sn
And finally the payload is encrypted:
(pld | pad16) xor S
truncation of the first len(payload) bytes
So far I managed to generate the blocks, but I'm new to cryptography and have no idea on how to proceed, can anyone help?
Here is the encryption script in python.
Here is the encryption script in js.
Here is the encryption script in c++.
And this is the C# code I have so far:
public string encrypt(string payload, string dev_eui, string dev_addr, int counter_up)
{
try
{
byte[] devaddr = StringToByteArray(dev_addr);
byte[] aBlock = {
0x01,
0x00,
0x00,
0x00,
0x00,
0,
devaddr[3],
devaddr[2],
devaddr[1],
devaddr[0],
(byte)(counter_up & 0xff),
(byte)((counter_up >> 8) & 0xff),
(byte)((counter_up >> 16) & 0xff),
(byte)((counter_up >> 24) & 0xff),
0x00,
0x00 };
\\...