This repository has been archived on 2025-12-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
SamaPager/SamaPager_Client/Client.cs
Andrea 7994f3d066 Fix revert errato
revert Revert to 19a5a08f04

revert First experimental encryption implementation (not working)
2025-12-03 09:20:35 +00:00

81 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SamaPager_Client
{
internal class Client
{
internal int Port { get; set; }
internal IPAddress DestinationIP { get; set; }
internal UdpClient UdpStack { get; set; }
private IPEndPoint serverEndPoint { get; set; }
internal Client(IPAddress destinationIP, int destinationPort)
{
this.DestinationIP = destinationIP;
this.Port = destinationPort;
}
internal string StartClient()
{
serverEndPoint = new IPEndPoint(this.DestinationIP, this.Port);
try
{
UdpStack = new UdpClient();
return "Serving on " + serverEndPoint;
}
catch (Exception ex)
{
return $"Error ({ex.Message})";
}
}
internal void SendMessage(string message)
{
byte[] msgData = Encoding.UTF8.GetBytes(message);
UdpStack.Send(msgData, msgData.Length, serverEndPoint);
}
internal void SendEncryptedMessage(string message, string key)
{
char[] encryptedMessage = new char[0];
for (int i = 0; i < message.Length; i++)
{
char cock = (char)(message[i] ^ key[i % key.Length]);
encryptedMessage.Append(cock);
}
MessageBox.Show(Convert.ToBase64String(Encoding.UTF8.GetBytes(encryptedMessage)));
}
internal string SendCommand(string command)
{
byte[] msgData = Encoding.UTF8.GetBytes(command);
UdpStack.Send(msgData, msgData.Length, serverEndPoint);
try
{
UdpStack.Client.ReceiveTimeout = 5000;
IPEndPoint receiveEndPoint = serverEndPoint;
byte[] receivedBytes = UdpStack.Receive(ref receiveEndPoint);
return $"Server replied: {Encoding.UTF8.GetString(receivedBytes)}";
}
catch(Exception ex)
{
return $"ERROR: {ex.Message}";
}
}
internal void KillClient()
{
this.UdpStack.Close();
}
}
}