Simulation
import socket
import time
import [Link] as plt
import numpy as np
from [Link] import AES
from [Link] import get_random_bytes
# Generate AES Key
KEY_AES = get_random_bytes(32) # 256-bit key
def generate_iv():
return get_random_bytes(12) # 12-byte IV
# Encrypt telecommand using AES-GCM
def encrypt_message(message):
iv = generate_iv()
cipher = [Link](KEY_AES, AES.MODE_GCM, nonce=iv)
ciphertext, tag = cipher.encrypt_and_digest([Link]())
return iv + ciphertext + tag
# Decrypt message using AES-GCM
def decrypt_message(encrypted_message):
iv = encrypted_message[:12]
ciphertext = encrypted_message[12:-16]
tag = encrypted_message[-16:]
cipher = [Link](KEY_AES, AES.MODE_GCM, nonce=iv)
try:
decrypted_data = cipher.decrypt_and_verify(ciphertext, tag)
return decrypted_data.decode()
except ValueError:
return "ERROR: Data integrity compromised!"
# Satellite (Server)
def satellite():
try:
server = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link](('[Link]', 5555)) # Use [Link] instead of 'localhost'
[Link](1)
print("[SATELLITE] Ready to receive commands...")
except OSError as e:
print(f"[ERROR] Failed to start server: {e}")
return
conn, addr = [Link]()
print(f"[SATELLITE] Connected to ground station {addr}")
data = [Link](1024)
if data:
command = decrypt_message(data)
print(f"[SATELLITE] Received Command: {command}")
# Simulate telemetry response
battery_levels = [Link](100, 80, 10) # Simulated battery drain
temperature_levels = [Link](20, 25, 10) # Simulated
temperature rise
telemetry_data = []
for battery, temp in zip(battery_levels, temperature_levels):
telemetry = f"Telemetry: Battery {battery:.1f}%, Temp:
{temp:.1f}°C"
encrypted_telemetry = encrypt_message(telemetry)
[Link](encrypted_telemetry)
telemetry_data.append((battery, temp))
[Link](0.5) # Simulate delay
print("[SATELLITE] Sent encrypted telemetry.")
[Link]()
[Link]()
return telemetry_data
# Ground Station (Client)
def ground_station():
[Link](1) # Ensure server starts first
try:
client = [Link](socket.AF_INET, socket.SOCK_STREAM)
[Link](('[Link]', 5555)) # Use [Link] instead of
'localhost'
print("[GROUND STATION] Connected to satellite.")
except OSError as e:
print(f"[ERROR] Failed to connect to satellite: {e}")
return
# Send encrypted command
command = "Activate camera"
encrypted_command = encrypt_message(command)
[Link](encrypted_command)
print("[GROUND STATION] Sent encrypted command.")
battery_data = []
temp_data = []
# Receive encrypted telemetry
for _ in range(10):
data = [Link](1024)
telemetry = decrypt_message(data)
print(f"[GROUND STATION] Received Telemetry: {telemetry}")
# Extract telemetry values for plotting
parts = [Link](', ')
battery = float(parts[0].split(' ')[1].strip('%'))
temp = float(parts[1].split(' ')[1].strip('°C'))
battery_data.append(battery)
temp_data.append(temp)
[Link]()
return battery_data, temp_data
# Plot telemetry data
def plot_telemetry(battery_data, temp_data):
time_points = [Link](len(battery_data))
[Link](figsize=(10, 5))
[Link](1, 2, 1)
[Link](time_points, battery_data, marker='o', linestyle='-',
color='blue')
[Link]('Time Step')
[Link]('Battery Level (%)')
[Link]('Battery Drain Over Time')
[Link](1, 2, 2)
[Link](time_points, temp_data, marker='o', linestyle='-', color='red')
[Link]('Time Step')
[Link]('Temperature (°C)')
[Link]('Temperature Rise Over Time')
plt.tight_layout()
[Link]()
# Run simulation sequentially
if __name__ == "__main__":
telemetry_data = satellite()
battery_data, temp_data = ground_station()
plot_telemetry(battery_data, temp_data)