-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuctionClient.java
More file actions
160 lines (139 loc) · 6.44 KB
/
Copy pathAuctionClient.java
File metadata and controls
160 lines (139 loc) · 6.44 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
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
import javax.crypto.*;
import java.rmi.Naming;
import java.util.HashMap;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class AuctionClient {
private ClientGUI gui;
private ClientId id;
private Auction auction;
public static void main(String[] args) {
new AuctionClient(1);
new AuctionClient(2);
}
public AuctionClient(int n) {
id = new ClientId(n);
gui = new ClientGUI(this);
try {
// Creates reference to the remote object through the remiregistry
auction = (Auction) Naming.lookup("rmi://localhost/AuctionService");
remoteAddListing("Apple iPhone 11", 399.99f, 499.99f,
"Black iPhone 11, comes with 64GB of storage and includes charging adapter.", "New", aesEncrypt(id));
remoteAddListing("Toshiba Microwave", 63.50f, 80.40f,
"Adjustable power levels and time. Low-Noise operation (55db).", "Used", aesEncrypt(id));
remoteAddListing("Office Chair", 59.99f, 69.99f, "Height adjustable swivel desk chair, black.", "New", aesEncrypt(id));
}
// Catch the exceptions that may occur
catch (MalformedURLException murle) {
System.out.println("\nMalformedURLException");
murle.printStackTrace();
} catch (RemoteException re) {
System.out.println("\nRemoteException");
re.printStackTrace();
} catch (NotBoundException nbe) {
System.out.println("\nNotBoundException");
nbe.printStackTrace();
} catch (java.lang.ArithmeticException ae) {
System.out.println("\njava.lang.ArithmeticException");
ae.printStackTrace();
} catch (InvalidKeySpecException ikse) {
System.out.println("\nInvalidKeySpecException");
ikse.printStackTrace();
}
}
public ClientId getId() {
return id;
}
//--------------------------------------------------------Remote Methods-----------------------------------------------------------------
public AuctionItem remoteGetSpec(int itemID) throws RemoteException, InvalidKeySpecException {
// Retrieve encrypted item from server
SealedObject item = auction.getSpec(itemID, aesEncrypt(id));
// Decrypt and return item
return (AuctionItem) aesDecrypt(item);
}
public HashMap<Integer, AuctionItem> remoteGetAll() throws RemoteException, InvalidKeySpecException {
return (HashMap<Integer, AuctionItem>) aesDecrypt(auction.getAll(aesEncrypt(id)));
}
public HashMap<Integer, AuctionItem> remoteGetUserBids() throws RemoteException, InvalidKeySpecException {
return (HashMap<Integer, AuctionItem>) auction.getUserBids(id);
}
public void remoteAddListing(String itemTitle, float price, float reserve, String itemDescription, String itemCondition, SealedObject seller)
throws RemoteException {
auction.addListing(itemTitle, price, reserve, itemDescription, itemCondition, seller);
}
public void remotePlaceBid(int itemId, float bid) throws RemoteException {
auction.placeBid(itemId, bid, id);
}
public void remoteCloseListing(int itemId) throws RemoteException {
auction.closeListing(itemId);
}
// -----------------------------------------------------Encryption Methods----------------------------------------------------------------
public Object aesDecrypt(SealedObject sealedItem) {
try {
// Reads shared key from text file
SecretKey key = KeyManager.loadKey("keys.txt");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
// Returns decrypted object
return sealedItem.getObject(cipher);
} catch (InvalidKeyException e) {
System.out.println("\nInvalidKeyException");
e.printStackTrace();
} catch (NoSuchPaddingException e) {
System.out.println("\nNoSuchPaddingException");
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
System.out.println("\nIllegalBlockSizeException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("\nIOException");
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
System.out.println("\nNoSuchAlogrithmException");
e.printStackTrace();
} catch (ClassNotFoundException e) {
System.out.println("\nClassNotFoundException");
e.printStackTrace();
} catch (BadPaddingException e) {
System.out.println("\nBadPaddingException");
e.printStackTrace();
} catch (InvalidKeySpecException e) {
System.out.println("\nInvalidKeySpecException");
e.printStackTrace();
}
return null;
}
public SealedObject aesEncrypt(Serializable toEncrypt) throws java.rmi.RemoteException, InvalidKeySpecException {
try {
SecretKey key = KeyManager.loadKey("keys.txt");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
return new SealedObject(toEncrypt, cipher);
} catch (InvalidKeyException e) {
System.out.println("\nInvalidKeyException");
e.printStackTrace();
} catch (NoSuchPaddingException e) {
System.out.println("\nNoSuchPaddingException");
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
System.out.println("\nIllegalBlockSizeException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("\nIOException");
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
System.out.println("\nNoSuchAlgorithmException");
e.printStackTrace();
} catch (InvalidKeySpecException e) {
System.out.println("\nInvalidKeySpecException");
e.printStackTrace();
}
return null;
}
}