Skip to content

Instantly share code, notes, and snippets.

@donrokzon
Created January 11, 2018 07:14
Show Gist options
  • Select an option

  • Save donrokzon/f64045bf0b378ddc79bfd5dc6c5bd7ec to your computer and use it in GitHub Desktop.

Select an option

Save donrokzon/f64045bf0b378ddc79bfd5dc6c5bd7ec to your computer and use it in GitHub Desktop.
import paho.mqtt.client as mqtt
from Crypto.Cipher import AES
import json, thread,time
MQTT_TOPIC_HOME = 'Raybaby'
MQTT_BROKER = 'iot.eclipse.org'
MQTT_PORT = 1883
sub_topic = "/stream/encrypt"
def send_ack(status):
pub_topic = "/stream/encrypt"
client.publish(pub_topic, MESSAGE)
def on_connect(client, userdata, flags, rc):
MESSAGE = "Hi_I_am_Sooraj"
print("Connected with result code " + str(rc))
sub_topic = "/stream/encrypt"
print 'sub_topic =' + sub_topic
client.subscribe(sub_topic)
time.sleep(2)
pub_topic = "/stream/encrypt"
obj = AES.new('expectoTrack6699', AES.MODE_CBC, 'coverMeTrack6699')
spa = ''
s = 16 - len(MESSAGE) % 16
if s != 0:
spa = ' ' * s
MESSAGE = MESSAGE + spa
ciphertext = obj.encrypt(MESSAGE)
print "ciphertext: ", ciphertext
data = ("".join("{:02x}".format(ord(c)) for c in ciphertext))
client.publish(pub_topic, data)
def on_message(client, userdata, msg):
print "got the message"
print(msg.topic + " " + str(msg.payload))
data = str(msg.payload)
got_data = data.decode('hex') # recieve data
print "received tcpdata: ", data # send this data
print "received cipherdata: ", got_data # send this data
obj2 = AES.new('expectoTrack6699', AES.MODE_CBC, 'coverMeTrack6699')
result = obj2.decrypt(got_data)
print "decrypted result: ", result
print "length of the result",len(result)
print result.split(' ')[0]
pub_topic = "/stream/encrypt"
obj = AES.new('expectoTrack6699', AES.MODE_CBC, 'coverMeTrack6699')
MESSAGE = "Hi_I_am_Sooraj"
spa = ''
s = 16 - len(MESSAGE) % 16
if s != 0:
spa = ' ' * s
MESSAGE = MESSAGE + spa
ciphertext = obj.encrypt(MESSAGE)
print "ciphertext: ", ciphertext
data = ("".join("{:02x}".format(ord(c)) for c in ciphertext))
client.publish(pub_topic, data)
if __name__ == '__main__':
while(1):
try:
client = mqtt.Client()
client.connect(MQTT_BROKER, MQTT_PORT, 60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
except Exception as e:
print 'Exception found >>',e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment