Advertisement
LEGEND2004

Client

Oct 7th, 2023
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. import asyncio
  2. import random
  3.  
  4. header = 1024
  5. ip, port = 'localhost', 8888
  6.  
  7. # Define a simple XOR-based encryption and decryption function
  8. def simple_xor_encrypt(message, key):
  9.     encrypted_message = bytearray()
  10.     for char in message.encode():
  11.         encrypted_char = char ^ key
  12.         encrypted_message.append(encrypted_char)
  13.     return encrypted_message.decode()
  14.  
  15. def simple_xor_decrypt(encrypted_message, key):
  16.     decrypted_message = bytearray()
  17.     for char in encrypted_message.encode():
  18.         decrypted_char = char ^ key
  19.         decrypted_message.append(decrypted_char)
  20.     return decrypted_message.decode()
  21.  
  22. async def send_message(writer, message, key):
  23.     encrypted_message = simple_xor_encrypt(message, key)
  24.     writer.write(encrypted_message.encode())
  25.     await writer.drain()
  26.  
  27. async def receive_message(reader, key):
  28.     data = await reader.read(header)
  29.     decrypted_message = simple_xor_decrypt(data.decode(), key)
  30.     return decrypted_message.strip()
  31.  
  32. async def user_input(writer, key):
  33.     while True:
  34.         message = input()
  35.         await send_message(writer, message, key)
  36.  
  37. async def main():
  38.     reader, writer = await asyncio.open_connection(ip, port)
  39.     encryption_key = 42  # Replace with your own encryption key (must match the server)
  40.     print("Connected to the server. Start chatting:")
  41.    
  42.     try:
  43.         asyncio.create_task(user_input(writer, encryption_key))
  44.        
  45.         while True:
  46.             message = await receive_message(reader, encryption_key)
  47.             print(message)
  48.            
  49.     except KeyboardInterrupt:
  50.         print("Disconnected from the server.")
  51.         writer.close()
  52.         await writer.wait_closed()
  53.  
  54. if __name__ == "__main__":
  55.     asyncio.run(main())
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement