-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlora_tx_crypt.py
More file actions
58 lines (47 loc) · 1.33 KB
/
lora_tx_crypt.py
File metadata and controls
58 lines (47 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# LoRa TX with Encryption
import time
from cryptolib import aes
import os
# Encryption/Decryption key
# REPLACE WITH YOUR OWN
CRYPT_KEY = b"OYc0MeKTjZS8vl2KNNEIIt6aWa8sVFfC"
# After replacing keys with your own, switch to False to disable the warning
KEY_INSECURE = True
LORA_CFG = {
"freq_khz": 915000,
"sf": 7,
"bw": 125,
"coding_rate": 5,
"preamble_len": 8,
"output_power": 5,
"crc": True,
"invert_iq": False,
}
def pad(data: bytes) -> bytes:
pad_len = 16 - (len(data) % 16)
return data + bytes([pad_len]) * pad_len
def encrypt(message: bytes) -> bytes:
iv = os.urandom(16) # Generate random IV
cipher = aes(CRYPT_KEY, 2, iv)
return iv + cipher.encrypt(pad(message)) # packet = iv + ciphertext
def update_display(counter):
display.fill(0)
display.text("Sending...", 0, 0, 1)
display.text(str(counter), 0, 12, 1)
if KEY_INSECURE:
display.text("Warning: Insecure Key!", 0, 24, 1)
display.show()
def main():
print("Initializing...")
modem = get_modem(LORA_CFG)
display.fill(0)
display.text("Sending...", 0, 0, 1)
display.show()
counter = 0
while True:
print("Sending...")
update_display(counter)
modem.send(encrypt(f"Hello world from MicroPython #{counter}".encode()))
time.sleep(2)
counter += 1
main()