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.

Code java:

/* 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;
}
}

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:
Code :

StringReader sr = new StringReader("7\n13\n36\n");
BufferedReader buff=new BufferedReader(sr); //new InputStreamReader(System.in));

The output was:
Quote:

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)

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

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?

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]

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

Then what about this:
Quote:

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

Post a console that shows a problem?

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

That is working. My mistake. Sorry.

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.