Guest User

Untitled

a guest
May 26th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2. import paho.mqtt.publish as publish
  3. import time
  4. import datetime
  5. import json
  6. import ast
  7. import requests
  8. import Queue
  9. import sys
  10. import base64
  11. import hashlib
  12. from Crypto import Random
  13. from Crypto.Cipher import AES
  14.  
  15. qmsg = Queue.Queue()
  16.  
  17. class AESCipher(object):
  18.  
  19. def __init__(self, key):
  20. self.bs = 32
  21. self.key = hashlib.sha256(key.encode()).digest()
  22.  
  23. def encrypt(self, raw):
  24. raw = self._pad(raw)
  25. iv = Random.new().read(AES.block_size)
  26. cipher = AES.new(self.key, AES.MODE_CBC, iv)
  27. return base64.b64encode(iv + cipher.encrypt(raw))
  28.  
  29. def decrypt(self, enc):
  30. enc = base64.b64decode(enc)
  31. iv = enc[:AES.block_size]
  32. cipher = AES.new(self.key, AES.MODE_CBC, iv)
  33. return self._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
  34.  
  35. def _pad(self, s):
  36. return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
  37.  
  38. @staticmethod
  39. def _unpad(s):
  40. return s[:-ord(s[len(s)-1:])]
  41.  
  42. key = "1234567890ABCDEF"
  43. key += sys.argv[1] #SSID PASSWORD FOR SALT KEY ENCRYPTION
  44. print key
  45. cipher=AESCipher(key)
  46.  
  47. def decrypt_aes256(encypted_msg):
  48. try:
  49. encypted_msg = encypted_msg.decode("utf-8")
  50. msg_aes256 = cipher.decrypt(encypted_msg)
  51. #print("MSG decrypted TYPE: {}".format(type(msg_aes256)))
  52. #print("MSG decrypted STR: {}".format(str(msg_aes256)))
  53. return msg_aes256
  54. except Exception as e:
  55. print("Exception i decrypt_aes256: {}".format(str(e)))
  56. #raise
  57.  
  58. def on_connect(client, userdata, flags, rc):
  59. print("[+] Running: on_connect")
  60. client.subscribe("test/topic")
  61.  
  62. def on_message(client, userdata, msg):
  63. print("[+] Running: on_message")
  64. print("client " + str(client))
  65. print("userdata " + str(userdata))
  66. print("msg " + str(msg))
  67. print("msg.payload " + str(msg.payload) + "\n")
  68.  
  69.  
  70. def on_subscribe(client, userdata, mid, granted_qos):
  71. print("[+] Running: on_subcribe")
  72. print("client : " +str(client))
  73. print("userdata : " +str(userdata))
  74. print("mid : " +str(mid))
  75. print("granted_qos : " +str(granted_qos))
  76.  
  77. def on_publish(mosq, obj, mid):
  78. print("[+] Running: on_publish")
  79. print("mosq: " + str(mosq))
  80. print("obj: " + str(obj))
  81. print("mid: " + str(mid))
  82.  
  83. class MqttClient(object):
  84. """docstring for MqttClient."""
  85. def __init__(self, client=mqtt.Client()):
  86. super(MqttClient, self).__init__()
  87. self.client = client
  88. self.client.on_connect = on_connect
  89. self.client.on_message = on_message
  90. self.client.connect("localhost", 1883, 60)
  91. actions = Queue.Queue()
  92.  
  93. def get_actions_queue(self):
  94. return self.actions
  95.  
  96. def get_client(self):
  97. return self.client
  98.  
  99. def set_on_connect(self, func):
  100. self.on_connect = func
  101.  
  102. def publish(message, topic):
  103. #print("Sending %s " % (message))
  104. publish.single(str(topic), message, hostname="localhost")
  105. return "Sending msg: %d " % (message)
  106.  
  107. if __name__ == "__main__":
  108. #print "Starting MQTT"
  109. mqtt = MqttClient()
  110. #mqtt.client.loop_start()
  111. topic = raw_input("Topic: ") or "test/topic"
  112. print("Subscribing to topic: " + str(topic))
  113. time.sleep(5)
  114. mqtt.client.subscribe(topic)
  115. mqtt.client.loop_forever()
Add Comment
Please, Sign In to add comment