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

Thread: I wanted to ask if i am doing this RSA assignment right i got it to work! (Assignments purpose was to use methods)

  1. #1
    Junior Member IBeeJava's Avatar
    Join Date
    Mar 2013
    Location
    Winnipeg
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking I wanted to ask if i am doing this RSA assignment right i got it to work! (Assignments purpose was to use methods)

    Assignments purpose was to use methods
    Question 2 in the PDF file provided

    import java.math.BigDecimal;
    import java.util.*;
     
    public class HussainDilawerA3Q2 {
     
      public static void main(String[] args) {
     
        long p = randomPrime();  // Generate p prime number
        long q = randomPrime();  // Generate q prime number 
        long n = p*q;
        long phi = (p-1)*(q-1);
        long e = generateEKey(phi);
        long d = generateDKey(phi, e);
        [ATTACH]1901[/ATTACH]
        System.out.println("e: "+e+", d: "+d+".");
     
        long enCr = encrypt(2216056, e, n);
        long dCr = decrypt(enCr, d, n);
        System.out.println(enCr);
        System.out.println(dCr);
        System.out.println(d);
     
     
     
        //this method call will give you the actall mesage after decryption
        long dCr1 = decrypt(2216056, 53972333, 269895149);   // its message is 109
        long dCr2 = decrypt(254084839, 53972333, 269895149);   // its message is 101
        System.out.println(dCr1);
        System.out.println(dCr2);
      }
     
      // Method to generate prime number in range 100 - 20000
      public static long randomPrime(){
     
        // Generate Random Number up to 20000
        long min = 100;
        long max = 20000;
        Random rnd = new Random();
        long rand = min+((long)(rnd.nextDouble()*max));
     
     
        // Keep checking until the random number is prime
        boolean test = isPrime(rand);
     
        while (test == false){
          rand = min+((long)(rnd.nextDouble()*max));
          test = isPrime(rand);
        }
     
        // return result
        return rand;
      }
     
     
      // Method to check if random number is prime
      public static boolean isPrime(long rand) {
     
        for (int i=2; i < rand; i++){ 
          if(rand%i == 0 && rand != i) {
            return false;
     
          }
        }
        return true;
      }
     
      public static long generateEKey(long phi){
     
        boolean result = false;
        for (long e=2; e<phi; e++){
     
          boolean test = isPrime(e);
          if(phi%e == 0 || test == false) {
            result = false;
          }
          else {
            return e;
          }
        }return 0;
      } 
     
      public static long generateDKey(long phi, long e){
        for(long k = 1; k < phi; k++){
          long d = (k*phi+1)/e;
          if((d*e) % phi==1){
            return d;
          }
        }
     
        return 0;
      }
     
      public static long encrypt(long message, long e, long n){
     
    	long result = expandedPowerMod(message, e, n);
     
    	return result;  
      }
     
      public static long decrypt(long message, long d, long n){
     
      long result = expandedPowerMod(message, d, n);  
     
    	return result;  
      }
     
      public static long expandedPowerMod(long base1,long exponent, long modulus)
      {
          long result = 0;
     
          if (base1 < 1 || exponent < 0 || modulus < 1)   // just and error check for valid values
              return -1;
     
          result = 1;
          while (exponent > 0)
          {
              if ((exponent % 2) == 1)    // first iteration only 
              {
                  result = (result * base1) % modulus;
              }
              base1 = (base1 * base1) % modulus;    // multiplied and added in orignal values after taking modulus
     
              BigDecimal bd = new BigDecimal(Math.floor(exponent / 2 ));    // these two steps will convert long to decimal as after division there might be some decimal values
              exponent = bd.setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
          }
          return result;
      }
    }


  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: I wanted to ask if i am doing this RSA assignment right i got it to work! (Assignments purpose was to use methods)

    Can you explain what problems you are having?
    Do you have any questions about the code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: I wanted to ask if i am doing this RSA assignment right i got it to work! (Assignments purpose was to use methods)

    It compiles, it runs, you use methods like the assignment asks. Not sure what else you're looking for. Open your assignment file and for each paragraph as yourself "Did I do that?" (In your best Steve Urkel voice). If you can answer yes, turn it in and see what your teacher says.

  4. #4
    Junior Member IBeeJava's Avatar
    Join Date
    Mar 2013
    Location
    Winnipeg
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I wanted to ask if i am doing this RSA assignment right i got it to work! (Assignments purpose was to use methods)

    Im basically not understanding what the whole RSA thing is like i made the program according to and with the help of what was given to me in the assignment. Yet i dont know that well what the question is really for, and i wanted to ask someone who can tell me where the code overflows because sometimes somewhere even the long values cant hold some values so the program overflows and returns me a 0.

    --- Update ---

    I have done that lol, and in Steve Urkels voice. i did it and it runs properly i just want to make sure its done exactly like it says because i cant afford to lose marks on this puppy

  5. #5
    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: I wanted to ask if i am doing this RSA assignment right i got it to work! (Assignments purpose was to use methods)

    somewhere even the long values cant hold some values so the program overflows and returns me a 0.
    See the tutorial for the max values a variable can hold:
    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

    If you need bigger values, you'll need to use the BigInteger class.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member IBeeJava's Avatar
    Join Date
    Mar 2013
    Location
    Winnipeg
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I wanted to ask if i am doing this RSA assignment right i got it to work! (Assignments purpose was to use methods)

    Thank-you for that i will definitely look into that and i used the BigDecimal class for the expandedPowerMod, then i converted it to long so i think i am safe on that part. Just not sure on the part where i generate the e and d key, but thank you very much

Similar Threads

  1. Need help with calling methods from classes - Java Assignment
    By seph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 10th, 2012, 08:12 PM
  2. I want assignments
    By .Swing in forum The Cafe
    Replies: 0
    Last Post: June 9th, 2012, 09:14 PM
  3. Help wanted, 20$ a week - you only work a few minutes.
    By kxsb11 in forum Paid Java Projects
    Replies: 1
    Last Post: January 30th, 2012, 03:55 AM
  4. Help with assignments Methods Java
    By jocke01 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 8th, 2011, 08:38 PM
  5. How do get and set methods work??
    By merdzins in forum Object Oriented Programming
    Replies: 2
    Last Post: December 25th, 2010, 03:17 AM

Tags for this Thread