Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import random
- header = 1024
- ip, port = 'localhost', 8888
- # Define a simple XOR-based encryption and decryption function
- def simple_xor_encrypt(message, key):
- encrypted_message = bytearray()
- for char in message.encode():
- encrypted_char = char ^ key
- encrypted_message.append(encrypted_char)
- return encrypted_message.decode()
- def simple_xor_decrypt(encrypted_message, key):
- decrypted_message = bytearray()
- for char in encrypted_message.encode():
- decrypted_char = char ^ key
- decrypted_message.append(decrypted_char)
- return decrypted_message.decode()
- async def send_message(writer, message, key):
- encrypted_message = simple_xor_encrypt(message, key)
- writer.write(encrypted_message.encode())
- await writer.drain()
- async def receive_message(reader, key):
- data = await reader.read(header)
- decrypted_message = simple_xor_decrypt(data.decode(), key)
- return decrypted_message.strip()
- async def user_input(writer, key):
- while True:
- message = input()
- await send_message(writer, message, key)
- async def main():
- reader, writer = await asyncio.open_connection(ip, port)
- encryption_key = 42 # Replace with your own encryption key (must match the server)
- print("Connected to the server. Start chatting:")
- try:
- asyncio.create_task(user_input(writer, encryption_key))
- while True:
- message = await receive_message(reader, encryption_key)
- print(message)
- except KeyboardInterrupt:
- print("Disconnected from the server.")
- writer.close()
- await writer.wait_closed()
- if __name__ == "__main__":
- asyncio.run(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement