How to Implement Symmetric Key Cryptography in NS2

To implement Symmetric Key Cryptography in ns2, it is approach in which the same key is used for both encryption and decryption. In NS2, while there are no built-in cryptographic functions, we can mimic symmetric key cryptography by encoding and decoding messages among the network nodes using a distributed secret key. The sender will encode the message using the shared key, and the receiver will decode the message with the same key. For customised services for your research work contact us we will help you with novel research ideas.

Here’s a step-by-step guide to simulating Symmetric Key Cryptography in NS2:

Step-by-Step Implementation:

  1. Set up NS2

Make sure that NS2 is installed on the system. If not, we can install it using:

sudo apt-get install ns2

  1. Define the Network Topology

Initially generate a basic network topology with sender and receiver nodes to mimic secure communication using symmetric key cryptography.

set ns [new Simulator]

set tracefile [open symmetric_key_crypto.tr w]

$ns trace-all $tracefile

# Create sender and receiver nodes

set sender [$ns node]

set receiver [$ns node]

# Create a link between the sender and receiver

$ns duplex-link $sender $receiver 1Mb 10ms DropTail

  1. Define the Shared Symmetric Key

In symmetric key cryptography, the sender and receiver share a secret key. This key is used for both encryption and decryption.

# Simulate a shared secret key for encryption and decryption

set shared_secret_key “secret_key_123”

  1. Encrypt Messages Using the Shared Key

In symmetric encryption, the sender will encode the message using the shared key. We can mimic this by implementing a basic encryption function to the message.

# Simulate message encryption using the shared symmetric key

proc encrypt_message {message shared_key} {

puts “Encrypting message: ‘$message’ using shared key: $shared_key”

return “encrypted_$message_with_$shared_key”  ;# Simulate the encrypted message

}

# Encrypt a message at 1 second

$ns at 1.0 “set encrypted_message [encrypt_message ‘Hello Receiver!’ $shared_secret_key]”

  1. Decrypt Messages Using the Shared Key

The receiver will decode the message using the same shared key. This step replicates the decryption process by retreating the encryption.

# Simulate message decryption using the shared symmetric key

proc decrypt_message {encrypted_message shared_key} {

puts “Decrypting message: ‘$encrypted_message’ using shared key: $shared_key”

return “decrypted_message”  ;# Simulate the decrypted message

}

# Decrypt the message at the receiver node

$ns at 1.5 “set decrypted_message [decrypt_message $encrypted_message $shared_secret_key]”

  1. Set up Secure Data Transmission Between Sender and Receiver

Now, configure the actual data transmission. The sender will transfer an encrypted message to the receiver, who will then decrypt it using the shared key.

(A) Set up UDP Data Transmission

Replicate the transmission of an encrypted message using the CBR (Constant Bit Rate) traffic generator in NS2. The message is mainly encrypted, and then and there sent as a packet to the receiver, where it is decrypted.

# Set up UDP agents for sender and receiver

set udp_sender [new Agent/UDP]

set null_receiver [new Agent/Null]

$ns attach-agent $sender $udp_sender

$ns attach-agent $receiver $null_receiver

$ns connect $udp_sender $null_receiver

# Create a CBR traffic generator to simulate message transmission

set cbr_sender [new Application/Traffic/CBR]

$cbr_sender set packetSize_ 512

$cbr_sender set rate_ 1Mb

$cbr_sender attach-agent $udp_sender

# Encrypt message and send it at 2.0 seconds

$ns at 2.0 “set encrypted_message [encrypt_message ‘Secure Message’ $shared_secret_key]”

$ns at 2.0 “$cbr_sender start”

# Decrypt message at the receiver

$ns at 2.5 “set decrypted_message [decrypt_message $encrypted_message $shared_secret_key]”

  1. Monitor and Log the Encryption/Decryption Process

We can log the encryption and decryption process to mimic message security.

# Log the encryption and decryption process

proc log_encryption {message encrypted_message} {

puts “Original message: ‘$message’ was encrypted as: ‘$encrypted_message'”

}

proc log_decryption {encrypted_message decrypted_message} {

puts “Encrypted message: ‘$encrypted_message’ was decrypted as: ‘$decrypted_message'”

}

# Log the encryption and decryption process

$ns at 2.0 “log_encryption ‘Secure Message’ $encrypted_message”

$ns at 2.5 “log_decryption $encrypted_message $decrypted_message”

  1. Optimize Network Resources for Secure Communication

To make the network more efficient, we can reduce the bandwidth usage and minimize packet sizes. This will mimic a lightweight and resource-efficient cryptography model.

# Reduce packet size and control bandwidth for secure transmission

$ns duplex-link $sender $receiver 512Kb 10ms DropTail;# Adjust link bandwidth for efficiency

$cbr_sender set packetSize_ 256  ;# Use smaller packet sizes to reduce overhead

  1. Run the Simulation

Once the script is ready, we can execute the simulation using NS2:

ns your_script.tcl

  1. Analyze the Results

After executing the simulation, validate the trace file (symmetric_key_crypto.tr) and console output to verify:

  • The message was encrypted and decrypted using the shared key.
  • The interaction among sender and receiver was successfully secured.

We can also use NAM (Network Animator) to envision on how the encrypted message is transfer from the sender to the receiver.

  1. Extend the Simulation

We can expand the simulation by:

  • Simulating key distribution: Add a key distribution step in which the shared key is securely allocated to both nodes before communication begins.
  • Adding more encryption methods: To mimic diverse encryption techniques by adjust the encryption and decryption logic.
  • Introducing network attacks: To mimic a man-in-the-middle attack or eavesdropping to validate the robustness of the encryption.
  • Optimizing network efficiency: Utilize diverse traffic patterns such as bursty traffic, low-latency links and study how encryption affects performance.

From the demonstration we utterly aggregate the data about the installation process and implementation procedure for Symmetric Key Cryptography that was set up in the tool of ns2. More information regarding the Symmetric Key Cryptography will also be provided.