Skip to content

Commit af2e3ad

Browse files
author
tickleberrie
committed
extra credit
1 parent f233254 commit af2e3ad

File tree

10 files changed

+671
-0
lines changed

10 files changed

+671
-0
lines changed

ExtraCredit/AES.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import javax.crypto.SecretKeyFactory;
2+
import javax.crypto.SecretKey;
3+
import javax.crypto.SealedObject;
4+
import javax.crypto.Cipher;
5+
import javax.crypto.spec.PBEKeySpec;
6+
import javax.crypto.spec.PBEParameterSpec;
7+
import java.security.Security;
8+
import javax.crypto.spec.IvParameterSpec;
9+
import javax.crypto.spec.SecretKeySpec;
10+
import java.io.*;
11+
import java.math.BigInteger;
12+
13+
public class AES
14+
{
15+
byte[] iv = new byte[]
16+
{
17+
0x00, 0x01, 0x02, 0x03,
18+
0x04, 0x05, 0x06, 0x07,
19+
0x08, 0x09,0x0a, 0x0b,
20+
0x0c, 0x0d, 0x0e, 0x0f
21+
};
22+
23+
private byte[] salt = "WENTWORTH_INSTITUTE".getBytes();
24+
private Cipher cipher_encr;
25+
private Cipher cipher_decr;
26+
27+
public AES(String passphrase){
28+
try{
29+
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
30+
PBEKeySpec pbeKeySpec = new PBEKeySpec(passphrase.toCharArray(), salt, 65536, 256);
31+
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
32+
SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
33+
34+
cipher_encr = Cipher.getInstance("AES/CBC/PKCS5Padding");
35+
cipher_encr.init(Cipher.ENCRYPT_MODE, secret , new IvParameterSpec(iv));
36+
cipher_decr = Cipher.getInstance("AES/CBC/PKCS5Padding");
37+
cipher_decr.init(Cipher.DECRYPT_MODE, secret , new IvParameterSpec(iv));
38+
39+
}catch(Exception e){
40+
System.out.println("Error!" + e);
41+
System.exit(-1);
42+
}
43+
}
44+
45+
46+
/** AES encrypton
47+
* @param data Object to Encrypt
48+
* @return an encrypted Object
49+
*/
50+
public SealedObject encrypt(Serializable data)
51+
throws Exception{
52+
return new SealedObject(data, cipher_encr);
53+
}
54+
55+
/**
56+
* - AES decrypton
57+
* @param sealedObj Encrypted Object
58+
* @return an Serializable Object
59+
*/
60+
public Serializable decrypt(SealedObject sealedObj)
61+
throws Exception{
62+
return (Serializable)sealedObj.getObject(cipher_decr);
63+
}
64+
65+
66+
}

ExtraCredit/Client.java

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import java.lang.*;
2+
import java.io.*;
3+
import java.net.*;
4+
import java.util.Scanner;
5+
6+
import javax.crypto.*;
7+
import javax.crypto.spec.*;
8+
import java.math.BigInteger;
9+
import java.security.MessageDigest;
10+
import java.nio.charset.StandardCharsets;
11+
12+
class Client {
13+
public static void main(String args[]) {
14+
try {
15+
Socket s = new Socket("localhost", 1234);
16+
System.out.println("Local Port: " + s.getLocalPort());
17+
System.out.println("Server Port: " + s.getPort());
18+
19+
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
20+
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
21+
22+
BigInteger q = (BigInteger) ois.readObject();
23+
System.out.println("Client: Recived modulus (q). ");
24+
25+
BigInteger a = (BigInteger) ois.readObject();
26+
System.out.println("Client: Recived prime root (a). ");
27+
28+
System.out.println("Client: Computing public key (Ya)...");
29+
DiffieHellman dh = new DiffieHellman();
30+
BigInteger y = dh.computePublicKey(q,a);
31+
32+
oos.writeObject(y);
33+
System.out.println("Client: Sending my Ya...");
34+
35+
System.out.println("Client: Waiting for Server's public key (Yb)...");
36+
BigInteger yb = (BigInteger)ois.readObject();
37+
System.out.println("Client: Recieved Yb.");
38+
39+
BigInteger mkey = dh.computeMasterKey(yb,q);
40+
41+
System.out.println("Client: Creating symmetric Key...");
42+
43+
String keyHash = DiffieHellman.createHashString(mkey);
44+
System.out.println("Client: Symmetric Key Created. \n");
45+
System.out.println("Hash:" + keyHash);
46+
47+
System.out.println("--------- Secured Communication Initialized -----------");
48+
Scanner scanner = new Scanner(System.in);
49+
//AES aes = new AES(keyHash);
50+
51+
MListener ml = new MListener(ois, oos, keyHash);
52+
ml.setDaemon(false);
53+
ml.start();
54+
55+
while (scanner.hasNext()){
56+
String message = scanner.nextLine();
57+
INFO info = new INFO("Rixing", message);
58+
// SealedObject sb = aes.encrypt(info);s
59+
// oos.writeObject(sb);
60+
ml.sendMessage(info);
61+
}
62+
63+
// /*
64+
// * (1) RECEIVE a BigInteger
65+
// */
66+
// BigInteger bi = (BigInteger) ois.readObject();
67+
// System.out.println("Server sent to me: " + bi);
68+
//
69+
// /*
70+
// * (2) SEND a BigInteger
71+
// */
72+
// oos.writeObject(bi.add(BigInteger.ONE));
73+
//
74+
//
75+
// /*
76+
// * (3) RECEIVE a custom object, which is Serializable
77+
// */
78+
// INFO info = (INFO) ois.readObject();
79+
// System.out.println("INFO object sent by server: " );
80+
// info.printInfo();
81+
// for(int i=0; i < info.getArray().length; i++) {
82+
// System.out.println("\t["+ i + "] " + info.getArray()[i]);
83+
// }
84+
//
85+
//
86+
// /*
87+
// * (4) RECEIVE a String
88+
// */
89+
// System.out.println("Server says: " + (String) ois.readObject());
90+
//
91+
//
92+
//
93+
// System.out.println("--------- ENCRYPTED SEND/RECEIVE -----------");
94+
//
95+
//
96+
//
97+
//
98+
// /*
99+
// * Create Key
100+
// */
101+
// String password = "12345678";
102+
// byte key[] = password.getBytes();
103+
// DESKeySpec desKeySpec = new DESKeySpec(key);
104+
// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
105+
// SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
106+
//
107+
//
108+
// /*
109+
// * Create Cipher
110+
// */
111+
// Cipher desCipher_encr = Cipher.getInstance("DES/ECB/PKCS5Padding");
112+
// Cipher desCipher_decr = Cipher.getInstance("DES/ECB/PKCS5Padding");
113+
//
114+
//
115+
// /*
116+
// * Initialize Cipher
117+
// */
118+
// desCipher_encr.init(Cipher.ENCRYPT_MODE, secretKey);
119+
// desCipher_decr.init(Cipher.DECRYPT_MODE, secretKey);
120+
//
121+
//
122+
//
123+
// /*
124+
// * RECEIVE a String (encrypted)
125+
// */
126+
// SealedObject sobj = (SealedObject) ois.readObject();
127+
// String ss = (String) sobj.getObject(desCipher_decr);
128+
// System.out.println("Server sent String: " + ss);
129+
//
130+
// System.out.println("--------- Secured Communication Initialized -----------");
131+
132+
// /*
133+
// * SEND a custom object (encrypted)
134+
// */
135+
// INFO info_en = new INFO("CLIENT", new BigInteger("999999999"));
136+
// SealedObject info_sealed_obj = new SealedObject(info_en, desCipher_encr);
137+
// oos.writeObject(info_sealed_obj);
138+
//
139+
// /*
140+
// * RECEIVE IMAGE
141+
// */
142+
// SealedObject si = (SealedObject) ois.readObject();
143+
// System.out.println("(1)");
144+
// IMAGE_OBJ iobj = (IMAGE_OBJ) si.getObject(desCipher_decr);
145+
// System.out.println("(2) ");
146+
// iobj.SaveImage("./IMAGE_RECEIVED.png");
147+
// System.out.println("(3)");
148+
149+
// close streams
150+
oos.close();
151+
ois.close();
152+
153+
/*
154+
* Close connection
155+
*/
156+
s.close();
157+
}
158+
catch(Exception e) {
159+
System.err.print("[ERROR] ::" + e);
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)