Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-020
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
O.V.B Sewmina
2022-020
Commits
5694faf9
Commit
5694faf9
authored
Oct 08, 2022
by
A.P.R.C. Abeyrathna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MQTT Added
parent
778498a4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
130 additions
and
0 deletions
+130
-0
MQTT Encryption/publish-payload-encrypt .py
MQTT Encryption/publish-payload-encrypt .py
+42
-0
MQTT Encryption/publish-subscribe-payload-encrypt.py
MQTT Encryption/publish-subscribe-payload-encrypt.py
+45
-0
MQTT Encryption/subscribe-payload-encrypt.py
MQTT Encryption/subscribe-payload-encrypt.py
+43
-0
No files found.
MQTT Encryption/publish-payload-encrypt .py
0 → 100644
View file @
5694faf9
import
time
import
paho.mqtt.client
as
paho
from
cryptography.fernet
import
Fernet
broker
=
"broker.hivemq.com"
# broker="192.168.1.206"
# define callback
def
on_log
(
client
,
userdata
,
level
,
buf
):
print
(
"log: "
,
buf
)
def
on_message
(
client
,
userdata
,
message
):
# time.sleep(1)
print
(
"receive payload "
,
message
.
payload
)
decrypted_message
=
cipher
.
decrypt
(
message
.
payload
)
# decrypted_message = cipher.decrypt(encrypted_message)
print
(
"
\n
received message ="
,
str
(
decrypted_message
.
decode
(
"utf-8"
)))
client
=
paho
.
Client
(
"client-pub"
)
# create client object client1.on_publish = on_publish #assign function to callback client1.connect(broker,port) #establish connection client1.publish("house/bulb1","on")
client
.
on_log
=
on_log
######
client
.
on_message
=
on_message
#####encryption
# cipher_key = Fernet.generate_key()
cipher_key
=
b
'WDrevvK8ZrPn8gmiNFjcOp2xovBr40TCwJlZOyI94IY='
cipher
=
Fernet
(
cipher_key
)
message
=
b
'on33'
# message = b'the quick brown fox jumps over the lazy dog'
encrypted_message
=
cipher
.
encrypt
(
message
)
out_message
=
encrypted_message
.
decode
()
# turn it into a string to send
##
print
(
"connecting to broker "
,
broker
)
client
.
connect
(
broker
)
# connect
print
(
"publishing encrypted message "
,
encrypted_message
)
# out_message="on"
client
.
publish
(
"house/bulb1"
,
out_message
)
# publish
time
.
sleep
(
4
)
client
.
disconnect
()
# disconnect
client
.
loop_stop
()
# stop loop
MQTT Encryption/publish-subscribe-payload-encrypt.py
0 → 100644
View file @
5694faf9
import
time
import
paho.mqtt.client
as
paho
from
cryptography.fernet
import
Fernet
broker
=
"broker.hivemq.com"
# define callback
def
on_log
(
client
,
userdata
,
level
,
buf
):
print
(
"log: "
,
buf
)
def
on_message
(
client
,
userdata
,
message
):
# time.sleep(1)
print
(
"receive payload "
,
message
.
payload
)
if
message
.
payload
==
encrypted_message
:
print
(
"
\n
published and received messages are the same"
)
decrypted_message
=
cipher
.
decrypt
(
message
.
payload
)
# decrypted_message = cipher.decrypt(encrypted_message)
print
(
"
\n
received message ="
,
str
(
decrypted_message
.
decode
(
"utf-8"
)))
client
=
paho
.
Client
(
"client-001"
)
# create client object client1.on_publish = on_publish
client
.
on_log
=
on_log
client
.
on_message
=
on_message
# encryption
cipher_key
=
Fernet
.
generate_key
()
cipher
=
Fernet
(
cipher_key
)
message
=
b
'on'
# message = b'the quick brown fox jumps over the lazy dog'
encrypted_message
=
cipher
.
encrypt
(
message
)
out_message
=
encrypted_message
.
decode
()
# turn it into a string to send
##
print
(
"connecting to broker "
,
broker
)
client
.
connect
(
broker
)
# connect
client
.
loop_start
()
# start loop to process received messages
print
(
"subscribing "
)
client
.
subscribe
(
"house/bulb1"
)
# subscribe
time
.
sleep
(
2
)
print
(
"publishing encrypted message "
,
encrypted_message
)
# message="on"
client
.
publish
(
"house/bulb1"
,
out_message
)
# publish
time
.
sleep
(
4
)
client
.
disconnect
()
# disconnect
client
.
loop_stop
()
# stop loop
MQTT Encryption/subscribe-payload-encrypt.py
0 → 100644
View file @
5694faf9
import
time
import
paho.mqtt.client
as
paho
from
cryptography.fernet
import
Fernet
broker
=
"broker.hivemq.com"
# broker="192.168.1.206"
# define callback
def
on_log
(
client
,
userdata
,
level
,
buf
):
print
(
"log: "
,
buf
)
def
on_message
(
client
,
userdata
,
message
):
# time.sleep(1)
print
(
"receive payload "
,
message
.
payload
)
decrypted_message
=
cipher
.
decrypt
(
message
.
payload
)
# decrypted_message = cipher.decrypt(encrypted_message)
print
(
"
\n
received message ="
,
str
(
decrypted_message
.
decode
(
"utf-8"
)))
client
=
paho
.
Client
(
"client-001"
)
# create client object client1.on_publish = on_publish #assign function
# to callback client1.connect(broker,port) #establish connection client1.publish(
# "house/bulb1","on")
client
.
on_log
=
on_log
######
client
.
on_message
=
on_message
#####encryption
cipher_key
=
b
'WDrevvK8ZrPn8gmiNFjcOp2xovBr40TCwJlZOyI94IY='
cipher
=
Fernet
(
cipher_key
)
print
(
"connecting to broker "
,
broker
)
client
.
connect
(
broker
)
# connect
client
.
loop_start
()
# start loop to process received messages
print
(
"subscribing "
)
client
.
subscribe
(
"house/bulb1"
)
# subscribe
count
=
0
while
count
<
60
:
time
.
sleep
(
1
)
count
+=
1
client
.
disconnect
()
# disconnect
client
.
loop_stop
()
# stop loop
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment