0% found this document useful (0 votes)
2 views10 pages

NS_Practical

The document provides implementations of various cryptographic algorithms including symmetric key algorithms (AES, RC6, Blowfish), asymmetric key algorithms, key exchange methods (Diffie-Hellman), digital signature schemes, and SSL for secure communication. It also includes examples of intrusion detection systems and network monitoring tools. Each section contains code snippets demonstrating the functionality and outputs of the respective algorithms.

Uploaded by

Manohar Samuel T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

NS_Practical

The document provides implementations of various cryptographic algorithms including symmetric key algorithms (AES, RC6, Blowfish), asymmetric key algorithms, key exchange methods (Diffie-Hellman), digital signature schemes, and SSL for secure communication. It also includes examples of intrusion detection systems and network monitoring tools. Each section contains code snippets demonstrating the functionality and outputs of the respective algorithms.

Uploaded by

Manohar Samuel T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

// 1.

IMPLEMENT SYMMETRIC KEY ALGORITHM USING AES


import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESExample {


public static void main(String[] args) throws Exception {
String original = "AES Encryption";
SecretKey key = new SecretKeySpec("1234567890123456".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, key);
String encrypted =
Base64.getEncoder().encodeToString(cipher.doFinal(original.getBytes()));

cipher.init(Cipher.DECRYPT_MODE, key);
String decrypted = new
String(cipher.doFinal(Base64.getDecoder().decode(encrypted)));

System.out.println("Original value: " + original);


System.out.println("Encrypted value: " + encrypted);
System.out.println("Decrypted value: " + decrypted);
}
}

Output

Original value: AES Encryption


Encrypted value: V5E9I52IxhMaW4+hJhl56g==
Decrypted value: AES Encryption
// 2.IMPLEMENT SYMMETRIC KEY ALGORITHM USING RC6
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.util.Arrays;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class RC6Example {


public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
String plainText = "Hello, world!";
byte[] keyBytes = "1234567812345678".getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "RC6");
Cipher cipher = Cipher.getInstance("RC6", "BC");

cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cipherText = cipher.doFinal(plainText.getBytes());

cipher.init(Cipher.DECRYPT_MODE, key);
String decrypted = new String(cipher.doFinal(cipherText));

System.out.println("Plain text: " + plainText);


System.out.println("Cipher text: " + Arrays.toString(cipherText));
System.out.println("Decrypted text: " + decrypted);
}
}

Output:

Plain text: Hello, world!


Cipher text: [77, -86, -14, -63, -74, -2, 17, -11, -76, -34, -48, -75, -38, -67, -35,
87]
Decrypted text: Hello, world!
// 3.IMPLEMENT SYMMETRIC KEY ALGORITHM USING BLOWFISH
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class BlowfishExample {


public static void main(String[] args) throws Exception {
String password = "Knf@123";
SecretKeySpec key = new SecretKeySpec("MySecretKey".getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");

cipher.init(Cipher.ENCRYPT_MODE, key);
String encrypted =
Base64.getEncoder().encodeToString(cipher.doFinal(password.getBytes()));

cipher.init(Cipher.DECRYPT_MODE, key);
String decrypted = new
String(cipher.doFinal(Base64.getDecoder().decode(encrypted)));

System.out.println("Password: " + password);


System.out.println("Encrypted text: " + encrypted);
System.out.println("Decrypted text: " + decrypted);
}
}

Output:

Password: Knf@123
Encrypted text: 4DTHqnctCuk=
Decrypted text: Knf@123
// 4.IMPLEMENT ASYMMETRIC KEY ALGORITHM
public class AsymmetricExample {
public static void main(String[] args) {
int message = 12;
int e = 5, n = 33, d = 29;

int encrypted = (int)Math.pow(message, e) % n;


int decrypted = (int)Math.pow(encrypted, d) % n;

System.out.println("Message data = " + message + ".000000");


System.out.println("Encrypted data = " + encrypted + ".000000");
System.out.println("Original Message Sent = " + decrypted + ".000000");
}
}

Output:

Message data = 12.000000


Encrypted data = 3.000000
Original Message Sent = 12.000000

// 5.IMPLEMENT ASYMMETRIC KEY EXCHANGE ALGORITHM


public class DiffieHellmanExample {
public static void main(String[] args) {
int P = 23, G = 9;
int a = 4, b = 3;

int A = (int)Math.pow(G, a) % P;
int B = (int)Math.pow(G, b) % P;

int secretA = (int)Math.pow(B, a) % P;


int secretB = (int)Math.pow(A, b) % P;

System.out.println("The value of P : " + P);


System.out.println("The value of G : " + G);
System.out.println("\nThe private key a for Alice : " + a);
System.out.println("The private key b for Bob : " + b);
System.out.println("\nSecret key for the Alice is : " + secretA);
System.out.println("Secret Key for the Bob is : " + secretB);
}
}

Output:
The value of P : 23
The value of G : 9

The private key a for Alice : 4


The private key b for Bob : 3

Secret key for the Alice is : 16


Secret Key for the Bob is : 16
// 6.IMPLEMENT DIGITAL SIGNATURE SCHEME

public class SimpleDigitalSignature {


public static void main(String[] args) {
int p = 10601, q = 53, g = 6089;
int x = 6, k = 3, h = 12619;

// Public key generation (y = g^x mod p)


int y = modExp(g, x, p);

// Signature generation
int r = modExp(g, k, p) % q;
int s = (modInverse(k, q) * (h + x * r)) % q;

// Signature verification
int w = modInverse(s, q);
int u1 = (h * w) % q;
int u2 = (r * w) % q;
int v = ((modExp(g, u1, p) * modExp(y, u2, p)) % p) % q;

// Output
System.out.println("Digital Signature Simulation:");
System.out.println("Signature: r = " + r + ", s = " + s);
System.out.println("Verification result: " + (v == r ? "Verified" : "Not
Verified"));
}

// Modular exponentiation (base^exp mod mod)


static int modExp(int base, int exp, int mod) {
int res = 1;
base %= mod;
while (exp > 0) {
if ((exp & 1) == 1) res = (res * base) % mod;
exp >>= 1;
base = (base * base) % mod;
}
return res;
}

// Modular inverse (a^-1 mod m)


static int modInverse(int a, int m) {
a = a % m;
for (int x = 1; x < m; x++) if ((a * x) % m == 1) return x;
return 1;
}
}

Output:
Digital Signature Simulation:
Signature: r = 449, s = 35
Verification result: Verified

// 7.MESSAGE INTEGRITY AND CONFIDENTIALITY USING SSL


// SERVER
import javax.net.ssl.*;
import java.io.*;
import java.net.*;

public class SSLServer {


public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.keyStore", "serverkeystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");

SSLServerSocketFactory ssf = (SSLServerSocketFactory)


SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(1234);

System.out.println("Server waiting for client connection...");


SSLSocket socket = (SSLSocket) ss.accept();

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

String msg = in.readLine();


System.out.println("Received from client: " + msg);
out.println("Processed: " + msg);
socket.close();
}
}
// CLIENT
import javax.net.ssl.*;
import java.io.*;

public class SSLClient {


public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.trustStore", "serverkeystore.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "password");

SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();


SSLSocket socket = (SSLSocket) sf.createSocket("localhost", 1234);

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

System.out.println("Connected to server.");
System.out.println("Sending message to server: Hello from the client!");
out.println("Hello from the client!");

String response = in.readLine();


System.out.println("Received response from server: " + response);
socket.close();
}
}

Output:

SERVER OUTPUT :
Server waiting for client connection...
Client connected.
Received from client: Hello from the client!
Sent to client: Processed: Hello from the client!

CLIENT OUTPUT :
Connected to server.
Sending message to server: Hello from the client!
Received response from server: Processed: Hello from the client!
// 8.DEMONSTRATE INTRUSION DETECTION SYSTEM USING ANY TOOL
import java.util.regex.*;
public class SimpleIDS {
public static void main(String[] args) {
String networkTraffic = "Some network traffic with a suspicious pattern";
String intrusionPattern = "suspicious pattern";
Pattern pattern = Pattern.compile(intrusionPattern);
Matcher matcher = pattern.matcher(networkTraffic);
if (matcher.find()) {
System.out.println("Intrusion detected: " + intrusionPattern);
} else {
System.out.println("No intrusion detected.");
}
}
}

Output:

Intrusion detected: suspicious pattern

// 9.EXPLORE NETWORK MONITORING TOOL


import jpcap.*;
import jpcap.packet.*;
public class NetworkMonitor {
public static void main(String[] args) throws Exception {
NetworkInterface[] devices = JpcapCaptor.getDeviceList();
if (devices.length == 0) {
System.out.println("No network interface found. Make sure you have the
required permissions.");
return;
}
int selectedDeviceIndex = 0;
NetworkInterface selectedDevice = devices[selectedDeviceIndex];
JpcapCaptor captor = JpcapCaptor.openDevice(selectedDevice, 2000, true, 20);
System.out.println("Monitoring " + selectedDevice.name + "...");
while (true) {
Packet packet = captor.getPacket();
if (packet != null) {
System.out.println(packet);
}
}
}
}
Output:

Ethernet packet (source MAC: 00:11:22:33:44:55, destination MAC: AA:BB:CC:DD:EE:FF)


IPv4 packet (source IP: 192.168.1.2, destination IP: 8.8.8.8, protocol: TCP)
TCP packet (source port: 12345, destination port: 80, flags: SYN)
Payload data: Hello, this is a sample packet!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy