Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is a program I'm currently working on implementing a Diffie-Hellman key exchange using Java. The program is first supposed to accept two of the

This is a program I'm currently working on implementing a Diffie-Hellman key exchange using Java. The program is first supposed to accept two of the first 10,000 prime numbers for variables P and G. Secondly, the program then asks for the input of User A and User B's private keys, which both have to be in the range of 0-100. These are both implemented correctly but the problem seems to be my public and secret keys are showing up as the same after compilation, sometimes the secret keys are even different. Any explanation/examples on how to go about fixing this issue?

package diffiehellman; import java.util.Scanner;

public class Main {

static long power(long a, long b, long P) { // Checks if b value is equals to 1 if (b == 1) // Return a value return a; // Otherwise calculate and return a ^ b modulo P else return (((long )Math.pow(a, b)) % P); }

public static boolean computePrime(long x) { int i = 0; int num = 2; int count = 1; int counter = 0; String result = ""; while (count < 10000) {

for (i = 2; i < num; i++) { if (num % i == 0) { break; }

else if (i == (num - 1)) { counter = 1; } }

if(counter==1) { if (x == 2) { result = (x + " is a prime number."); break; } else if(x == num) { result = (x + " is a prime number."); break; }

else if(x != num) { count++; counter = 0; } } if(count == 10000) { return false; } num++; } return true; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); Object x = new Object(); int privateKeyA, privateKeyB; long secretKeyA,secretKeyB; long p, g; long keyA, keyB;

//Entering and computing one of the first 10,000 prime numbers. System.out.println(" Enter a prime number for G and P thats one of" + " the first 10,000 prime numbers."); do{ System.out.println("Enter P: "); p = scan.nextLong(); System.out.println(computePrime(p)+" "); }while(computePrime(p) == false); do{ System.out.println("Enter G: "); g = scan.nextLong(); System.out.println(computePrime(g)+" "); }while(computePrime(g) == false);

do{ System.out.println("Enter 'a' for User A (private key)"); privateKeyA = scan.nextInt(); }while(privateKeyA > 100 || privateKeyA < 0); do{ System.out.println("Enter 'b' for User B (private key)"); privateKeyB = scan.nextInt(); System.out.println(); }while(privateKeyB > 100 || privateKeyB < 0);

System.out.println("The value of P: " + p); System.out.println("The value of G: " + g + " "); System.out.println("Private key for User A: " + privateKeyA); System.out.println("Private key for User B: " + privateKeyB + " ");

//Calls the methods to generate keys for User A and User B keyA = power(g, privateKeyA, p); keyB = power(g, privateKeyB, p);

//Calls the methods to generate secret keys for User A and User B secretKeyA = power(keyB, privateKeyA, p); secretKeyB = power(keyA, privateKeyB, p); System.out.println("Public key for User A: " + keyA); System.out.println("Public key for User B: " + keyB + " "); System.out.println("Secret key for User A: " + secretKeyA); System.out.println("Secret Key for User B: " + secretKeyB + " "); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions