forked from ravipatel447/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.java
More file actions
61 lines (58 loc) · 1.96 KB
/
rsa.java
File metadata and controls
61 lines (58 loc) · 1.96 KB
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
import java.util.*;
public class rsa {
public static long gcd(long a,long b){
if(a==0) return b;
return gcd(b%a,a);
}
public static long[] keyGenerate(long p,long q){
long n=(p-1)*(q-1);
long e=Integer.MIN_VALUE;
ArrayList<Long> al =new ArrayList<Long>();
for(long key=2;key<=n;key++){
if(gcd(key,n)==1){
al.add(key);
}
}
System.out.println("All possible public key is");
for(int i=0;i<al.size();i++) System.out.println(al.get(i));
e=al.get(0);
long d=Integer.MIN_VALUE;
long index=1;
while (true){
if((index*e)<n){
index++;
}
else{
if((index*e)%n==1){
d=index;
break;
}
index++;
}
}
long[] key=new long[]{e,d};
return key;
}
public static double powerModeFunction(long[] key,double planeText){
double text = Math.pow(planeText,key[0])%key[1];
return text;
}
public static void main(String[] arr){
Scanner sc=new Scanner(System.in);
System.out.println("Enter message ");
double planeText=sc.nextDouble();
System.out.println("Enter message p");
long p=sc.nextLong();
System.out.println("Enter message q");
long q=sc.nextLong();
long[] keyArray=keyGenerate(p,q);
long[] publicKey=new long[]{keyArray[0],(p)*(q)};
long[] privateKey=new long[]{keyArray[1],(p)*(q)};
double encryptText = powerModeFunction(publicKey,planeText);
System.out.println("Private key is "+privateKey[0]);
System.out.println("Public key is "+publicKey[0]);
System.out.println("Encryption text is "+encryptText);
double decryptText=powerModeFunction(privateKey,encryptText);
System.out.println("Decryption text is "+decryptText);
}
}