Advertisement
Guest User

nuovo

a guest
Jan 25th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. #!/usr/bin/python
  2. #coding: utf-8
  3. import socket
  4. import sys
  5. import time
  6. from Crypto.Cipher import XOR
  7. import base64
  8. import binascii
  9. import threading #fix
  10. ######lo compiliamo assieme questo###
  11. keyz = "sdhsfd3434YYAI@pogna"                          #
  12. ip = "51.178.52.91"                         #
  13. #####################################
  14. #incorrect padding
  15. #keyboard interrupt
  16. #login, who, auth, part
  17. try:
  18.     username = sys.argv[1]
  19. except IndexError:
  20.     print("Utilizzo: python client_chat.py nickname")
  21.     exit()
  22. def encrypt(msg, key):
  23.     key = key
  24.     cipher = XOR.new(key)
  25.     encrypted_msg = base64.b64encode(cipher.encrypt(msg))
  26.     return encrypted_msg.decode("utf-8")
  27.  
  28. def decrypt(crypted_msg, key):
  29.     key = key
  30.     cipher = XOR.new(key)
  31.     try:
  32.         decrypted_msg = cipher.decrypt(base64.b64decode(crypted_msg))
  33.     except binascii.Error:
  34.         return crypted_msg
  35.     except TypeError:
  36.         return crypted_msg
  37.     return decrypted_msg
  38.  
  39.  
  40.  
  41. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  42. try:
  43.     s.connect((ip, 8934))
  44. except socket.error:
  45.     print("Controlla la connessione.")
  46.     exit()
  47. def read():  #fix
  48.     while True:  #fix
  49.         data = s.recv(1024)  #fix
  50.         user = data.split(":")[0]  #fix
  51.         try:
  52.             msg = decrypt(data.split(":")[1].strip(), keyz)   #fix
  53.             if "[104]" in data:
  54.                 user = data.split(":")[1]
  55.                 print("{} connesso".format(user))
  56.             else:
  57.                 print("<{}>: {}".format(user, msg))  #fix
  58.         except IndexError:
  59.             print(data)
  60.            
  61. def write():  #fix
  62.     message = raw_input()  #fix
  63.     time.sleep(0.1)
  64.     s.send(encrypt("{}:{}".format(username,message),keyz))  # fix
  65.    
  66. t = threading.Thread(target=read, args=[])  #fix
  67. t.setDaemon(True)
  68. s.send(encrypt("{}:{}".format(username,"!hello!3498758972389712387"),keyz))
  69. #fix
  70. try:
  71.     t.start()  #fix
  72.     while True:
  73.         write()  #fix
  74. except KeyboardInterrupt:
  75.     print("Programma interrotto dall'utente (ctrl+c)")
  76.     exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement