How to Implement Public Key Cryptography in NS2
To implement Public Key Cryptography (PKC) in NS2, we usually replicate the secure communication channel among nodes in which the exchange of information is encoded using public/private key pairs. Public key cryptography is required for securely transmitting data, specifically in untrusted networks. However NS2 does not directly assist an encryption or cryptographic operations that can mimic the cryptographic process via custom approaches to denote the key exchange and encryption/decryption process.
Here’s a step-by-step guide on how to simulate Public Key Cryptography (PKC) in NS2.
Step-by-Step Implementation:
- Set up NS2
Make sure NS2 is installed on system. we can install it using the following command:
sudo apt-get install ns2
- Define the Network Topology
Initiate, describe a basic network topology with sender and receiver nodes to mimic secure interaction using public key cryptography.
set ns [new Simulator]
set tracefile [open public_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
- Simulate Public and Private Keys
In public key cryptography, each node has a public/private key pair. Public keys are exchanged among nodes, and messages are encoded with the recipient’s public key and decoded with the recipient’s private key.
(A) Generate Public/Private Keys
We can mimic key generation and storage. For simplicity, assume each node has a key pair, and the keys are strings or numbers.
# Simulate public/private key pairs for sender and receiver
set sender_private_key “priv_key_sender”
set sender_public_key “pub_key_sender”
set receiver_private_key “priv_key_receiver”
set receiver_public_key “pub_key_receiver”
(B) Exchange Public Keys
Public keys need to be exchanged among nodes before secure communication can begin. This step mimics the exchange of public keys among the sender and the receiver.
# Simulate public key exchange
proc exchange_public_keys {sender receiver} {
global sender_public_key receiver_public_key
puts “Sender public key sent to receiver: $sender_public_key”
puts “Receiver public key sent to sender: $receiver_public_key”
}
# Simulate public key exchange at the start of communication
$ns at 0.5 “exchange_public_keys sender receiver”
- Encrypt Messages Using Public Key
Once the public keys are swap over, the sender can encode the message using the receiver’s public key. The receiver can then decode the message using its private key.
(A) Encrypt Message Using Public Key
Mimic encryption by implementing a basic function to the message and the recipient’s public key.
# Simulate message encryption using the receiver’s public key
proc encrypt_message {message public_key} {
puts “Encrypting message: ‘$message’ using public key: $public_key”
return “encrypted_$message_with_$public_key” ;# Simulate the encrypted message
}
# Encrypt a sample message at 1 second
$ns at 1.0 “set encrypted_message [encrypt_message ‘Hello Receiver!’ $receiver_public_key]”
- Decrypt Messages Using Private Key
The receiver decodes the message using its private key. Mimic decryption by implement a reverse process to the encrypted message.
(A) Decrypt Message Using Private Key
To mimic decryption by retreating the encryption process using the recipient’s private key.
# Simulate message decryption using the receiver’s private key
proc decrypt_message {encrypted_message private_key} {
puts “Decrypting message: ‘$encrypted_message’ using private key: $private_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 $receiver_private_key]”
- Simulate Secure Data Transmission
Now, configure the actual data transmission. The sender wills transmit an encrypted message to the receiver, who will then decode it. We can replicate this as a packet transmission in NS2.
(A) Set up Data Transmission between Sender and Receiver
Replicate secure message transmission in which the information is first encrypted by the sender, routed over the network, and decrypted by the receiver.
# Set up UDP agents for sender and receiver
set udp_sender [new Agent/UDP]
set udp_receiver [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’ $receiver_public_key]”
$ns at 2.0 “$cbr_sender start”
# Decrypt message at the receiver
$ns at 2.5 “set decrypted_message [decrypt_message $encrypted_message $receiver_private_key]”
- Monitor and Log the Transmission
You can log the encryption and decryption process to simulate 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 process
$ns at 2.0 “log_encryption ‘Secure Message’ $encrypted_message”
$ns at 2.5 “log_decryption $encrypted_message $decrypted_message”
- Run the Simulation
Once the script is ready, execute the simulation using NS2:
ns your_script.tcl
- Analyse Results
After executing the simulation, evaluate the trace file (public_key_crypto.tr) and the console output to validate:
- Public keys were exchanged appropriately.
- Messages were encoded and decoded using the respective keys.
- Data was routed securely among nodes.
We can also envision the network activity using NAM (Network Animator) to see how packets were sent securely among nodes.
- Extend the Simulation
We can expand this implementation by:
- Simulating key management: Add key distribution centres or PKI (Public Key Infrastructure) for more complex key exchange scenarios.
- Adding message signing: execute message signing using the sender’s private key to make sure message authenticity.
- Simulating attacks: Establish eavesdropping or man-in-the-middle assaults to validate the security of the public key system.
- Implementing hybrid encryption: Incorporate public key encryption with symmetric encryption for performance optimization.
In this module, we had clearly understood the implementation procedures, sample snippets were given to enforce the Public Key Cryptography with the help of ns2 tool. We also deliver further significant information regarding the Public Key Cryptography will be provided. Ns2project.com team is here to help you with your performance analysis. We provide personalized guidance and support for implementing Public Key Cryptography in NS2.