To Implement network encryption in ns-3, which means the data should be encrypted in an application before sending it to the network and decrypt it upon reception. Here’s steps will guide to implement Network Encryption using ns-3.
Step-by-Step Implementation for Network Encryption
- Install ns-3
Ensure ns-3 is installed on your system. we can download and install it from the official ns-3 website.
- Define the Network Topology
Define the network topology including:
- Sender node
- Receiver node
- Create Network Nodes
Create network nodes using NodeContainer.
NodeContainer senderNode, receiverNode;
senderNode.Create(1);
receiverNode.Create(1);
4. Set Up Network Devices
Install network devices on the nodes using appropriate network interfaces, such as WiFi for wireless communication.
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);
WifiMacHelper mac;
mac.SetType(“ns3::AdhocWifiMac”);
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
phy.SetChannel(channel.Create());
NetDeviceContainer senderDevice = wifi.Install(phy, mac, senderNode);
NetDeviceContainer receiverDevice = wifi.Install(phy, mac, receiverNode);
5. Configure Mobility Model
Set up the mobility model for the nodes.
MobilityHelper mobility;
mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);
mobility.Install(senderNode);
mobility.Install(receiverNode);
6. Implement Encryption Application
Create an application that performs encryption and decryption. Below is an example of a simple cryptographic application.
Cryptographic Application (Example)
class CryptoApplication : public Application {
public:
void StartApplication() override {
sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
sendSocket->Connect(InetSocketAddress(destAddress, destPort));
recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), localPort));
recvSocket->SetRecvCallback(MakeCallback(&CryptoApplication::ReceivePacket, this));
// Schedule the first packet send
SendPacket();
}
void SetRemote(Address address, uint16_t port) {
destAddress = address;
destPort = port;
}
void SetLocalPort(uint16_t port) {
localPort = port;
}
void SendPacket() {
std::string message = “Hello, this is an encrypted message”;
std::string encryptedMessage = EncryptMessage(message);
Ptr<Packet> packet = Create<Packet>((uint8_t*) encryptedMessage.c_str(), encryptedMessage.size());
sendSocket->Send(packet);
// Schedule the next packet send
Simulator::Schedule(Seconds(1.0), &CryptoApplication::SendPacket, this);
}
void ReceivePacket(Ptr<Socket> socket) {
Ptr<Packet> packet;
Address from;
while ((packet = socket->RecvFrom(from))) {
std::string encryptedMessage = std::string((char*) packet->PeekData(), packet->GetSize());
std::string message = DecryptMessage(encryptedMessage);
std::cout << “Received message: ” << message << std::endl;
}
}
std::string EncryptMessage(const std::string& message) {
// Simple XOR encryption for demonstration
std::string encrypted = message;
char key = ‘K’; // Encryption key
for (size_t i = 0; i < message.size(); ++i) {
encrypted[i] ^= key;
}
return encrypted;
}
std::string DecryptMessage(const std::string& encryptedMessage) {
// Simple XOR decryption for demonstration
return EncryptMessage(encryptedMessage); // XOR with the same key
}
private:
Ptr<Socket> sendSocket;
Ptr<Socket> recvSocket;
Address destAddress;
uint16_t destPort;
uint16_t localPort;
};
7. Set Up Applications
Install the applications on the nodes.
ApplicationContainer senderApp, receiverApp;
// Sender application
Ptr<CryptoApplication> senderCryptoApp = CreateObject<CryptoApplication>();
senderCryptoApp->SetRemote(receiverNode.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9);
senderCryptoApp->SetLocalPort(10);
senderNode.Get(0)->AddApplication(senderCryptoApp);
senderCryptoApp->SetStartTime(Seconds(1.0));
senderCryptoApp->SetStopTime(Seconds(20.0));
senderApp.Add(senderCryptoApp);
// Receiver application
Ptr<CryptoApplication> receiverCryptoApp = CreateObject<CryptoApplication>();
receiverCryptoApp->SetLocalPort(9);
receiverNode.Get(0)->AddApplication(receiverCryptoApp);
receiverCryptoApp->SetStartTime(Seconds(1.0));
receiverCryptoApp->SetStopTime(Seconds(20.0));
receiverApp.Add(receiverCryptoApp);
8. Set Up Routing Protocols
Configure routing protocols for the network.
AodvHelper aodv;
InternetStackHelper internet;
internet.SetRoutingHelper(aodv);
internet.Install(senderNode);
internet.Install(receiverNode);
9. Assign IP Addresses
Assign IP addresses to the network devices.
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer senderInterfaces = address.Assign(senderDevice);
Ipv4InterfaceContainer receiverInterfaces = address.Assign(receiverDevice);
10. Run the Simulation
Configure the simulation runtime and execute it.
Simulator::Stop(Seconds(20.0));
Simulator::Run();
Simulator::Destroy();
Example of a Simple Cryptographic Network Script
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/wifi-module.h”
#include “ns3/mobility-module.h”
#include “ns3/applications-module.h”
#include “ns3/aodv-module.h”
using namespace ns3;
class CryptoApplication : public Application {
public:
void StartApplication() override {
sendSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
sendSocket->Connect(InetSocketAddress(destAddress, destPort));
recvSocket = Socket::CreateSocket(GetNode(), UdpSocketFactory::GetTypeId());
recvSocket->Bind(InetSocketAddress(Ipv4Address::GetAny(), localPort));
recvSocket->SetRecvCallback(MakeCallback(&CryptoApplication::ReceivePacket, this));
// Schedule the first packet send
SendPacket();
}
void SetRemote(Address address, uint16_t port) {
destAddress = address;
destPort = port;
}
void SetLocalPort(uint16_t port) {
localPort = port;
}
void SendPacket() {
std::string message = “Hello, this is an encrypted message”;
std::string encryptedMessage = EncryptMessage(message);
Ptr<Packet> packet = Create<Packet>((uint8_t*) encryptedMessage.c_str(), encryptedMessage.size());
sendSocket->Send(packet);
// Schedule the next packet send
Simulator::Schedule(Seconds(1.0), &CryptoApplication::SendPacket, this);
}
void ReceivePacket(Ptr<Socket> socket) {
Ptr<Packet> packet;
Address from;
while ((packet = socket->RecvFrom(from))) {
std::string encryptedMessage = std::string((char*) packet->PeekData(), packet->GetSize());
std::string message = DecryptMessage(encryptedMessage);
std::cout << “Received message: ” << message << std::endl;
}
}
std::string EncryptMessage(const std::string& message) {
// Simple XOR encryption for demonstration
std::string encrypted = message;
char key = ‘K’; // Encryption key
for (size_t i = 0; i < message.size(); ++i) {
encrypted[i] ^= key;
}
return encrypted;
}
std::string DecryptMessage(const std::string& encryptedMessage) {
// Simple XOR decryption for demonstration
return EncryptMessage(encryptedMessage); // XOR with the same key
}
private:
Ptr<Socket> sendSocket;
Ptr<Socket> recvSocket;
Address destAddress;
uint16_t destPort;
uint16_t localPort;
};
int main(int argc, char *argv[]) {
NodeContainer senderNode, receiverNode;
senderNode.Create(1);
receiverNode.Create(1);
// WiFi setup
WifiHelper wifi;
wifi.SetStandard(WIFI_PHY_STANDARD_80211n_5GHZ);
WifiMacHelper mac;
mac.SetType(“ns3::AdhocWifiMac”);
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
phy.SetChannel(channel.Create());
NetDeviceContainer senderDevice = wifi.Install(phy, mac, senderNode);
NetDeviceContainer receiverDevice = wifi.Install(phy, mac, receiverNode);
// Mobility setup
MobilityHelper mobility;
mobility.SetMobilityModel(“ns3::ConstantPositionMobilityModel”);
mobility.Install(senderNode);
mobility.Install(receiverNode);
// Internet stack and routing
AodvHelper aodv;
InternetStackHelper internet;
internet.SetRoutingHelper(aodv);
internet.Install(senderNode);
internet.Install(receiverNode);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer senderInterfaces = address.Assign(senderDevice);
Ipv4InterfaceContainer receiverInterfaces = address.Assign(receiverDevice);
// Install applications
ApplicationContainer senderApp, receiverApp;
// Sender application
Ptr<CryptoApplication> senderCryptoApp = CreateObject<CryptoApplication>();
senderCryptoApp->SetRemote(receiverNode.Get(0)->GetObject<Ipv4>()->GetAddress(1, 0).GetLocal(), 9);
senderCryptoApp->SetLocalPort(10);
senderNode.Get(0)->AddApplication(senderCryptoApp);
senderCryptoApp->SetStartTime(Seconds(1.0));
senderCryptoApp->SetStopTime(Seconds(20.0));
senderApp.Add(senderCryptoApp);
// Receiver application
Ptr<CryptoApplication> receiverCryptoApp = CreateObject<CryptoApplication>();
receiverCryptoApp->SetLocalPort(9);
receiverNode.Get(0)->AddApplication(receiverCryptoApp);
receiverCryptoApp->SetStartTime(Seconds(1.0));
receiverCryptoApp->SetStopTime(Seconds(20.0));
receiverApp.Add(receiverCryptoApp);
Simulator::Stop(Seconds(20.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Finally, the implementation of Network Encryption in ns-3 environment is explained clearly and we support all kind of advancement in network encryption function.