0% found this document useful (0 votes)
13 views13 pages

labda3

The document outlines a Java program that simulates two asymmetric cryptographic algorithms: RSA and ElGamal. It includes code for generating keys, encrypting, and decrypting messages, along with a menu-driven interface for user interaction. The program emphasizes data validation and error handling for input values.

Uploaded by

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

labda3

The document outlines a Java program that simulates two asymmetric cryptographic algorithms: RSA and ElGamal. It includes code for generating keys, encrypting, and decrypting messages, along with a menu-driven interface for user interaction. The program emphasizes data validation and error handling for input values.

Uploaded by

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

Name : Dhruvin Mehta

Reg. No.: 22BCE0145

Cryptography and Network Security


LAB DA 3

Without using library functions develop a menu-driven code to


simulate the following Asymmetric algorithms.

1. RSA
2. Elgammal

 The program should have sufficient test cases to perform data


validation.
1. RSA

Code:

import java.util.Scanner;

public class Main {

static Scanner sc = new Scanner(System.in);


static long p, q, n, phi, e, d;

// Function to compute GCD


static long gcd(long a, long b) {
while (b != 0) {
long temp = b;
b = a % b;
a = temp;
}
return a;
}

// Function to compute modular inverse


static long modInverse(long e, long phi) {
for (long d = 1; d < phi; d++) {
if ((e * d) % phi == 1) {
return d;
}
}
return -1;
}

// Function for modular exponentiation


static long modExp(long base, long exponent, long mod) {
long result = 1;
while (exponent > 0) {
if (exponent % 2 == 1) {
result = (result * base) % mod;
}
exponent /= 2;
base = (base * base) % mod;
}
return result;
}

// Function to generate RSA keys


static void generateKeys() {
System.out.print("Enter two prime numbers (p and q): ");
p = sc.nextLong();
q = sc.nextLong();

if (p <= 1 || q <= 1 || p == q) {
System.out.println("Invalid input! Both numbers should be
distinct primes.");
return;
}
n = p * q;
phi = (p - 1) * (q - 1);

// Selecting e
for (e = 2; e < phi; e++) {
if (gcd(e, phi) == 1) {
break;
}
}

d = modInverse(e, phi);
if (d == -1) {
System.out.println("Error: Could not find modular inverse!");
return;
}

System.out.println("Public Key: (" + e + ", " + n + ")");


System.out.println("Private Key: (" + d + ", " + n + ")");
}

// Function to encrypt a message


static void encryptMessage() {
if (n == 0) {
System.out.println("Error: Generate keys first!");
return;
}

System.out.print("Enter an integer message to encrypt: ");


long message = sc.nextLong();
if (message <= 0 || message >= n) {
System.out.println("Invalid message! It should be between 1
and " + (n - 1));
return;
}

long cipher = modExp(message, e, n);


System.out.println("Encrypted Message: " + cipher);
}

// Function to decrypt a message


static void decryptMessage() {
if (n == 0) {
System.out.println("Error: Generate keys first!");
return;
}

System.out.print("Enter the encrypted message: ");


long cipher = sc.nextLong();

long decrypted = modExp(cipher, d, n);


System.out.println("Decrypted Message: " + decrypted);
}

// Menu-driven approach
public static void main(String[] args) {
while (true) {
System.out.println("\n----- RSA Simulation Menu -----");
System.out.println("1. Generate RSA Keys");
System.out.println("2. Encrypt Message");
System.out.println("3. Decrypt Message");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");

int choice = sc.nextInt();

switch (choice) {
case 1:
generateKeys();
break;
case 2:
encryptMessage();
break;
case 3:
decryptMessage();
break;
case 4:
System.out.println("Exiting program...");
sc.close();
return;
default:
System.out.println("Invalid choice! Please select a valid
option.");
}
}
}
}
Outputs:
2. Elgammal:

Pseudocode:

Code:
import java.util.Random;
import java.util.Scanner;

public class Main{

static Scanner sc = new Scanner(System.in);


static long p, g, x, y; // Public and Private Key parameters
static long k, r, encryptedMessage, decryptedMessage; //
Encryption & Decryption values

// Function for modular exponentiation (base^exp % mod)


static long modExp(long base, long exp, long mod) {
long result = 1;
while (exp > 0) {
if (exp % 2 == 1) {
result = (result * base) % mod;
}
exp /= 2;
base = (base * base) % mod;
}
return result;
}

// Function to generate keys


static void generateKeys() {
System.out.print("Enter a large prime number (p): ");
p = sc.nextLong();
System.out.print("Enter a primitive root (g): ");
g = sc.nextLong();
System.out.print("Enter private key (x): ");
x = sc.nextLong();

// Calculate public key


y = modExp(g, x, p);
System.out.println("Public Key: (p=" + p + ", g=" + g + ", y=" + y +
")");
System.out.println("Private Key: (x=" + x + ")");
}

// Function to encrypt a message


static void encryptMessage() {
if (p == 0) {
System.out.println("Error: Generate keys first!");
return;
}

System.out.print("Enter message (as integer): ");


long message = sc.nextLong();

System.out.print("Enter random integer (k): ");


k = sc.nextLong();

// Compute r and encrypted message


r = modExp(g, k, p);
encryptedMessage = (message * modExp(y, k, p)) % p;

System.out.println("Ciphertext: (r=" + r + ", C=" +


encryptedMessage + ")");
}

// Function to decrypt a message


static void decryptMessage() {
if (p == 0) {
System.out.println("Error: Generate keys first!");
return;
}

System.out.print("Enter received r: ");


long rReceived = sc.nextLong();
System.out.print("Enter received encrypted message: ");
long cReceived = sc.nextLong();

// Compute decryption
long rX = modExp(rReceived, x, p); // r^x % p
long rXInverse = modExp(rX, p - 2, p); // Modular inverse using
Fermat's theorem
decryptedMessage = (cReceived * rXInverse) % p;

System.out.println("Decrypted Message: " +


decryptedMessage);
}

// Menu-driven approach
public static void main(String[] args) {
while (true) {
System.out.println("\n----- ElGamal Encryption Menu -----");
System.out.println("1. Generate Keys");
System.out.println("2. Encrypt Message");
System.out.println("3. Decrypt Message");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");

int choice = sc.nextInt();

switch (choice) {
case 1:
generateKeys();
break;
case 2:
encryptMessage();
break;
case 3:
decryptMessage();
break;
case 4:
System.out.println("Exiting program...");
sc.close();
return;
default:
System.out.println("Invalid choice! Please select a valid
option.");
}
}
}
}

Outputs:

You might also like