-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsender.py
More file actions
54 lines (41 loc) · 1.66 KB
/
sender.py
File metadata and controls
54 lines (41 loc) · 1.66 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
import can
from cryptography.fernet import Fernet
from can import Message
# Initialize CAN bus interface
bus = can.interface.Bus(interface='socketcan', channel='vcan0', bitrate=2500000)
# Generate encryption key and store it in a file
key = Fernet.generate_key()
with open("keystorage.txt", 'wb') as fkey:
fkey.write(key)
print('\nEncryption key generated and stored.\n')
# Initialize Fernet cipher with the generated key
cipher = Fernet(key)
# Define the message to be sent (as a bytearray)
message = bytearray([0, 25, 0, 1, 3, 1, 4, 1])
# Convert bytearray to a string, then encode to bytes
message_string = ' '.join(map(str, message)).encode()
# Encrypt the message string
encrypted_msg = cipher.encrypt(message_string)
print("\nEncrypted message:\n", encrypted_msg)
# Reload the key from file for decryption (example of key retrieval)
with open("keystorage.txt", "rb") as fkey:
key2 = fkey.read()
cipher = Fernet(key2)
# Split the encrypted message into chunks of 8 bytes each
def split_list(lst, n):
"""Splits a list into chunks of size n."""
return [lst[i:i + n] for i in range(0, len(lst), n)]
# Convert encrypted message to a list of hexadecimal values
split_string = encrypted_msg.decode()
hex_data = [ord(c) for c in split_string]
# Split the hexadecimal data into chunks suitable for CAN messages
n = 8
encrypted_data = split_list(hex_data, n)
# Send the encrypted data over the CAN bus
try:
for enc_data in encrypted_data:
data = Message(data=enc_data)
bus.send(data)
print("\nMessage sent on {}\n".format(bus.channel_info))
except can.CanError as e:
print(f"CAN error: {e}")