-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.java
55 lines (46 loc) · 1.79 KB
/
RSA.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
import java.math.BigInteger;
import java.security.SecureRandom;
public class RSA {
private final static BigInteger one = new BigInteger("1");
private BigInteger privateKey;
private BigInteger publicKey;
private BigInteger modulus;
// Generate an N-bit (roughly) public and private key
RSA(int N) {
SecureRandom r = new SecureRandom();
BigInteger p = new BigInteger(N / 2, 100, r);
BigInteger q = new BigInteger(N / 2, 100, r);
modulus = p.multiply(q);
BigInteger m = (p.subtract(one)).multiply(q.subtract(one));
publicKey = new BigInteger("65537"); // common value in practice = 2^16 + 1
privateKey = publicKey.modInverse(m);
}
BigInteger encrypt(BigInteger message) {
return message.modPow(publicKey, modulus);
}
BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}
public String toString() {
String s = "";
s += "public = " + publicKey + "\n";
s += "private = " + privateKey + "\n";
s += "modulus = " + modulus;
return s;
}
public static void main(String[] args) {
RSA key = new RSA(Integer.parseInt(args[0]));
System.out.println(key);
// create message by converting string to integer
String message = "Hello World";
BigInteger plaintext = new BigInteger(message.getBytes());
System.out.println("Plaintext: " + message);
// encrypt the message
BigInteger ciphertext = key.encrypt(plaintext);
System.out.println("Ciphertext: " + ciphertext);
// decrypt the message
plaintext = key.decrypt(ciphertext);
String decrypted = new String(plaintext.toByteArray());
System.out.println("Decrypted: " + decrypted);
}
}