-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment7.java
63 lines (60 loc) · 1.4 KB
/
Assignment7.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.io.*; import java.math.*;
class Assignmnet7
{
public static void main(String args[])throws IOException
{
int q,p,n,pn,publickey=0,d=0,msg; double cipher,ptext;
int check,check1;
DataInputStream in=new DataInputStream(System.in);
System.out.println("*******************************");
System.out.println("Assignment No : 7");
System.out.println("*******************************");
System.out.println("ENTER NO"); p=Integer.parseInt(in.readLine()); q=Integer.parseInt(in.readLine());
check=prime(p); check1=prime(q); if(check!=1||check1!=1)
{
System.exit(0);
}
n=p*q;
pn=(p-1)*(q-1); for(int e=2;e<pn;e++)
{
if(gcd(e,pn)==1)
{
publickey=e; System.out.println("PUBLIC KEY :"+e); break;
}
}
for(int i=0;i<pn;i++)
{
d=i;
if(((d*publickey)%pn)==1)
break;
}
System.out.println("PRIVATE KEY :"+d);
System.out.println("ENTER MESSAGE ");
msg=Integer.parseInt(in.readLine());
cipher=Math.pow(msg,publickey);
cipher=cipher%n;
System.out.println("ENCRYPTED :"+cipher);
ptext=Math.pow(cipher,d);
ptext=ptext%n; System.out.println("DECRYPTED :"+ptext);
}
static int prime(int a)
{
int flag=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
{
System.out.println(a+" is not a Prime Number"); flag = 1;
return 0;
}
}
if(flag==0) return 1;
return 1;
}
static int gcd(int number1, int number2)
{
if(number2 == 0){ return number1;
}
return gcd(number2, number1%number2);
}
}