Sweetening

P2P Chat System

Oct 15th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import socket
  5. import base64
  6. import random
  7. import string
  8. import hashlib
  9. from Crypto.Cipher import AES
  10. from Crypto import Random
  11. from threading import Thread
  12. import getpass
  13. import os
  14.  
  15. class CryptoClient(object):
  16. def __init__(self):
  17. print("Welcome to CryptoChat, a secure P2P chat client coded by Me")
  18. print("If you don't know what you're doing, read the README.md!!!")
  19. self.IP = input("Please enter the IP address you wish to chat with: ")
  20. self.PORT = int(input("Enter the port for communication: "))
  21. print()
  22. print("Now enter the keys for the different encryption methods, make sure they are different.")
  23. print("Please note they will not be printed for your security.")
  24. print()
  25. self.EncryptKeyXOR = getpass.getpass("Enter desired key for XOR encryption: ")
  26. self.EncryptKeyAES = hashlib.md5(getpass.getpass("Enter a secure passphrase for AES: ").encode()).hexdigest()
  27. print()
  28. input("Press enter when both clients are ready.")
  29. ### Shit for AES padding
  30. BS = 16
  31. self.pad = lambda s: s + (BS - len(s) % BS).to_bytes(1, byteorder='big', signed=False) * (BS - len(s) % BS)
  32. self.unpad = lambda s: s[:-s[-1]]
  33. ### Start chat server and client
  34. try:
  35. Thread(target=self.RecvMSG, args=()).start()
  36. except socket.error as e:
  37. print(self.IP + " is not ready! Press enter when " + self.IP + " is ready.")
  38. input()
  39. Thread(target=self.RecvMSG, args=()).start()
  40. self.SendMSG()
  41.  
  42. # ... (rest of your methods remain the same)
  43.  
  44. def RecvMSG(self):
  45. serversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
  46. serversocket.bind(('', self.PORT))
  47. while True:
  48. data, addr = serversocket.recvfrom(1073741824) # buffer size is 1 gbit (for large files/images)
  49. data = self.DecryptMSG(data.decode())
  50. if data.startswith("\x02"):
  51. # ... (rest of the code remains the same)
  52. elif data.startswith("\x01"): # all messages start with "\x01" to prevent file spamming
  53. data = list(data)
  54. del data[0]
  55. data = ''.join(data)
  56. print("[" + addr[0] + "] > | " + data)
  57. elif data.startswith("\x03"):
  58. print("[CLIENT] " + addr[0] + " has left.")
  59. sys.exit(0)
  60.  
  61. if __name__ == "__main__":
  62. if os.name == 'nt': # Check if running on Windows
  63. print("Windows detected.")
  64. CryptoClient()
  65. elif os.name == 'posix': # Check if running on Linux
  66. print("Linux detected.")
  67. CryptoClient()
  68. else:
  69. print("Unsupported operating system.")
Add Comment
Please, Sign In to add comment