-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuctionImpl.java
More file actions
137 lines (120 loc) · 5.59 KB
/
Copy pathAuctionImpl.java
File metadata and controls
137 lines (120 loc) · 5.59 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
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.util.HashMap;
import javax.crypto.*;
public class AuctionImpl extends java.rmi.server.UnicastRemoteObject implements Auction {
private static final long serialVersionUID = 6192197272342675756L;
private HashMap<Integer, AuctionItem> items;
private int keyCounter = 0;
// Implementations must have an explicit constructor in order to declare the
// RemoteException exception
public AuctionImpl() throws java.rmi.RemoteException {
super();
items = new HashMap<>();
}
public SealedObject getSpec(int itemId, SealedObject clientId) throws java.rmi.RemoteException {
// Decrypt client id
System.out.println("Request from client " + aesDecrypt(clientId) + " for item " + itemId);
// Return requested auction item as SealedObject
return (aesEncrypt(items.get(itemId)));
}
public void placeBid(int itemId, float bid, ClientId bidder) throws RemoteException {
items.get(itemId).bid(bid, bidder);
}
public void closeListing(int itemId) throws RemoteException {
items.get(itemId).close();
}
public SealedObject getAll(SealedObject clientId) throws RemoteException {
System.out.println("Request from client " + aesDecrypt(clientId) + " for all listings");
return aesEncrypt(items);
}
public HashMap<Integer, AuctionItem> getUserBids(ClientId clientId) throws RemoteException {
System.out.println("Request from client " + clientId + " for their bids");
int id = clientId.getId();
HashMap<Integer, AuctionItem> userBidItems = new HashMap<Integer, AuctionItem>();
for (AuctionItem item : items.values()) {
if(item.getBidders().containsKey(id))
userBidItems.put(item.getId(), item);
}
return userBidItems;
}
public void addListing(String itemTitle, float price, float reserve, String itemDescription, String itemCondition, SealedObject seller)
throws java.rmi.RemoteException {
if(reserve > price) {
AuctionItem item = new AuctionItem(keyCounter, itemTitle, price, reserve, itemDescription, itemCondition, (ClientId)aesDecrypt(seller));
System.out.println("Request from client " + aesDecrypt(seller) + " to add: \n" + item);
keyCounter++;
items.put(item.getId(), item);
}
}
//---------------------------------------------------Encrpytion Methods----------------------------------------------------//
public Object aesDecrypt(SealedObject toDecrypt) {
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 toDecrypt.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;
}
// Takes any Serializable object to encrypt
public SealedObject aesEncrypt(Serializable toEncrypt) throws java.rmi.RemoteException {
try {
// Reads shared key from text file
SecretKey key = KeyManager.loadKey("keys.txt");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
// Returns encrypted object
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;
}
}