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

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

1. ## 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
{
System.out.println("Enter 1st prime nos:");
System.out.println("Enter 2nd prime nos:");
//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 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. ## 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");
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)

3. ## 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. ## 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?

5. ## 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. ## Re: RSA algo not working. (algoritm included)... not working for some numbers..[TOMORROW EXAMS]

not working when prime nos are 7 and 13.. and when data is 36.
Post a console that shows a problem?

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

That is working. My mistake. Sorry.

8. ## 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.

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

sandesh10 (April 27th, 2013)