-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_generation.java
More file actions
97 lines (73 loc) · 4.14 KB
/
Copy pathkey_generation.java
File metadata and controls
97 lines (73 loc) · 4.14 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
import java.io.*;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.math.BigInteger;
import java.util.Scanner;
public class key_generation{
public void saveToFile(String fileName, BigInteger modValue, BigInteger expValue) throws IOException {
System.out.println("Writing to File " + fileName);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
try{
out.writeObject(modValue);
out.writeObject(expValue);
} catch (Exception e){
try {
throw new IOException("OPERATION FAILED", e);
} catch (IOException ex) {
ex.printStackTrace();
}
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public key_generation() throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
/* This section is for the sender's public and private key */
SecureRandom random1 = new SecureRandom();
KeyPairGenerator generator1 = KeyPairGenerator.getInstance("RSA");
generator1.initialize(1024,random1);
KeyPair sendpair = generator1.generateKeyPair();
Key sendPubKey = sendpair.getPublic();
Key sendPrivKey = sendpair.getPrivate();
/* This section is for the receiver's public and private key */
SecureRandom random2 = new SecureRandom();
KeyPairGenerator generator2 = KeyPairGenerator.getInstance("RSA");
generator2.initialize(1024,random2);
KeyPair receivepair = generator2.generateKeyPair();
Key receivePubKey = receivepair.getPublic();
Key receivePrivKey = receivepair.getPrivate();
/* set factory to be used by public/private keys. */
KeyFactory factory = KeyFactory.getInstance("RSA");
/* create and save sender's public and private keys to respective files */
RSAPublicKeySpec sendPubKSpec = factory.getKeySpec(sendPubKey,RSAPublicKeySpec.class);
RSAPrivateKeySpec sendPrivKSpec = factory.getKeySpec(sendPrivKey,RSAPrivateKeySpec.class);
saveToFile("Keys/SenderPublicKey.key", sendPubKSpec.getModulus(), sendPubKSpec.getPublicExponent());
saveToFile("Keys/SenderPrivateKey.key", sendPrivKSpec.getModulus(), sendPrivKSpec.getPrivateExponent());
/*create and save recevier's public and private keys to respective files */
RSAPublicKeySpec recPubKSpec = factory.getKeySpec(receivePubKey,RSAPublicKeySpec.class);
RSAPrivateKeySpec recPrivKSpec = factory.getKeySpec(receivePrivKey,RSAPrivateKeySpec.class);
saveToFile("Keys/ReceiverPublicKey.key", recPubKSpec.getModulus(), recPubKSpec.getPublicExponent());
saveToFile("Keys/ReceiverPrivateKey.key", recPrivKSpec.getModulus(), recPrivKSpec.getPrivateExponent());
Scanner input = new Scanner(System.in);
System.out.println("Enter in a 16 character string: ");
String symmetricKey = input.nextLine();
createSymKey(symmetricKey);
}
private void createSymKey(String key) throws IOException {
BufferedOutputStream outFile = new BufferedOutputStream(new FileOutputStream("Keys/symmetric.key"));
byte[] keyBytes = key.getBytes("UTF-8");
outFile.write(keyBytes,0,keyBytes.length);
outFile.close();
}
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
new key_generation();
}
}
1,5 Top