-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSAKeyGenerator.java
122 lines (99 loc) · 4.6 KB
/
RSAKeyGenerator.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
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
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSAKeyGenerator {
// TODO Auto-generated method stub
private static final String Certificate_PUBLIC_KEY_FILE = "CPublic.key";
private static final String Certificate_PRIVATE_KEY_FILE = "CPrivate.key";
private static final String Alice_PUBLIC_KEY_FILE = "AlicePublic.key";
private static final String Alice_PRIVATE_KEY_FILE = "AlicePrivate.key";
public static void main(String[] args){
try {
System.out.println("-------GENRATE PUBLIC and PRIVATE KEY-------------");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024); //1024 used for normal securities
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
System.out.println("Alice Public Key - " + publicKey);
System.out.println("Alice Private Key - " + privateKey);
System.out.println("Get Certificate Key pairs");
KeyPair cKeypair = keyPairGenerator.generateKeyPair();
PublicKey cpublicKey = cKeypair.getPublic();
PrivateKey cprivateKey = cKeypair.getPrivate();
System.out.println("Certificate Public Key - " + cpublicKey);
System.out.println("Certificate Private Key - " + cprivateKey);
//Pullingout parameters which makes up Key
System.out.println("\n------- PULLING OUT PARAMETERS WHICH MAKES KEYPAIR----------\n");
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec rsaPubKeySpec = keyFactory.getKeySpec(publicKey, RSAPublicKeySpec.class);
RSAPrivateKeySpec rsaPrivKeySpec = keyFactory.getKeySpec(privateKey, RSAPrivateKeySpec.class);
RSAPublicKeySpec rsaCPubKeySpec = keyFactory.getKeySpec(cpublicKey, RSAPublicKeySpec.class);
RSAPrivateKeySpec rsaCPrivKeySpec = keyFactory.getKeySpec(cprivateKey, RSAPrivateKeySpec.class);
System.out.println("PubKey Modulus : " + rsaPubKeySpec.getModulus());
System.out.println("PubKey Exponent : " + rsaPubKeySpec.getPublicExponent());
System.out.println("PrivKey Modulus : " + rsaPrivKeySpec.getModulus());
System.out.println("PrivKey Exponent : " + rsaPrivKeySpec.getPrivateExponent());
System.out.println("PubKey Modulus : " + rsaCPubKeySpec.getModulus());
System.out.println("PubKey Exponent : " + rsaCPubKeySpec.getPublicExponent());
System.out.println("PrivKey Modulus : " + rsaCPrivKeySpec.getModulus());
System.out.println("PrivKey Exponent : " + rsaCPrivKeySpec.getPrivateExponent());
//Share public key with other so they can encrypt data and decrypt thoses using private key(Don't share with Other)
System.out.println("\n--------SAVING PUBLIC KEY AND PRIVATE KEY TO FILES-------\n");
RSAKeyGenerator rsaObj = new RSAKeyGenerator();
rsaObj.saveKeys(Certificate_PUBLIC_KEY_FILE, rsaCPubKeySpec.getModulus(), rsaCPubKeySpec.getPublicExponent());
rsaObj.saveKeys(Certificate_PRIVATE_KEY_FILE, rsaCPrivKeySpec.getModulus(), rsaCPrivKeySpec.getPrivateExponent());
rsaObj.saveKeys(Alice_PUBLIC_KEY_FILE, rsaPubKeySpec.getModulus(), rsaPubKeySpec.getPublicExponent());
rsaObj.saveKeys(Alice_PRIVATE_KEY_FILE, rsaPrivKeySpec.getModulus(), rsaPrivKeySpec.getPrivateExponent());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (InvalidKeySpecException e) {
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}
}
/**
* Save Files
* @param fileName
* @param mod
* @param exp
* @throws IOException
*/
private void saveKeys(String fileName,BigInteger mod,BigInteger exp) throws IOException{
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
System.out.println("Generating "+fileName + "...");
fos = new FileOutputStream(fileName);
oos = new ObjectOutputStream(new BufferedOutputStream(fos));
oos.writeObject(mod);
oos.writeObject(exp);
System.out.println(fileName + " generated successfully");
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(oos != null){
oos.close();
if(fos != null){
fos.close();
}
}
}
}
}