Skip to content

Commit

Permalink
On the fly data encryption for UDP connections
Browse files Browse the repository at this point in the history
  • Loading branch information
nsiatras committed Mar 9, 2024
1 parent 78e68fe commit 78e8542
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ of this software and associated documentation files (the "Software"), to deal
THE SOFTWARE.*/
package Extasys.Examples.UDPClient;

import Extasys.Encryption.Base64Encryptor;
import Extasys.Network.UDP.Client.Connectors.UDPConnector;
import java.net.DatagramPacket;
import java.net.InetAddress;


/**
*
* @author Nikos Siatras
Expand All @@ -39,7 +39,8 @@ public UDPClient(String name, String description, int readTimeOut, int corePoolS

// Add a UDP connector to this UDP client.
// You can add more than one connectors if you need to.
super.AddConnector("My connector", 10240, 10000, remoteHostIP, remoteHostPort);
UDPConnector conn = super.AddConnector("My connector", 10240, 10000, remoteHostIP, remoteHostPort);
//conn.setConnectionEncryptor(new Base64Encryptor());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ of this software and associated documentation files (the "Software"), to deal
THE SOFTWARE.*/
package Extasys.Examples.UDPServer;

import Extasys.Encryption.Base64Encryptor;
import Extasys.Network.UDP.Server.Listener.UDPListener;
import java.net.DatagramPacket;
import java.net.InetAddress;
Expand All @@ -33,7 +34,8 @@ public class UDPServer extends Extasys.Network.UDP.Server.ExtasysUDPServer
public UDPServer(String name, String description, InetAddress listenerIP, int port, int connectionsTimeOut, int corePoolSize, int maximumPoolSize)
{
super(name, description, corePoolSize, maximumPoolSize);
this.AddListener("My UDP Listener", listenerIP, port, 10240, connectionsTimeOut);
UDPListener listener = this.AddListener("My UDP Listener", listenerIP, port, 10240, connectionsTimeOut);
//listener.setConnectionEncryptor(new Base64Encryptor());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ of this software and associated documentation files (the "Software"), to deal
THE SOFTWARE.*/
package Extasys.Network.TCP.Client.Connectors;

import Extasys.DataFrame;
import Extasys.Encryption.ConnectionEncryptor;
import Extasys.Encryption.NullEncryptor;
import Extasys.MessageCollector.MessageETX;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public void run()
{
// Trim the incoming packet
byte[] cleanData = Arrays.copyOfRange(fDataGram.getData(), 0, fDataGram.getLength());

// Decrypt incoming data
cleanData = fConnector.getConnectionEncyptor().Decrypt(cleanData);

// Set trimmed and decrypted data to fDataGram
fDataGram.setData(cleanData, 0, cleanData.length);

fConnector.getMyExtasysUDPClient().OnDataReceive(fConnector, fDataGram);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public void run()
// Wait for previous Packet to be processed
// by the thread pool.
this.WaitForPreviousPacketToBeProcessedAndCheckIfItWasCanceled();


// Encrypt outgoing data
byte[] encryptedData = fConnector.getConnectionEncyptor().Encrypt(fDataGram.getData());
fDataGram.setData(encryptedData, 0, encryptedData.length);

if (!fCancel)
{
fConnector.fSocket.send(fDataGram);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ of this software and associated documentation files (the "Software"), to deal
THE SOFTWARE.*/
package Extasys.Network.UDP.Client.Connectors;

import Extasys.Encryption.ConnectionEncryptor;
import Extasys.Encryption.NullEncryptor;
import Extasys.Network.UDP.Client.Connectors.Packets.*;
import Extasys.Network.UDP.Client.ExtasysUDPClient;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Arrays;

/**
*
Expand All @@ -36,19 +37,22 @@ public class UDPConnector
{

private boolean fActive = false;
private ExtasysUDPClient fMyUDPClient; //Extasys UDP Client reference.
private final ExtasysUDPClient fMyUDPClient; //Extasys UDP Client reference.
public DatagramSocket fSocket;
private InetAddress fServerIP;
private int fServerPort;
private final InetAddress fServerIP;
private final int fServerPort;
private Thread fReadDataThread;
private String fName;
private int fReadBufferSize;
private int fReadTimeOut;
private final String fName;
private final int fReadBufferSize;
private final int fReadTimeOut;
public long fBytesIn = 0, fBytesOut = 0;

public IncomingUDPClientPacket fLastIncomingPacket = null;
public OutgoingUDPClientPacket fLastOutgoingPacket = null;

// Connection Encryption
private ConnectionEncryptor fConnectionEncryptor = new NullEncryptor();

/**
* Constructs a new UDP Connector.
*
Expand Down Expand Up @@ -219,11 +223,11 @@ public int getReadBufferSize()
}

/**
* Returns the maximum time in milliseconds in which a datagram packet can be
* received.
* Returns the maximum time in milliseconds in which a datagram packet can
* be received.
*
* @return the maximum time in milliseconds in which a datagram packet can be
* received.
* @return the maximum time in milliseconds in which a datagram packet can
* be received.
*/
public int getReadTimeOut()
{
Expand All @@ -250,6 +254,26 @@ public long getBytesOut()
return fBytesOut;
}

/**
* Returns the connection encryptor
*
* @return
*/
public ConnectionEncryptor getConnectionEncyptor()
{
return fConnectionEncryptor;
}

/**
* Sets the connection encryptor of this UDPConnector
*
* @param encryptor
*/
public void setConnectionEncryptor(ConnectionEncryptor encryptor)
{
fConnectionEncryptor = (encryptor == null) ? new NullEncryptor() : encryptor;
}

}

class ReadIncomingData extends Thread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public void run()
{
// Trim the incoming packet
byte[] cleanData = Arrays.copyOfRange(fDataGram.getData(), 0, fDataGram.getLength());

// Decrypt incoming data
cleanData = fMyListener.getConnectionEncyptor().Decrypt(cleanData);

fDataGram.setData(cleanData, 0, cleanData.length);

fMyListener.getMyExtasysUDPServer().OnDataReceive(fMyListener, fDataGram);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public void run()
// by the thread pool.
this.WaitForPreviousPacketToBeProcessedAndCheckIfItWasCanceled();

// Encrypt outgoing data
byte[] encryptedData = fMyListener.getConnectionEncyptor().Encrypt(fDataGram.getData());
fDataGram.setData(encryptedData, 0, encryptedData.length);

if (!fCancel)
{
fMyListener.fSocket.send(fDataGram);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ of this software and associated documentation files (the "Software"), to deal
THE SOFTWARE.*/
package Extasys.Network.UDP.Server.Listener;

import Extasys.Encryption.ConnectionEncryptor;
import Extasys.Encryption.NullEncryptor;
import Extasys.Network.UDP.Server.ExtasysUDPServer;
import Extasys.Network.UDP.Server.Listener.Packets.IncomingUDPServerPacket;
import Extasys.Network.UDP.Server.Listener.Packets.OutgoingUDPServerPacket;
Expand Down Expand Up @@ -49,6 +51,9 @@ public class UDPListener
public IncomingUDPServerPacket fLastIncomingPacket = null;
public OutgoingUDPServerPacket fLastOutgoingPacket = null;

// Connection Encryption
private ConnectionEncryptor fConnectionEncryptor = new NullEncryptor();

/**
* Constructs a new UDP Listener.
*
Expand Down Expand Up @@ -231,6 +236,26 @@ public boolean isActive()
return fActive;
}

/**
* Returns the connection encryptor
*
* @return
*/
public ConnectionEncryptor getConnectionEncyptor()
{
return fConnectionEncryptor;
}

/**
* Sets the connection encryptor of this UDPListener
*
* @param encryptor
*/
public void setConnectionEncryptor(ConnectionEncryptor encryptor)
{
fConnectionEncryptor = (encryptor == null) ? new NullEncryptor() : encryptor;
}

}

class ReadIncomingDataThread extends Thread
Expand Down

0 comments on commit 78e8542

Please sign in to comment.