Hi,
I'm a beginner and I'm not sure what is wrong with my code or maybe someone can help. I only need to implement code in the very last function (CalculateKey).
Thanks,
import java.math.BigInteger; public class DH_TEST { public static void main(String [] args) { BigInteger q = new BigInteger("353"); // q is a prime number (the modulo) BigInteger a = new BigInteger("3"); // a is a primitive root of q (a<q) BigInteger user_A_private = new BigInteger("97"); // private component of User A USER userA = new USER(q, a, user_A_private); // create a the user A BigInteger user_B_private = new BigInteger("233"); // private component of User B USER userB = new USER(q, a, user_B_private); // create the other user BigInteger YB = userB.getPublic(); // Retrieve the public component of user B's key userA.OtherUserPublic(YB); // Store it in user A's instance (need this to calculate our key) BigInteger YA = userA.getPublic(); // Retrieve the public component of user A's key userB.OtherUserPublic(YA); // Store it in user B's instance (need this to calculate our key) BigInteger KEY_OF_USER_A = userA.CalculateKey(); System.out.println("User A's Key: " + KEY_OF_USER_A); BigInteger KEY_OF_USER_B = userB.CalculateKey(); System.out.println("User B's Key: " + KEY_OF_USER_B); } } class USER { private BigInteger q; // This is the q (the modulo) private BigInteger a; // this is the a, which is < q private BigInteger X; // our PRIVATE component of the key private BigInteger Y; // our PUBLIC component of the key private BigInteger Y_otherUser; // The other user's PUBLIC component of the key public USER(BigInteger q, BigInteger a, BigInteger X) { this.q = q; this.a = a; this.X = X; Y = a.modPow(X,q); // having a,q, and X, we can calculate Y. } public BigInteger getPublic() { return Y; } public void OtherUserPublic(BigInteger Y_otherUser) { this.Y_otherUser = Y_otherUser; } public BigInteger CalculateKey(userA, userB) { userA = Y.modPow(X,q); return userA; } }