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

Thread: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    for example: not working when prime nos are 7 and 13.. and when data is 36.

    /* Algorithm:
        Choose p = 3 and q = 11
        Compute n = p * q = 3 * 11 = 33
        Compute f(n) = (p - 1) * (q - 1) = 2 * 10 = 20
        Choose e such that 1 < e < f(n) and e and n are coprime. Let e = 7
        Compute a value for d such that (d * e) % f(n) = 1. One solution is d = 3 [(3 * 7) % 20 = 1]
        Public key is (e, n) => (7, 33)
        Private key is (d, n) => (3, 33)
        The encryption of m = 2 is c = 27 % 33 = 29
        The decryption of c = 29 is m = 293 % 33 = 2
    */
    import java.io.*;
     
    public class RSA
    {
      public static void main(String args[]) throws IOException
      {
         BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter 1st prime nos:");
         int p=Integer.parseInt(buff.readLine());
         System.out.println("Enter 2nd prime nos:");
         int q=Integer.parseInt(buff.readLine());
         //prime
         boolean flag=true;
         boolean p_true=check(p,flag);     
         boolean q_true=check(q,flag);
         if((p_true==true) && (q_true==true))
            {
               System.out.println("Nos are prime");
            }
        else
            {
               System.out.println("Nos are not prime");
            }
     
     
         int n=p*q;
         int N=((p-1)*(q-1));
         int e=2;
         for(int i=2;i<N;i++)      
            {
               if((N%i)!=0)
                 {e=i;break;}
               else
                  {continue;
                   }            
            }System.out.println("e="+e);
     
     
     
     
           int d;
           for(int i=1;;i++)
             {
               if(((i*e)%N)==1)
                 { d=i;
                  break;}
               else continue; 
             }
           System.out.println("d="+d);
     
     
         System.out.println("Enter data to be encrypted");
         int data=Integer.parseInt(buff.readLine());
         int encrypt_data=(((int)Math.pow(data,e))%n);
          System.out.println("encrypted data"+encrypt_data);
         int decrypt_data=(((int)Math.pow(encrypt_data,d))%n);
          System.out.println("decrypted data"+decrypt_data);
    }
         public static boolean check(int p, boolean flag)
            { flag=true;
               for(int i=2;i<p;i++)
                 {
                    if(p%i!=0)
                      continue;
                    else 
                    {flag=false;
                    break;}
                 }return flag;
            }
    }


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

    Default Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    What is the input and output for the program?
    When I used this:
         StringReader sr = new StringReader("7\n13\n36\n");
         BufferedReader buff=new BufferedReader(sr); //new InputStreamReader(System.in));
    The output was:
    Running: java -cp . RSA_Problem

    Enter 1st prime nos:
    Enter 2nd prime nos:
    Nos are prime
    e=5
    d=29
    Enter data to be encrypted
    encrypted data43
    decrypted data36

    0 error(s)
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    Input- Two prime numbers which is used to first encrypt and then data given:
    in above case, 7 and 13 were prime numbers, data was 36
    Output- the output shows the encrypted and decrypted data.

    but still it doent work for prime numbers : 7 and 5. data:65

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    Please copy the full contents of the command prompt console from when you execute the program and paste it here that shows the problem.

    What was the program's output for the data: 7, 13 and 36? What was wrong with the output from when I ran the program? See the program's output in post#2

    Why do you think the algorithm in the code works as you want it to?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    nothing wrong when you executed it. but the problem is, its working only for limited numbers.

    i think it should work because it is an standard algorithm developed by Rivest, Adelman. Google it.

    [output]C:\Users\Ra\Desktop>java RSA
    Enter 1st prime nos:
    7
    Enter 2nd prime nos:
    13
    Nos are prime
    e=5
    d=29
    Enter data to be encrypted
    65
    encrypted data39
    decrypted data36
    [/output]

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    Then what about this:
    not working when prime nos are 7 and 13.. and when data is 36.
    Post a console that shows a problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    That is working. My mistake. Sorry.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

    If it is not working, time for some debugging.
    Work through all the forumulas manually to verify that you understand them and that they give the right results.
    Write the results for each step on a piece of paper.
    Have the program do each step separately and print out the results of each step.
    Compare the two lists.
    When you get a difference you can't understand, post the text of the manual step that shows the formula and its results and the java source code and results from the program that is different.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    sandesh10 (April 27th, 2013)

Similar Threads

  1. Working with numbers when there's "space" in between them
    By ashl7 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 12th, 2013, 02:19 AM
  2. IF statement accumulataion not working properly - random numbers
    By StrugglerWithJava in forum Loops & Control Statements
    Replies: 1
    Last Post: April 4th, 2013, 07:48 AM
  3. Replies: 5
    Last Post: July 24th, 2012, 12:37 AM
  4. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM