Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
OC Robotique
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Info_Sismondi
OC Robotique
Commits
09358678
Commit
09358678
authored
2 months ago
by
Vincent N
Browse files
Options
Downloads
Patches
Plain Diff
Fix protocole encryption
parent
eb26c6df
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Protocole/protocole.py
+14
-10
14 additions, 10 deletions
Protocole/protocole.py
with
14 additions
and
10 deletions
Protocole/protocole.py
+
14
−
10
View file @
09358678
...
...
@@ -22,7 +22,7 @@ radio.config(channel=7, address=50)
radio
.
on
()
#### Init AES ####
key
=
bytes
([
random
.
getrandbits
(
8
)
for
_
in
range
(
16
)])
key
=
bytes
([
156
,
110
,
239
,
52
,
206
,
138
,
164
,
35
,
3
,
76
,
3
,
60
,
84
,
199
,
63
,
253
])
#bytes([
random.getrandbits(8) for _ in range(16)])
iv
=
bytes
([
0
for
_
in
range
(
16
)])
cipher
=
aes
.
AES
(
key
)
...
...
@@ -108,6 +108,7 @@ def trame_to_msg(trame : bytes, userId :int):
msgObj(Message): Objet Message contenant tous les paramètres du message recu si crc et destinataire ok, sinon None
'''
trame
=
bytes_to_int
(
trame
)
print
(
trame
)
msgObj
=
Message
(
trame
[
0
],
trame
[
1
],
trame
[
2
],
trame
[
3
],
trame
[
4
:
-
1
],
trame
[
-
1
])
if
msgObj
.
crc
==
sum
(
trame
[:
-
1
])
%
256
:
if
msgObj
.
dest
!=
userId
:
...
...
@@ -129,7 +130,7 @@ def ack_msg(msg : Message):
'''
ack
=
[
msg
.
exped
,
msg
.
dest
,
msg
.
seqNum
,
ackMsgId
]
ack
+=
[
sum
(
ack
)
%
256
]
radio
.
send_bytes
(
int_to_bytes
(
ack
))
radio
.
send_bytes
(
cipher
.
encrypt_cfb
(
int_to_bytes
(
ack
)
,
iv
)
)
def
receive_ack
(
msg
:
Msg
):
...
...
@@ -148,7 +149,10 @@ def receive_ack(msg: Msg):
decrypted
=
cipher
.
decrypt_cfb
(
new_trame
,
iv
)
msgRecu
=
trame_to_msg
(
decrypted
,
msg
.
exped
)
# print("Reçu Ack : ", msgRecu.msgStr())
return
msgRecu
.
exped
==
msg
.
dest
and
msgRecu
.
dest
==
msg
.
exped
and
msgRecu
.
seqNum
==
msg
.
seqNum
and
msgRecu
.
msgId
==
ackMsgId
if
msgRecu
:
return
msgRecu
.
exped
==
msg
.
dest
and
msgRecu
.
dest
==
msg
.
exped
and
msgRecu
.
seqNum
==
msg
.
seqNum
and
msgRecu
.
msgId
==
ackMsgId
else
:
return
False
else
:
return
False
...
...
@@ -177,11 +181,10 @@ def send_msg(msgId:int, payload:List[int], userId:int, dest:int):
acked
=
False
t0
=
running_time
()
print
(
"
Envoyé :
"
,
msg
.
msgStr
())
print
(
msg
.
msgStr
())
while
not
acked
and
running_time
()
-
t0
<
Timeout
:
# print("envoi")
encrypted
=
cipher
.
encrypt_cfb
(
msg_to_trame
(
msg
)
,
iv
)
print
(
encrypted
)
trame
=
msg_to_trame
(
msg
)
encrypted
=
cipher
.
encrypt_cfb
(
trame
,
iv
)
radio
.
send_bytes
(
encrypted
)
sleep
(
tryTime
//
2
)
display
.
clear
()
...
...
@@ -206,9 +209,10 @@ def receive_msg(userId:int):
'''
new_trame
=
radio
.
receive_bytes
()
if
new_trame
:
msgRecu
=
trame_to_msg
(
new_trame
,
userId
)
decrypted
=
cipher
.
decrypt_cfb
(
new_trame
,
iv
)
msgRecu
=
trame_to_msg
(
decrypted
,
userId
)
#print("Reçu : ", msgRecu.msgStr())
if
msgRecu
.
msgId
!=
ackMsgId
:
if
msgRecu
.
msgId
!=
ackMsgId
:
# Ajouter un if msgRecu
ack_msg
(
msgRecu
)
return
msgRecu
...
...
@@ -216,11 +220,11 @@ def receive_msg(userId:int):
if
__name__
==
'
__main__
'
:
import
music
userId
=
1
userId
=
0
while
True
:
# Messages à envoyer
destId
=
2
destId
=
1
if
button_a
.
was_pressed
():
send_msg
(
1
,[
60
],
userId
,
destId
)
elif
button_b
.
was_pressed
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment