cryptor.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import socket
  2. import threading
  3. import config as cfg
  4. from nacl.public import PrivateKey
  5. from connection.socket import SocketClient
  6. from tcp import TLSClientSession
  7. import time
  8. def new_x25519():
  9. private_key = PrivateKey.generate()
  10. key_exchange = bytes(private_key.public_key)
  11. return bytes(private_key).hex() , key_exchange.hex()
  12. class Cryptor():
  13. def __init__(self, parser):
  14. self.privatekey , self.key_exchange = new_x25519()
  15. self.server = None
  16. self.parser = parser
  17. self.connector = None
  18. self.data = []
  19. print("\n\n CRYPTOR, \n ",self.privatekey ,self.key_exchange,"\n")
  20. def oracleInitial(self,data):
  21. try:
  22. self.server = data["server"]
  23. self.connector = SocketClient(data["connector"][0],data["connector"][1],self.parser,"Cryptor")
  24. time.sleep(1)
  25. self.keyExchange()
  26. except:
  27. print("\33[41mAn exception occurred oracleInitial Cryptor\33[0m" )
  28. # def socketOracle(self,HOST,PORT):
  29. # self.connector = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  30. # self.connector.connect((HOST, PORT))
  31. # t = threading.Thread(target=self.receive)
  32. # t.start()
  33. # self.toSEND({
  34. # "type":"oracleInit",
  35. # "oracle":"Cryptor"
  36. # })
  37. # print("socket is created and Listener is created\n")
  38. # self.toSEND({
  39. # "type":"key_exchange",
  40. # "key_exchange":self.key_exchange
  41. # })
  42. def keyExchange(self):
  43. try:
  44. d = {}
  45. self.parser.add(d,"type","keyExchange")
  46. self.parser.add(d,"keyExchange",self.key_exchange)
  47. self.connector.send(self.parser.toSEND(d))
  48. except:
  49. print("\33[41mAn exception occurred keyExchange Cryptor\33[0m")
  50. # def start(self,a):
  51. # print("Cryptor",a)
  52. # self.socketOracle(self.connectorIP,self.connectorPort)
  53. def fromContoCryHelloCLient(self, data):
  54. try:
  55. self.tcp = TLSClientSession(self.privatekey , self.key_exchange,memoryview(bytes.fromhex(data["data"])))
  56. print("TODO fromContoCry",data["data"]) # TODO
  57. except Exception as exception :
  58. print("\33[41mAn exception occurred fromContoCryHelloCLient Cryptor " ,exception,"\33[0m" )
  59. def toCryptorFromConnector(self,data):
  60. try:
  61. print("TODO") # TODO
  62. except Exception as exception :
  63. print("\33[41mAn exception occurred toCryptorFromConnector Cryptor " ,exception,"\33[0m" )
  64. def MPC(self,data):
  65. try:
  66. print("TODO") # TODO
  67. except Exception as exception :
  68. print("\33[41mAn exception occurred MPC Cryptor " ,exception,"\33[0m" )
  69. def fromContoCry(self,data):
  70. try:
  71. self.tcp.tls_response(data["data"])
  72. print("TODO fromContoCry") # TODO
  73. except Exception as exception :
  74. print("\33[41mAn exception occurred fromContoCry Cryptor " ,exception,"\33[0m" )