NS_Practical
NS_Practical
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)));
Output
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));
Output:
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)));
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;
Output:
int A = (int)Math.pow(G, a) % P;
int B = (int)Math.pow(G, b) % P;
Output:
The value of P : 23
The value of G : 9
// 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"));
}
Output:
Digital Signature Simulation:
Signature: r = 449, s = 35
Verification result: Verified
System.out.println("Connected to server.");
System.out.println("Sending message to server: Hello from the client!");
out.println("Hello from the client!");
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: