freematics-traccar-encrypted/server/test/test.py

35 lines
881 B
Python
Raw Normal View History

2024-06-27 17:16:47 -06:00
import socket
import time
from Crypto.Cipher import ChaCha20_Poly1305
# The server's address and port
2024-06-27 17:30:09 -06:00
server_address = ('localhost', 5171)
2024-06-27 17:16:47 -06:00
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# The message to be sent
message = ('Hello, Server! ' + str(time.time())).encode()
# The key and nonce
2024-06-27 22:35:28 -06:00
key = bytes.fromhex('d38a3b96a26d0b1139bd30c174884f5dbc8eaaf492493725633ecebfa4ab19e9')
2024-06-27 17:16:47 -06:00
# Encrypt the message
cipher = ChaCha20_Poly1305.new(key=key)
ciphertext, tag = cipher.encrypt_and_digest(message)
# Send the encrypted message to the server
sock.sendto(cipher.nonce + ciphertext + tag, server_address)
cipher = ChaCha20_Poly1305.new(key=key, nonce=cipher.nonce)
plaintext = cipher.decrypt_and_verify(ciphertext, tag)
print(plaintext)
2024-06-27 17:38:04 -06:00
time.sleep(1)
# Send a bad message to the server
sock.sendto(b"this should fail", server_address)
sock.close()