-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
153 lines (140 loc) · 5.36 KB
/
Client.java
File metadata and controls
153 lines (140 loc) · 5.36 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
package AuctioningSystem;
/*
Code: Calculator client calculatorClient.java
Date: 10th October 2000
Simple client super class that is used to hold features that are common to both buyer and seller clients.
*/
import java.rmi.Remote;
import java.util.*;
import java.rmi.Naming; //Import the rmi naming - so you can lookup remote object
import java.rmi.RemoteException; //Import the RemoteException class so you can catch it
import java.net.MalformedURLException; //Import the MalformedURLException class so you can catch it
import java.rmi.NotBoundException; //Import the NotBoundException class so you can catch it
import java.util.Scanner;
import java.io.*;
import java.security.*;
public class Client {
protected Remote frontEndInstance;
public Client(){
try {
// Create the reference to the remote object through the remiregistry
frontEndInstance =
Naming.lookup("rmi://localhost/FrontEnd");
}
// Catch the exceptions that may occur - rubbish URL, Remote exception
// Not bound exception or the arithmetic exception that may occur in
// one of the methods creates an arithmetic error (e.g. divide by zero)
catch (Exception e) {
System.out.println(e);
}
}
public static int askForID(){
System.out.println("Please enter your userID: ");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
return id;
}
public static User authenticateUser(SellerInterface s, int uid){
byte[] sig = null;
User authedUser = null;
try{
byte[] randomnumber = s.authUser(uid);
User user = readUser(uid);
PrivateKey priv = user.getPrivateKey();
Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(priv);
dsa.update(randomnumber);
sig = dsa.sign();
boolean userAuthenticated = s.returnSignedNumber(sig, uid);
if(userAuthenticated){
authedUser = user;
System.out.println("---- USER VERIFIED ----- ");
System.out.println("Hey " + user.getName());
}
} catch(Exception e){
System.out.println(e.toString());
}
return authedUser;
}
public static User authenticateUser(BuyingInterface b, int uid){
byte[] sig = null;
User authedUser = null;
try{
byte[] randomnumber = b.authUser(uid);
User user = readUser(uid);
PrivateKey priv = user.getPrivateKey();
Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
dsa.initSign(priv);
dsa.update(randomnumber);
sig = dsa.sign();
boolean userAuthenticated = b.returnSignedNumber(sig, uid);
if(userAuthenticated){
authedUser = user;
System.out.println("---- USER VERIFIED ----- ");
System.out.println("Hey " + user.getName());
}
} catch(Exception e){
System.out.println(e.toString());
}
return authedUser;
}
public static User readUser(int uid){
User user = null;
try{
FileInputStream fis = new FileInputStream("user" + uid + ".ser");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println(fis);
user = (User)ois.readObject();
ois.close();
} catch(Exception e){
System.out.println("YO" + e.toString());
}
return user;
}
public static boolean authenticateServer(SellerInterface s, int uid){
boolean outcome = false;
Random rand = new Random();
byte[] nonse = new byte[10];
rand.nextBytes(nonse);
try{
byte[] returned = s.sign(nonse);
User user = readUser(uid);
PublicKey pub = user.getServerPublicKey();
Signature sig = Signature.getInstance("SHA1withDSA", "SUN"); //instance
sig.initVerify(pub);
sig.update(nonse);
outcome = sig.verify(returned);
} catch(Exception e) {
System.out.println(e.toString());
}
return outcome;
}
public static boolean authenticateServer(BuyingInterface b, int uid){
boolean outcome = false;
Random rand = new Random();
byte[] nonse = new byte[10];
rand.nextBytes(nonse);
try{
byte[] returned = b.sign(nonse);
User user = readUser(uid);
PublicKey pub = user.getServerPublicKey();
Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
sig.initVerify(pub);
sig.update(nonse);
outcome = sig.verify(returned);
} catch(Exception e) {
System.out.println(e.toString());
}
return outcome;
}
//Lists operations that the user has to choose from
public static int ask(String[] commands){
System.out.println("Please choose an operation from this list of options: ");
for (int i = 1; i <= commands.length; i++){
System.out.println("Press " + i + " to " + commands[i-1]);
}
Scanner scanner = new Scanner(System.in);
int command = scanner.nextInt();
return command;
}
}