How to Begin Implement encryption decryption Time in ns3

To implement and study the Encryption and Decryption Time in NS3, we require replicating the network surroundings in which data packets are encode for previously communication and decode upon the response. The NS3 don’t natively offers a cryptographic operation, so we will want to incorporate the cryptography collection such as OpenSSL, Crypto++, or any lightweight custom encode the logic.

Steps to Implement Encryption and Decryption Time in NS3

  1. Understand Encryption and Decryption Time
  • Definition:
    • Encryption time: The time taken for encodes the data packet before transmission.
    • Decryption time: The time taken for decodes the received data packet.
  • Measurement:
    • Use NS3’s logging or tracing devices for calculate the time in every operation.
    • Execute for encode and decode as part of application layer processing.
  1. Set up NS3 Simulation Environment
  1. Install NS3:
    • Assure the tool NS3 is installed. Download it from the NS3 website.
  2. Choose a Network Type:
    • Uses a basic wired or wireless network for encode/decode the testing.
  3. Integrate Cryptography Logic:
    • Use an external collection such as OpenSSL or execute the lightweight encode for procedure directly in NS3.
  1. Implement Encryption and Decryption Logic
  1. Encrypt Before Transmission:
    • Change or encompass the application logic for encode data before forwarding packets.
  2. Decrypt After Reception:
    • Decode the data for packet reception.
  1. Measure Time for Encryption and Decryption
  1. Capture Timestamps:
    • Use Simulator::Now () to log timestamps before and after encode and decode.
  2. Log Processing Time:
    • Measure the various among timestamps and log the results.

Example Script: ncryption and Decryption Time in NS3

#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 for encryption/decryption

using namespace ns3;

// Encryption and Decryption Logic

class CryptoHandler {

public:

CryptoHandler() {

// Initialize the AES key and IV

std::fill(std::begin(m_key), std::end(m_key), 0);

std::fill(std::begin(m_iv), std::end(m_iv), 0);

AES_set_encrypt_key(m_key, 128, &m_encryptKey);

AES_set_decrypt_key(m_key, 128, &m_decryptKey);

}

std::string Encrypt(const std::string &data) {

unsigned char encrypted[128];

Simulator::Now(); // Get encryption start time

AES_cfb128_encrypt(reinterpret_cast<const unsigned char *>(data.c_str()), encrypted, data.length(), &m_encryptKey, m_iv, 1);

Simulator::Now(); // Timestamps for encryption time logic

return std::string(reinterpret_cast<char *>(encrypted), data.length());

}

std::string Decrypt(const std::string &data) {

unsigned char decrypted[128];

Simulator::Now();

Here’s the continued and corrected clarification:

unsigned char decrypted[128];

AES_cfb128_encrypt(reinterpret_cast<const unsigned char *>(data.c_str()), decrypted, data.length(), &m_decryptKey, m_iv, 0);

return std::string(reinterpret_cast<char *>(decrypted), data.length());

}

private:

unsigned char m_key[16]; // AES 128-bit key

unsigned char m_iv[16];  // Initialization vector

AES_KEY m_encryptKey;

AES_KEY m_decryptKey;

};

void SimulateCryptoProcess(Ptr<Socket> socket, Ptr<Packet> packet) {

CryptoHandler cryptoHandler;

// Extract data from the packet

uint8_t buffer[1024];

packet->CopyData(buffer, packet->GetSize());

std::string originalData(reinterpret_cast<char *>(buffer), packet->GetSize());

// Measure encryption time

Time startEncryption = Simulator::Now();

std::string encryptedData = cryptoHandler.Encrypt(originalData);

Time endEncryption = Simulator::Now();

NS_LOG_UNCOND(“Encryption Time: ” << (endEncryption – startEncryption).GetMicroSeconds() << ” us”);

// Measure decryption time

Time startDecryption = Simulator::Now();

std::string decryptedData = cryptoHandler.Decrypt(encryptedData);

Time endDecryption = Simulator::Now();

NS_LOG_UNCOND(“Decryption Time: ” << (endDecryption – startDecryption).GetMicroSeconds() << ” us”);

// Log if decryption matches the original data

if (decryptedData == originalData) {

NS_LOG_UNCOND(“Decryption successful: Data integrity verified.”);

} else {

NS_LOG_UNCOND(“Decryption failed: Data mismatch.”);

}

}

int main(int argc, char *argv[]) {

CommandLine cmd;

cmd.Parse(argc, argv);

// Create nodes

NodeContainer nodes;

nodes.Create(2);

// Set up Point-to-Point connection

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute(“DataRate”, StringValue(“5Mbps”));

pointToPoint.SetChannelAttribute(“Delay”, StringValue(“2ms”));

NetDeviceContainer devices = pointToPoint.Install(nodes);

// Install Internet stack

InternetStackHelper internet;

internet.Install(nodes);

// Assign IP addresses

Ipv4AddressHelper address;

address.SetBase(“10.1.1.0”, “255.255.255.0”);

Ipv4InterfaceContainer interfaces = address.Assign(devices);

// Configure application

uint16_t port = 9;

Address sinkAddress(InetSocketAddress(interfaces.GetAddress(1), port));

PacketSinkHelper packetSinkHelper(“ns3::TcpSocketFactory”, sinkAddress);

ApplicationContainer sinkApp = packetSinkHelper.Install(nodes.Get(1));

sinkApp.Start(Seconds(1.0));

sinkApp.Stop(Seconds(10.0));

Ptr<Socket> sourceSocket = Socket::CreateSocket(nodes.Get(0), TcpSocketFactory::GetTypeId());

sourceSocket->Bind();

sourceSocket->Connect(sinkAddress);

Simulator::Schedule(Seconds(2.0), &SimulateCryptoProcess, sourceSocket, Create<Packet>(1024));

Simulator::Run();

Simulator::Destroy();

return 0;

}

Explanation of the Script

  1. Encryption/Decryption Logic:
    • Uses OpenSSL to implement the AES encode and decode.
    • Seizures the timestamps using Simulator::Now () for calculate the time taken for encode and decode.
  2. Simulate Data Flow:
    • It replicates the data communication using a point-to-point connection.
    • Encapsulates for encode and decode method around data transmission.
  3. Logging:
    • Record for encode and decode duration of validates the data integrity after decode.

Steps to Run the Simulation

  1. Install OpenSSL:
    • Assure the OpenSSL is installed on the system.
  2. Build the Script:

./waf –run “encryption-decryption-example”

  1. Observe Logs:
    • Test for encode and decode duration for the outcomes.

We had explored the basic concepts on how to calculate and how to setup the network using the ns3 tool and also we offer the more details regarding the time for encryption and decryption.