Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: What is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code?

    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;
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: What is wrong with my code?

    It doesn't compile because you have two parameters but dont tell the compiler what type they are.

    public BigInteger CalculateKey(userA, userB) {

    You need to tell the compiler what type userA and userB are

    example:

    public BigInteger CalculateKey(int userA,int userB) {

  3. The Following User Says Thank You to koder632417 For This Useful Post:

    GoodbyeWorld (June 10th, 2014)

  4. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: What is wrong with my code?

    I see that you are passing userB to the function but you never use userB.

    I'm assuming that the type that you would need, as he said above, is BigInteger.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code?

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly. If you haven't already read this topic to learn other useful info for newcomers.

    In the future, please give us a hint at the trouble/problem you're trying to solve. If you're getting an error, post it in its entirety.

  6. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    After adding in the BigInteger type for both userA and userB. Now I'm getting another error about the return (userA, userB);


    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(BigInteger userA, BigInteger userB) {
     
            userA = Y.modPow(X,q);
            userB = Y.modPow(X,q);
     
            return (userA, userB);
    	}
    }

  7. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: What is wrong with my code?

    There is no need to use two parameters in the method because they are both doing the same thing and don't interact. Just use one and call it twice.

    	public BigInteger CalculateKey(BigInteger user) {
     
            user = Y.modPow(X,q);
     
     
            return user;
    }

    Then call it twice if you want to do the same for two objects:

    CalculateKey(usr1);
    CalculateKey(usr2);

  8. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code?

    Now I'm getting another error about the return (userA, userB)
    Again, you didn't post the error. Is there a reason?

  9. #8
    Junior Member
    Join Date
    Jun 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    This is the error I'm getting: (Below in RED)


    DH_TEST.java:51: error: method CalculateKey in class USER cannot be applied to g
    iven types;
    BigInteger KEY_OF_USER_A = userA.CalculateKey();
    ^
    required: BigInteger
    found: no arguments
    reason: actual and formal argument lists differ in length
    DH_TEST.java:58: error: method CalculateKey in class USER cannot be applied to g
    iven types;
    BigInteger KEY_OF_USER_B = userB.CalculateKey();
    ^
    required: BigInteger
    found: no arguments
    reason: actual and formal argument lists differ in length
    2 errors



    import java.math.BigInteger;
     
    /**
     *		** Diffie-Hellman **
     * Two parties use a key agreement protocol to generate identical secret keys for 
     * encryption without ever having to transmit the secret key. The protocol works by 
     * both parties agreeing on a set of values (a prime, a base, and a private value) 
     * which are used to generate a key pair.
     */
    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
     
     
     
    		//
    		// Simulation: Transmit YB and YA to each other
    		//
     
    		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)
     
    		//
    		// Done exchanging data
    		//
     
     
     
    		//
    		// User A calculates the key and displays it.
    		//
    		BigInteger KEY_OF_USER_A = userA.CalculateKey();
    		System.out.println("User A's Key: " + KEY_OF_USER_A);
     
     
    		//
    		// User A calculates the key and displays it.
    		//
    		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
     
     
     
    	/**
    	 * Pass to us the q,a and our PRIVATE component of our key (the X).
    	 * We then calculate our PUBLIC component of our key (the Y)
    	 */
    	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.
    	}
     
    	/**
    	 * Returns our PUBLIC component of our key.
    	 * The other user will need this to calculate the final secret key.
    	 */
    	public BigInteger getPublic() {
    		return Y;
    	}
     
     
    	/**
    	 * We need to know the other user's PUBLIC component of the key.
    	 * Store it so we can use it to calculate our final secret key.
    	 */
    	public void OtherUserPublic(BigInteger Y_otherUser) {
    		this.Y_otherUser = Y_otherUser;
    	}
     
    	/**
    	 * Calculates and returns the SECRET KEY (in the slides this is the K);
    	 */
    	public BigInteger CalculateKey(BigInteger userA) {
    		//
    		// *** TO BE IMPLEMENTED ***
    		// CALCULATE THE KEY AND RETURN IT
    		// NOTE THAT THIS FUNCTION RETURNS A BigInteger Object
    		//
            userA = Y.modPow(X,q);
     
            return userA;
    	}
    }

  10. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What is wrong with my code?

    Re-read the error:

    DH_TEST.java:51: error: method CalculateKey in class USER cannot be applied to given types;
    BigInteger KEY_OF_USER_A = userA.CalculateKey();
    ^
    required: BigInteger
    found: no arguments

    Translating: the method CalculateKey() requires a parameter of type BigInteger, but it was called with no arguments.

    To fix: call the method with the required parameters.

Similar Threads

  1. What's wrong with my code?
    By deejaypanininini in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 8th, 2014, 08:50 PM
  2. [SOLVED] can anyone help me what is wrong with my code?
    By marc172 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2013, 09:53 PM
  3. What's wrong with my code?
    By shen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 06:34 AM
  4. WHAT IS WRONG WITH MY CODE
    By YG N JYP FAM4EVA in forum Object Oriented Programming
    Replies: 1
    Last Post: February 26th, 2013, 05:14 PM
  5. What is wrong in the code
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 29th, 2011, 12:09 PM