-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA
175 lines (162 loc) · 3.41 KB
/
RSA
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
public class RSA {
public static int bitlength = 256;
public static Random r = new Random();
public static BigInteger p = BigInteger.probablePrime(bitlength, r);
public static BigInteger q = BigInteger.probablePrime(bitlength, r);
public static BigInteger n = p.multiply(q);
public static BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
public static BigInteger d;
public static BigInteger e = BigInteger.probablePrime(bitlength / 2, r);
public static void main(String args[]) {
// While gcd(phi,e) > 1 and e < phi
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0) {
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
System.out.println("Public key : " + e);
System.out.println("Private key : " + d);
Scanner sc = new Scanner(System.in);
System.out.print("Enter plaintext :");
String plaintext = sc.nextLine();
sc.close();
System.out.println("\nPlaintext : " + plaintext);
byte encrypted[] = encrypt(plaintext.getBytes());
System.out.println("\nPlaintext in bytes : " + bytestoString(plaintext.getBytes()));
System.out.println("\nCiphertext in bytes : " + bytestoString(encrypted));
byte decrypted[] = decrypt(encrypted);
System.out.println("\nPlaintext after decrypting : " + new String(decrypted));
}
private static String bytestoString(byte[] encrypted) {
StringBuilder ciphertext = new StringBuilder();
for (byte b : encrypted) {
ciphertext.append(Byte.toString(b));
}
return ciphertext.toString();
}
private static byte[] encrypt(byte[] message) {
return (new BigInteger(message)).modPow(e, n).toByteArray();
}
private static byte[] decrypt(byte[] message) {
return (new BigInteger(message)).modPow(d, n).toByteArray();
}
}
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.*;
import java.math.*;
class RSA
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int p,q,n,z,d=0,e,i;
System.out.println("Enter the number to be encrypted and decrypted");
int msg=sc.nextInt();
double c;
BigInteger msgback;
System.out.println("Enter 1st prime number p");
p=sc.nextInt();
System.out.println("Enter 2nd prime number q");
q=sc.nextInt();
n=p*q;
z=(p-1)*(q-1);
System.out.println("the value of z = "+z);
for(e=2;e<z;e++)
{
if(gcd(e,z)==1) // e is for public key exponent
{
break;
}
}
System.out.println("the value of e = "+e);
for(i=0;i<=9;i++)
{
int x=1+(i*z);
if(x%e==0) //d is for private key exponent
{
d=x/e;
break;
}
}
System.out.println("the value of d = "+d);
c=(Math.pow(msg,e))%n;
System.out.println("Encrypted message is : -");
System.out.println(c);
//converting int value of n to BigInteger
BigInteger N = BigInteger.valueOf(n);
//converting float value of c to BigInteger
BigInteger C = BigDecimal.valueOf(c).toBigInteger();
msgback = (C.pow(d)).mod(N);
System.out.println("Derypted message is : -");
System.out.println(msgback);
}
static int gcd(int e, int z)
{
if(e==0)
return z;
else
return gcd(z%e,e);
}
}