-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJFortune.java
58 lines (57 loc) · 1.52 KB
/
JFortune.java
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
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import rsa.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.math.*;
public class JFortune{
//a Java Fortune client
/*James Buchanan && Kwandwo Eck
*/
int port;
RSA myRSA = new RSA();
InetAddress host;
public JFortune(int port, String host){
this.port = port;
try{
this.host = InetAddress.getByName(host);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
static void print(Object o){
System.out.print(o+"\n");
}
public String getFortune(){
try{
Socket s = new Socket(host,port);
Scanner socketIn = new Scanner(s.getInputStream());
PrintWriter socketOut = new PrintWriter(s.getOutputStream(),true);
Key myPubKey = myRSA.getPublicKey();
socketOut.println(myPubKey.getExponent());
print("Public exponent sent\n");
socketOut.println(myPubKey.getModulus());
print("Public modulus sent\n");
ArrayList<String> fortuneV = new ArrayList<String>();
while(!socketIn.hasNext()){
print("waiting\n");
}
int numOfBlocks = Integer.parseInt(socketIn.nextLine());
for(int i=0; i<numOfBlocks; i++){
fortuneV.add(socketIn.nextLine());
}
s.close();
return myRSA.decrypt(fortuneV);
}
catch(IOException e){
throw new RuntimeException(e);
}
}
public static void main(String[] ARGV){
JFortune jfort = new JFortune(Integer.parseInt(ARGV[0]),ARGV[1]);
print(jfort.getFortune());
}
}