-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINS_P14.java
More file actions
148 lines (136 loc) · 3.35 KB
/
Copy pathINS_P14.java
File metadata and controls
148 lines (136 loc) · 3.35 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ins;
import java.math.BigInteger;
import java.util.*;
/**
*
* @author Raj Dhanani
*/
class Diffie{
numbers globalPublicData;
int x,y,k;
Diffie(int n, int x){
globalPublicData = new numbers(n);
this.x = x;
}
void generatePublicKey(){
BigInteger temp = new BigInteger(Integer.toString(globalPublicData.a));
temp = temp.pow(x).mod(new BigInteger(Integer.toString(globalPublicData.n)));
y = temp.intValue();
}
void generateKey(int publicKey){
BigInteger temp = new BigInteger(Integer.toString(publicKey));
temp = temp.pow(x).mod(new BigInteger(Integer.toString(globalPublicData.n)));
k = temp.intValue();
}
}
class numbers{
int n,a;
int GCD(int a,int b){
int min = (a<b)?a:b;
for(int c = min ; c > 1; c--){
if(a%c == 0 && b%c == 0){
return c;
}
}
return 1;
}
boolean isPrime(int x){
boolean flag = true;
for(int i = 2 ;i < x; i++){
if(x % i == 0){
flag = false;
}
}
return flag;
}
public numbers(int prime) {
try
{
if(isPrime(prime)){
n = prime;
a = findPrimitive(n);
}
else {
throw new Exception("SELECT PRIME NUMBER ONLY");
}
}
catch(Exception e){
e.printStackTrace();
}
}
int power(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0){
if (y % 2 == 1){
res = (res * x) % p;
}
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
int findPrimitive(int n)
{
ArrayList<Integer> s = new ArrayList<Integer>();
if (isPrime(n) == false){
return -1;
}
int phi = n - 1;
int phi1 = phi;
while (phi1 % 2 == 0){
s.add(2);
phi1 = phi1 / 2;
}
for (int i = 3; i <= Math.sqrt(phi1); i = i + 2){
while (phi1 % i == 0)
{
s.add(i);
phi1 = phi1 / i;
}
}
if (phi1 > 2)
{
s.add(phi1);
}
for (int r = 2; r <= phi; r++)
{
boolean flag = false;
for (Integer a : s)
{
if (power(r, phi / (a), n) == 1){
flag = true;
break;
}
}
if (flag == false){
return r;
}
}
return -1;
}
}
public class INS_P14 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("ENTER Q:");
int q = in.nextInt();
int xa,xb;
in.nextLine();
System.out.println("ENTER PRIVATE KEY FOR USER A");
xa = in.nextInt();
in.nextLine();
System.out.println("ENTER PRIVATE KEY FOR USER B");
xb = in.nextInt();
in.nextLine();
Diffie a = new Diffie(q, xa);
Diffie b = new Diffie(q, xb);
a.generatePublicKey();
b.generatePublicKey();
a.generateKey(b.y);
b.generateKey(a.y);
System.out.println("USER-A KEY:"+a.k);
System.out.println("USER-B KEY:"+b.k);
}
}