made encryption optional, put server params into config, default is server = off
This commit is contained in:
1 parent
50046901fd
commit
74af5ae691
5 files changed
+118
-33
No files matched your search
@@ -13,6 +13,7 @@ namespace ClientServerLib
|
||||
internal class CConnection
|
||||
{
|
||||
private bool keySet;
|
||||
private bool encryption;
|
||||
private CDiffieHellman dh;
|
||||
private UTF8Encoding encoder;
|
||||
private int connectionID;
|
||||
@@ -29,17 +30,23 @@ namespace ClientServerLib
|
||||
get { return connectionID; }
|
||||
}
|
||||
|
||||
public CConnection(TcpClient client, int ConnectionID)
|
||||
public CConnection(TcpClient client, int ConnectionID, bool Encryption = false)
|
||||
{
|
||||
TcpClient = client;
|
||||
client.NoDelay = true;
|
||||
keySet = false;
|
||||
encryption = Encryption;
|
||||
connectionID = ConnectionID;
|
||||
encoder = new UTF8Encoding();
|
||||
}
|
||||
|
||||
public byte[] GetKeyParams()
|
||||
{
|
||||
if (!encryption)
|
||||
{
|
||||
return encoder.GetBytes("NO ENCRYPTION");
|
||||
}
|
||||
|
||||
if (dh == null)
|
||||
dh = new CDiffieHellman(256).GenerateRequest();
|
||||
|
||||
@@ -51,34 +58,65 @@ namespace ClientServerLib
|
||||
if (serverParam == null)
|
||||
return null;
|
||||
|
||||
dh = new CDiffieHellman(256).GenerateResponse(
|
||||
encoder.GetString(serverParam, 0, serverParam.Length));
|
||||
string response = encoder.GetString(serverParam, 0, serverParam.Length);
|
||||
if (response == "NO ENCRYPTION")
|
||||
{
|
||||
encryption = false;
|
||||
keySet = true;
|
||||
return encoder.GetBytes(response);
|
||||
}
|
||||
|
||||
dh = new CDiffieHellman(256).GenerateResponse(response);
|
||||
|
||||
keySet = true;
|
||||
encryption = true;
|
||||
return encoder.GetBytes(dh.ToString());
|
||||
}
|
||||
|
||||
public void CreateServerKey(byte[] clientResponse)
|
||||
{
|
||||
string response = encoder.GetString(clientResponse, 0, clientResponse.Length);
|
||||
|
||||
if (response == "NO ENCRYPTION")
|
||||
{
|
||||
keySet = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (dh == null)
|
||||
return;
|
||||
|
||||
dh.HandleResponse(encoder.GetString(clientResponse, 0, clientResponse.Length));
|
||||
dh.HandleResponse(response);
|
||||
keySet = true;
|
||||
}
|
||||
|
||||
public byte[] Encrypt(byte[] Data)
|
||||
{
|
||||
if (Data == null || !keySet)
|
||||
if (Data == null || (!keySet && encryption))
|
||||
return Data;
|
||||
|
||||
byte[] dataLength = BitConverter.GetBytes(Data.Length);
|
||||
|
||||
if (!encryption)
|
||||
{
|
||||
int len = 4 + dataLength.Length + Data.Length;
|
||||
byte[] messageLength = BitConverter.GetBytes(len);
|
||||
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
stream.Write(messageLength, 0, messageLength.Length);
|
||||
stream.Write(dataLength, 0, dataLength.Length);
|
||||
stream.Write(Data, 0, Data.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
using (Aes aes = new AesManaged())
|
||||
{
|
||||
aes.Key = dh.Key;
|
||||
aes.GenerateIV();
|
||||
|
||||
byte[] dataLength = BitConverter.GetBytes(Data.Length);
|
||||
|
||||
using (var encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
|
||||
using (var memoryStream = new MemoryStream())
|
||||
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
|
||||
@@ -91,32 +129,47 @@ namespace ClientServerLib
|
||||
int len = 4 + aes.IV.Length + dataLength.Length + encrypted.Length;
|
||||
byte[] messageLength = BitConverter.GetBytes(len);
|
||||
|
||||
MemoryStream stream = new MemoryStream();
|
||||
stream.Write(messageLength, 0, messageLength.Length);
|
||||
stream.Write(aes.IV, 0, aes.IV.Length);
|
||||
stream.Write(dataLength, 0, dataLength.Length);
|
||||
stream.Write(encrypted, 0, encrypted.Length);
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
stream.Write(messageLength, 0, messageLength.Length);
|
||||
stream.Write(aes.IV, 0, aes.IV.Length);
|
||||
stream.Write(dataLength, 0, dataLength.Length);
|
||||
stream.Write(encrypted, 0, encrypted.Length);
|
||||
|
||||
return stream.ToArray();
|
||||
return stream.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] Decrypt(byte[] Data)
|
||||
{
|
||||
if (Data == null || !keySet)
|
||||
if (Data == null || (!keySet && encryption))
|
||||
return Data;
|
||||
|
||||
if (Data.Length < 25)
|
||||
return null;
|
||||
|
||||
|
||||
int messageLength = BitConverter.ToInt32(Data, 0);
|
||||
if (messageLength > Data.Length)
|
||||
return null;
|
||||
|
||||
int dataLength;
|
||||
|
||||
if (!encryption)
|
||||
{
|
||||
dataLength = BitConverter.ToInt32(Data, 4);
|
||||
|
||||
byte[] message = new byte[dataLength];
|
||||
Array.Copy(Data, 8, message, 0, dataLength);
|
||||
return message;
|
||||
}
|
||||
|
||||
if (Data.Length < 25)
|
||||
return null;
|
||||
|
||||
byte[] IV = new byte[16];
|
||||
Array.Copy(Data, 4, IV, 0, 16);
|
||||
int dataLength = BitConverter.ToInt32(Data, 20);
|
||||
|
||||
dataLength = BitConverter.ToInt32(Data, 20);
|
||||
|
||||
using (Aes aes = new AesManaged())
|
||||
{
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace ClientServerLib
|
||||
private TcpListener tcpListener;
|
||||
private Thread listenThread;
|
||||
private int port;
|
||||
private bool encryption;
|
||||
private int bufferLength = 8192;
|
||||
private bool running;
|
||||
|
||||
@@ -21,11 +22,12 @@ namespace ClientServerLib
|
||||
private Dictionary<int, CConnection> clients;
|
||||
private Queue<int> ids;
|
||||
|
||||
public CServer(HandleRequest RequestCallback, int Port = 3000)
|
||||
public CServer(HandleRequest RequestCallback, int Port = 3000, bool Encryption = false)
|
||||
{
|
||||
port = Port;
|
||||
running = false;
|
||||
requestCallback = RequestCallback;
|
||||
encryption = Encryption;
|
||||
|
||||
clients = new Dictionary<int, CConnection>();
|
||||
ids = new Queue<int>(1000);
|
||||
@@ -90,7 +92,7 @@ namespace ClientServerLib
|
||||
if (client != null)
|
||||
{
|
||||
int id = ids.Dequeue();
|
||||
CConnection connection = new CConnection(client, id);
|
||||
CConnection connection = new CConnection(client, id, encryption);
|
||||
|
||||
lock (clients)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user