0% found this document useful (0 votes)
19 views4 pages

Cryptoda 3

The document contains Java code for two cryptographic applications: hashing using MD5 and SHA-512, and implementing a Digital Signature Standard. The first part allows users to compute MD5 and SHA-512 hashes of input text, while the second part generates keys, signs messages, and verifies signatures using a simple algorithm. Both applications include user interaction through a console menu for executing various functions.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Cryptoda 3

The document contains Java code for two cryptographic applications: hashing using MD5 and SHA-512, and implementing a Digital Signature Standard. The first part allows users to compute MD5 and SHA-512 hashes of input text, while the second part generates keys, signs messages, and verifies signatures using a simple algorithm. Both applications include user interaction through a console menu for executing various functions.

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 PDF, TXT or read online on Scribd
You are on page 1/ 4

Name:Atharva Mahesh Bhangale

Reg No:22BCE3274
Cryptography and Network Security Lab Da-3

i. SHA512 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();
}
}

Output:
ii. Digital Signature Standard
Code:
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.");
}
}

scanner.close();
}
}

Output:

You might also like