random prime number generation for RSA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hebaahmed
    New Member
    • May 2010
    • 4

    #1

    random prime number generation for RSA

    How do I determine random prime numbers in RSA? I have already generated numbers but can't check if prime or not.

    Here is the part of generation
    Code:
        public static void main(String[] args) throws IOException {
    int q=0;
    int p=0;
    
    int maxValue = 100;
    int maxNumber = 2;
    
    Random x = new Random ();
    
    for (int k=1; k < maxNumber; k ++) {
    p = (Math.abs(x.nextInt () % maxValue ));
    
    System.out.println("p: "+p);
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    You're looking for a Primality test. Look at some of the algorithms given, and decide which one you want to implement. Come back with your problems, if you have any.

    Comment

    • hebaahmed
      New Member
      • May 2010
      • 4

      #3
      Code:
      public static boolean isPrime(int n) {
          for(int i=2;2*i<n;i++) {
              if(n%i==0)
                  return false;
          }
          return true;
      }
      
      public static void main(String[] args) throws IOException {
      int q=0;
      int p=0;
      
      int maxValue = 100;
      int maxNumber = 2;
      
      Random x = new Random ();
      
      for (int k=1; k < maxNumber; k ++) {
      p = (Math.abs(x.nextInt () % maxValue ));
      
      System.out.println("p: "+p);
      I already got prime test algorithm but I want to make that if it's prime number ok show p=number that has been generated and being prime else break and generate another one and check again I try alot but i don't know the problem.
      Last edited by Niheel; May 13 '10, 02:55 AM. Reason: code tags, grammar, spelling

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        You probably want a do-while loop
        Code:
        do{
        Set your random number
        } while (random number is not prime);
        Then you can only leave the loop if your number IS prime.


        Also, consider changing your for loop condition from
        2*i<n TO
        i*i <= n

        Right now, 4 wouldn't work, because when i = 2, 2 * i = 2 * 2 = 4 which is not < n.

        Comment

        • hebaahmed
          New Member
          • May 2010
          • 4

          #5
          Thanks for your reply. I can do it.
          Last edited by Niheel; May 13 '10, 02:56 AM. Reason: spelling, grammar, punctuation

          Comment

          Working...