0% found this document useful (0 votes)
4 views

Elgamal

The document contains Java implementations for ElGamal encryption, MD5 and SHA-512 hashing, and Digital Signature System (DSS). It includes functions for key generation, message encryption and decryption, as well as signing and verifying messages. Each section provides a menu-driven interface for user interaction.

Uploaded by

atharvabhangale3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Elgamal

The document contains Java implementations for ElGamal encryption, MD5 and SHA-512 hashing, and Digital Signature System (DSS). It includes functions for key generation, message encryption and decryption, as well as signing and verifying messages. Each section provides a menu-driven interface for user interaction.

Uploaded by

atharvabhangale3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Elgamal

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.");
}

Sha and md5

Code:
import java.util.Scanner;
import java.nio.ByteBuffer;
import java.security.MessageDigest;

public class Main {


public static int leftRotate(int x, int c) {
return (x << c) | (x >>> (32 - c));
}

public static String md5(byte[] message) {


try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(message);
byte[] digest = md.digest();
byte[] firstRound = md.digest();

System.out.print("MD5 Intermediate (after 1st round): ");


for (byte b : firstRound) {
System.out.printf("%02x", b);
}
System.out.println();

StringBuilder hexString = new StringBuilder();


for (byte b : digest) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
} catch (Exception e) {
return "Error computing MD5";
}
}

public static String sha512(byte[] message) {


try {
MessageDigest sha = MessageDigest.getInstance("SHA-512");
sha.update(message);
byte[] hash = sha.digest();
byte[] firstRound = sha.digest();

System.out.print("SHA-512 Intermediate (after 1st round): ");


for (byte b : firstRound) {
System.out.printf("%02x", b);
}
System.out.println();

StringBuilder hexString = new StringBuilder();


for (byte b : hash) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
} catch (Exception e) {
return "Error computing SHA-512";
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("\n1. MD5");
System.out.println("2. SHA-512");
System.out.println("3. Exit");
System.out.print("Enter choice: ");

int choice = scanner.nextInt();


scanner.nextLine();

if (choice == 1) {
System.out.print("Enter text: ");
String text = scanner.nextLine();
System.out.println("MD5 Hash: " + md5(text.getBytes()));
} else if (choice == 2) {
System.out.print("Enter text: ");
String text = scanner.nextLine();
System.out.println("SHA-512 Hash: " +
sha512(text.getBytes()));
} else if (choice == 3) {
break;
} else {
System.out.println("Invalid choice. Try again.");
}
}

scanner.close();
}
}

Dss

import java.util.Scanner;
import java.math.BigInteger;
import java.security.SecureRandom;

public class Main {


private static final BigInteger P = new BigInteger("23"); // Prime
number
private static final BigInteger G = new BigInteger("5"); // Generator
private static BigInteger privateKey;
private static BigInteger publicKey;

// Key Generation for Digital Signature


public static void generateKeys() {
SecureRandom random = new SecureRandom();
privateKey = new BigInteger(20,
random).mod(P.subtract(BigInteger.ONE));
publicKey = G.modPow(privateKey, P);
System.out.println("Private Key: " + privateKey);
System.out.println("Public Key: " + publicKey);
}

// Signing a Message
public static BigInteger signMessage(String message) {
BigInteger messageHash = new BigInteger(message.getBytes()).mod(P);
return messageHash.multiply(privateKey).mod(P); // Simple signature
calculation
}

// Verifying the Signature


public static boolean verifySignature(String message, BigInteger
signature) {
BigInteger messageHash = new BigInteger(message.getBytes()).mod(P);
BigInteger verify =
signature.multiply(publicKey.modInverse(P)).mod(P);
return verify.equals(messageHash);
}

// Main Menu
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
generateKeys();

while (true) {
System.out.println("\n1. Sign Message");
System.out.println("2. Verify Signature");
System.out.println("3. Exit");
System.out.print("Enter choice: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline

if (choice == 1) {
System.out.print("Enter message to sign: ");
String message = scanner.nextLine();
BigInteger signature = signMessage(message);
System.out.println("Generated Signature: " + signature);
} else if (choice == 2) {
System.out.print("Enter message to verify: ");
String message = scanner.nextLine();
System.out.print("Enter signature: ");
BigInteger signature = scanner.nextBigInteger();
boolean isValid = verifySignature(message, signature);
System.out.println("Signature Valid: " + isValid);
} else if (choice == 3) {
break;
} else {
System.out.println("Invalid choice. Try again.");
}
}

You might also like