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 3 of 3

Thread: Big Integer help

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Big Integer help

    Hi all,

    I'm currently working on a RSA Encryption using Fast Exponential technique. As the numbers I'm working with will be very huge, I am forced to use BigInteger.

    As this is the first time I'm using Big Integer, I encountered quite a number of issues. I'll try my best to explain the errors I'm facing:

    1. I had done this step: BigInteger d;
    So I'm currently trying to get d = 0;
    However, I keep getting an error required: BigInteger, found int.

    When I changed it to d = new BigInteger(0) , I get this error:

    java:19: error: constructor BigInteger in class BigInteger cannot be applied to given types;

    d = new BigInteger(0);
    ^
    required: no arguments
    found: int
    reason: actual and formal argument lists differ in length


    Part of Code:
    public class BigFastExponential
    {
    BigInteger e; // public exponenet: all has this
    BigInteger d; // private key: only key owner has this
    BigInteger n; // public modulus: all has this

    /**
    * Create a LongRSA object for a public key holder,
    * with just e and n.
    */
    public BigFastExponential(BigInteger public_e, BigInteger public_n)
    {
    e = public_e;
    n = public_n;
    d = new BigInteger(0);
    }



    2. Similar to above, I tried the following if (d == 0).

    I also tried :
    BigInteger = result;
    result = 1;

    This is the error I received:

    java:57: error: incomparable types: BigInteger and int
    if (d == 0) {
    ^
    BigFastExponential.java:77: error: incompatible types
    result = 1;
    ^
    required: BigInteger
    found: int


    3. I'm also getting some erros about the operators for BigIntegers. All these used to work when I was using LONG instead of BigInteger.

    Error:
    BigFastExponential.java:79: error: bad operand types for binary operator '>'
    while (e > 0)
    ^
    first type: BigInteger
    second type: int
    BigFastExponential.java:81: error: bad operand types for binary operator '&'
    if ((e & 1) == 1) // & = bitwise &&
    ^
    first type: BigInteger
    second type: int
    BigFastExponential.java:83: error: bad operand types for binary operator '*'
    result = (result * b) % n;
    ^
    first type: BigInteger
    second type: BigInteger
    BigFastExponential.java:85: error: bad operand types for binary operator '>>'
    e = e >> 1;
    ^
    first type: BigInteger
    second type: int
    BigFastExponential.java:86: error: bad operand types for binary operator '*'
    b = (b * b) % n;
    ^
    first type: BigInteger
    second type: BigInteger



    Code segment:
    public BigInteger decrypt(BigInteger c) {
    if (d == 0) {
    throw new IllegalStateException("No private key value set.");
    }
    BigInteger M;
    M = modpow(c, d, n);
    return M;
    }


    public static final BigInteger modpow(BigInteger b, BigInteger e, BigInteger n)
    {
    BigInteger result;
    result = 1;

    while (e > 0)
    {
    if ((e & 1) == 1) // & = bitwise &&
    {
    result = (result * b) % n;
    }
    e = e >> 1;
    b = (b * b) % n;
    }

    return result;
    }


    4. Last but not least, I did my research and I was told that to get input as BigInteger, I have to use scanner.nextBigInteger();

    I did so but I received a very weird error:

    BigFastExponential.java:104: error: incompatible types
    n = user_input.nextBigInteger();
    ^
    required: BigInteger
    found: java.math.BigInteger


    Are they different ?

    Code Segment:

    public static void main(String[] args)
    {
    BigInteger p;
    BigInteger q;
    BigInteger e;
    BigInteger n;
    BigInteger d;
    BigInteger M;

    // Lab 2 - (2)(a) Bob uses PublicAlice key to encrypt a plaintext message M
    Scanner user_input = new Scanner (System.in);
    System.out.print("Enter value of n: "); // n = value of pq. Bob does NOT know p and q, only n
    n = user_input.nextBigInteger();

    System.out.print("Enter value of e: "); // (pq/n,e) = Public Alice Key
    e = user_input.nextBigInteger();

    System.out.print("Enter value of d: "); // for decryption using Private Alice Key
    d = user_input.nextBigInteger();


    Thanks in advance for all your help ! Sorry for the long list of questions on my very first post !

    Warmest Regards,
    Zeke


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Big Integer help

    BigInteger is a class, not a primitive like an int. You have to use the class's methods to compare the contents of the class.

    BigFastExponential.java:104: error: incompatible types
    I don't get that error. What imports or definitions do you have? Do you have your own version of BigInteger?
    Last edited by Norm; April 17th, 2012 at 10:01 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Big Integer help

    Oh. Turns out I there was some corruption in the library java.util.BigInteger I tried to import. I figured out the rest already. Thanks !

Similar Threads

  1. Integer program help
    By oriordc2 in forum Member Introductions
    Replies: 3
    Last Post: February 26th, 2012, 02:38 PM
  2. Use of Integer.parseInt
    By tarkal in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:37 AM
  3. Help with ArrayList<Integer>
    By shanklove in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2010, 09:15 AM
  4. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  5. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM

Tags for this Thread