Cyber Security Lab Assignment 6
A. Caesar Cipher
1. Implement the Caesar Cipher
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter message:");
String str = sc.nextLine();
System.out.println("Enter encryption key (integer):");
int key = sc.nextInt();
String encrypted = encrypt(str, key);
String decrypted = decrypt(encrypted, key);
System.out.println("Shift Value: " + key);
System.out.println("Ciphertext: " + encrypted);
System.out.println("Decrypted Text: " + decrypted);
}
public static String encrypt(String str, int key) {
String encrypted = "";
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = (c + key - 'A') % 26 + 'A';
} else if (Character.isLowerCase(c)) {
c = (c + key - 'a') % 26 + 'a';
}
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key) {
String decrypted = "";
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = (c - key - 'A' + 26) % 26 + 'A';
} else if (Character.isLowerCase(c)) {
c = (c - key - 'a' + 26) % 26 + 'a';
}
decrypted += (char) c;
}
return decrypted;
}
}
2. Modified Caesar Cipher Program
import java.util.Scanner;
public class ModifiedCaesarCipher {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter message:");
String str = sc.nextLine();
System.out.println("Enter encryption key (integer):");
int key;
try {
key = sc.nextInt();
} catch (Exception e) {
System.out.println("Invalid key! Must be an integer.");
return;
}
while (true) {
System.out.println("1. Encrypt\n2. Decrypt\n3. Exit...");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Encrypted message: " + encrypt(str, key));
break;
case 2:
System.out.println("Decrypted message: " + decrypt(encrypt(str, key), key));
break;
case 3:
System.exit(0);
break;
default:
System.out.println("Invalid option..");
}
}
}
public static String encrypt(String str, int key) {
String encrypted = "";
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = (c + key - 'A') % 26 + 'A';
} else if (Character.isLowerCase(c)) {
c = (c + key - 'a') % 26 + 'a';
}
System.out.println("Shifted Character: " + (char) c);
encrypted += (char) c;
}
return encrypted;
}
public static String decrypt(String str, int key) {
String decrypted = "";
for (int i = 0; i < str.length(); i++) {
int c = str.charAt(i);
if (Character.isUpperCase(c)) {
c = (c - key - 'A' + 26) % 26 + 'A';
} else if (Character.isLowerCase(c)) {
c = (c - key - 'a' + 26) % 26 + 'a';
}
System.out.println("Shifted Character: " + (char) c);
decrypted += (char) c;
}
return decrypted;
}
}
B. Diffie-Hellman Key Exchange
1. Implement the Diffie-Hellman Key Exchange Algorithm
import java.io.*;
import java.math.BigInteger;
class DiffieHellman {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter prime number:");
BigInteger p = new BigInteger(br.readLine());
System.out.print("Enter primitive root of " + p + ": ");
BigInteger g = new BigInteger(br.readLine());
System.out.println("Enter value for x less than " + p + ": ");
BigInteger x = new BigInteger(br.readLine());
BigInteger R1 = g.modPow(x, p);
System.out.println("R1 = " + R1);
System.out.print("Enter value for y less than " + p + ": ");
BigInteger y = new BigInteger(br.readLine());
BigInteger R2 = g.modPow(y, p);
System.out.println("R2 = " + R2);
BigInteger k1 = R2.modPow(x, p);
System.out.println("Key calculated at Alice's side: " + k1);
BigInteger k2 = R1.modPow(y, p);
System.out.println("Key calculated at Bob's side: " + k2);
System.out.println("Diffie-Hellman secret key exchange completed.");
}
}
2. Modified Diffie-Hellman Key Exchange
import java.io.*;
import java.math.BigInteger;
class ModifiedDiffieHellman {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter prime number:");
BigInteger p;
try {
p = new BigInteger(br.readLine());
if (!p.isProbablePrime(10)) {
throw new Exception("Not a valid prime number.");
}
} catch (Exception e) {
System.out.println("Invalid input! " + e.getMessage());
return;
}
System.out.print("Enter primitive root of " + p + ": ");
BigInteger g = new BigInteger(br.readLine());
System.out.println("Enter value for x less than " + p + ": ");
BigInteger x = new BigInteger(br.readLine());
BigInteger R1 = g.modPow(x, p);
System.out.println("Computed R1 = " + R1);
System.out.print("Enter value for y less than " + p + ": ");
BigInteger y = new BigInteger(br.readLine());
BigInteger R2 = g.modPow(y, p);
System.out.println("Computed R2 = " + R2);
BigInteger k1 = R2.modPow(x, p);
System.out.println("Intermediate Key at Alice's side: " + k1);
BigInteger k2 = R1.modPow(y, p);
System.out.println("Intermediate Key at Bob's side: " + k2);
System.out.println("Diffie-Hellman key exchange successful.");
}
}
These implementations strictly follow your provided notes and structure. Let me know if you
need any modifications or clarifications.