-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
59 lines (48 loc) · 2.81 KB
/
Client.java
File metadata and controls
59 lines (48 loc) · 2.81 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
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.*;
public class Client {
private static ObjectOutputStream oos;
private static ObjectInputStream ois;
private static Key AESKey,DESKey;
private static PublicKey publicKeyServer;
private static PrivateKey clientPrivateKey;
public static void main(String[] args) throws Exception {
Socket writeSocket = new Socket("localhost",3110);
ServerSocket ss1= new ServerSocket(3111);
Socket readSocket = ss1.accept();
System.out.println("rs "+readSocket);
oos = new ObjectOutputStream(writeSocket.getOutputStream());
ois = new ObjectInputStream(readSocket.getInputStream());
System.out.println("\nStart generating RSA key");
KeyPairGenerator keyGenRSA = KeyPairGenerator.getInstance("RSA");
keyGenRSA.initialize(1024);
KeyPair keyRSA = keyGenRSA.generateKeyPair();
clientPrivateKey = keyRSA.getPrivate();
PublicKey clientPublicKey = keyRSA.getPublic();
System.out.println("Finish generating RSA key");
oos.writeObject(clientPublicKey);
oos.flush();
System.out.println("Public key (RSA)of client has been sent to server");
PublicKey serverPublicKey= (PublicKey) ois.readObject();
try {
AESkeyAndSignature aeSkeyAndSignature = (AESkeyAndSignature) ois.readObject();
//MessageDecryptionRSA decryptedAESkey = new MessageDecryptionRSA(aeSkeyAndSignature.getCipherKeyAES(), keyRSAPrivate);
DecryptRSAwithSignature decryptRSAwithSignature = new DecryptRSAwithSignature(aeSkeyAndSignature.getCipherKeyAES(), clientPrivateKey, serverPublicKey, aeSkeyAndSignature.getSignature(),"AES");
AESKey = decryptRSAwithSignature.getKey();
System.out.println("Common key(AES) from server received" + AESKey);
DESKeyAndSignature deSkeyAndSignature = (DESKeyAndSignature) ois.readObject();
//MessageDecryptionRSA decryptedAESkey = new MessageDecryptionRSA(aeSkeyAndSignature.getCipherKeyAES(), keyRSAPrivate);
DecryptRSAwithSignature decryptRSAwithSignature1 = new DecryptRSAwithSignature(deSkeyAndSignature.getCipherKeyDES(), clientPrivateKey, serverPublicKey, deSkeyAndSignature.getSignature(),"DES");
DESKey = decryptRSAwithSignature1.getKey();
System.out.println("Common key(DES) from server received" + DESKey);
} catch (Exception e) {
e.printStackTrace();
}
new Launcher(readSocket,writeSocket,AESKey,DESKey,ois,oos).setVisible(true);
System.out.println("Hello");
}
}