To implement the fiber optical security in the ns3, we have to interact through fiber optics by simulating a network and protect the transmitted data that are passed through the fiber optic links by integrating the security mechanisms. We have to simulate the fiber optic communication that contains high speed, low-latency properties and then execute the security mechanisms such as encryption.
Here, we provide the step-by-step implementation process of fiber optical security in the ns3 with an example:
Step-by-Step Implementation:
Step 1: Set Up ns3 Environment
- Install ns3: Make certain, ns3 is installed. Follow the installation guide suitable for your operating system.
- Familiarize Yourself with ns3: Use the ns3 tutorial to know the basic concepts and its simulation structure.
Step 2: Simulate Fiber Optic Communication
- Create a High-Speed, Low-Latency Network: For Fiber optic communication, we have to define a network topology that simulates its properties. We’ll use a point-to-point topology with high data rate and low delay.
#include “ns3/core-module.h”
#include “ns3/network-module.h”
#include “ns3/internet-module.h”
#include “ns3/point-to-point-module.h”
#include “ns3/applications-module.h”
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace ns3;
// Helper function to convert a string to a hexadecimal representation
std::string StringToHex(const std::string &input) {
std::ostringstream output;
for (unsigned char c : input) {
output << std::hex << std::setw(2) << std::setfill(‘0’) << (int)c;
}
return output.str();
}
// Helper function to convert a hexadecimal string to bytes
std::string HexToString(const std::string &input) {
std::string output;
output.reserve(input.size() / 2);
for (size_t i = 0; i < input.size(); i += 2) {
unsigned int byte;
std::istringstream(input.substr(i, 2)) >> std::hex >> byte;
output.push_back(static_cast<unsigned char>(byte));
}
return output;
}
// Lightweight encryption using AES
std::string Encrypt(const std::string &plaintext, const std::string &key) {
AES_KEY encryptKey;
unsigned char iv[AES_BLOCK_SIZE];
unsigned char ciphertext[plaintext.size() + AES_BLOCK_SIZE];
if (!RAND_bytes(iv, AES_BLOCK_SIZE)) {
std::cerr << “Error generating random bytes for IV” << std::endl;
exit(1);
}
AES_set_encrypt_key((const unsigned char *)key.c_str(), 128, &encryptKey);
int len = plaintext.size();
AES_cfb128_encrypt((const unsigned char *)plaintext.c_str(), ciphertext, len, &encryptKey, iv, &len, AES_ENCRYPT);
std::string ivStr((char *)iv, AES_BLOCK_SIZE);
std::string ciphertextStr((char *)ciphertext, len);
return ivStr + ciphertextStr;
}
// Lightweight decryption using AES
std::string Decrypt(const std::string &ciphertext, const std::string &key) {
AES_KEY decryptKey;
unsigned char iv[AES_BLOCK_SIZE];
unsigned char plaintext[ciphertext.size() – AES_BLOCK_SIZE];
memcpy(iv, ciphertext.c_str(), AES_BLOCK_SIZE);
AES_set_decrypt_key((const unsigned char *)key.c_str(), 128, &decryptKey);
int len = ciphertext.size() – AES_BLOCK_SIZE;
AES_cfb128_encrypt((const unsigned char *)ciphertext.c_str() + AES_BLOCK_SIZE, plaintext, len, &decryptKey, iv, &len, AES_DECRYPT);
return std::string((char *)plaintext, len);
}
class FiberOpticSecurityApp : public Application {
public:
FiberOpticSecurityApp() {}
virtual ~FiberOpticSecurityApp() {}
void Setup(Address address, uint16_t port, const std::string &key) {
m_peerAddress = address;
m_peerPort = port;
m_key = key;
}
private:
virtual void StartApplication() {
m_socket = Socket::CreateSocket(GetNode(), TypeId::LookupByName(“ns3::UdpSocketFactory”));
m_socket->Bind();
m_socket->Connect(InetSocketAddress(m_peerAddress, m_peerPort));
// Schedule the first packet transmission
Simulator::Schedule(Seconds(1.0), &FiberOpticSecurityApp::SendPacket, this);
// Set up the receive callback
m_socket->SetRecvCallback(MakeCallback(&FiberOpticSecurityApp::ReceivePacket, this));
}
virtual void StopApplication() {
if (m_socket) {
m_socket->Close();
m_socket = 0;
}
}
void SendPacket() {
std::string message = “Hello, this is a secure message!”;
std::string encryptedMessage = Encrypt(message, m_key);
Ptr<Packet> packet = Create<Packet>((uint8_t *)encryptedMessage.c_str(), encryptedMessage.size());
m_socket->Send(packet);
// Schedule the next packet transmission
Simulator::Schedule(Seconds(1.0), &FiberOpticSecurityApp::SendPacket, this);
}
void ReceivePacket(Ptr<Socket> socket) {
Ptr<Packet> packet = socket->Recv();
uint8_t buffer[1024];
packet->CopyData(buffer, packet->GetSize());
std::string encryptedMessage((char *)buffer, packet->GetSize());
std::string decryptedMessage = Decrypt(encryptedMessage, m_key);
std::cout << “Received message: ” << decryptedMessage << std::endl;
}
Ptr<Socket> m_socket;
Address m_peerAddress;
uint16_t m_peerPort;
std::string m_key;
};
int main(int argc, char *argv[]) {
NodeContainer nodes;
nodes.Create(2); // Example: 2 nodes (1 client, 1 server)
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“10Gbps”)); // Simulate fiber optic speeds
pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
InternetStackHelper stack;
stack.Install(nodes);
Ipv4AddressHelper address;
address.SetBase(“10.1.1.0”, “255.255.255.0”);
Ipv4InterfaceContainer interfaces = address.Assign(devices);
uint16_t port = 9;
// Generate a symmetric key for AES encryption (128 bits for AES)
std::string key = “1234567890123456”; // Example key, should be securely generated in practice
Ptr<FiberOpticSecurityApp> clientApp = CreateObject<FiberOpticSecurityApp>();
clientApp->Setup(InetSocketAddress(interfaces.GetAddress(1), port), port, key);
nodes.Get(0)->AddApplication(clientApp);
clientApp->SetStartTime(Seconds(2.0));
clientApp->SetStopTime(Seconds(10.0));
Ptr<FiberOpticSecurityApp> serverApp = CreateObject<FiberOpticSecurityApp>();
serverApp->Setup(InetSocketAddress(Ipv4Address::GetAny(), port), port, key);
nodes.Get(1)->AddApplication(serverApp);
serverApp->SetStartTime(Seconds(1.0));
serverApp->SetStopTime(Seconds(10.0));
Simulator::Run();
Simulator::Destroy();
return 0;
}
Explanation:
- Simulate Fiber Optic Communication:
- We use PointToPointHelper with a high data rate (10 Gbps) and low delay (2 ms) to simulate the features of fiber optic communication.
- Lightweight Encryption and Decryption:
- We use OpenSSL’s AES for encryption and decryption. Make sure OpenSSL is installed and connected properly.
- FiberOpticSecurityApp Class:
- Handles the application logic, including sending and receiving encrypted messages.
- We have to initialize an application that has peer address, port, and AES key using the Setup method.
- StartApplication method sets up the socket connection, schedules the first packet transmission, and sets up the receive callback.
- Message is encrypted by the SendPacket method and sends it to the peer node.
- Received message is decrypted by the ReceivePacket method and prints it.
- Main Function:
- Creates a simple point-to-point network with 2 nodes (client and server).
- Initializes the FiberOpticSecurityApp applications on both client and server nodes with the generated AES key.
- The client sends encrypted messages to the server, and the server decrypts and prints the messages.
Compile and Run:
- Compile the Code: Make certain that OpenSSL is installed on your computer. Then, compile the code using the following command:
g++ -std=c++11 -o ns3-fiber-optic-security main.cc -lssl -lcrypto `pkg-config –cflags –libs ns3-dev`
- Run the Simulation: Execute the compiled program:
./ns3-fiber-optic-security
This setup demonstrates a simple implementation of fiber optic security in ns. You can expand it further to include more nodes, complex topologies, and additional cryptographic functionalities as needed.
In conclusion, we were comprehensively provided the essential information on how to implement the Fiber Optical Security in the ns3 tool through this script. You can also get the relevant details of this topic from us we provide you with best topic ideas along with execution support.