Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions Communications.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void messageServer() { //fix
}
//call method in crypt
System.out.println("Fe"+message);
crypt.handleMessage(message,cn.getInetAddress().toString());
crypt.handleMessage(message,cn.getInetAddress().getHostAddress());
//close streams
dat.close();istream.close();cn.close();

Expand Down Expand Up @@ -81,35 +81,38 @@ private void keyServer() {
//PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
InputStream istream= cn.getInputStream();
DataInputStream dat=new DataInputStream(istream);
byte buf[]=new byte[16]; //arb value, fix if not
byte buf[]=new byte[32]; //arb value, fix if not
byte mod[]=new byte[32];
int ind=0;
try { //keep going until EOF
//scans input and records it in arrays
if (ind<16) {
buf[ind]=dat.readByte();
}
else {
mod[ind]=dat.readByte();
}
while (ind<64) {
if (ind<32) {
buf[ind]=dat.readByte();
}
else {
mod[ind-32]=dat.readByte();
}
ind++;
}
}
catch (EOFException e) {
System.out.println("end of file");//temp

}
//call method in crypt
if (ind<=16) {//if the user not the one initiating
crypt.handleKey(buf,cn.getInetAddress().toString());
if (ind<=32) {//if the user not the one initiating
crypt.handleKey(buf,cn.getInetAddress().getHostAddress());
}
else {
crypt.handleKeyAndMod(buf,mod,cn.getInetAddress().toString());
crypt.handleKeyAndMod(buf,mod,cn.getInetAddress().getHostAddress());
//close streams
dat.close();istream.close();cn.close();

}
}
keyServer.close();
} catch (Exception e) {e.printStackTrace();}
}catch (Exception e) {e.printStackTrace();}

}

Expand All @@ -123,30 +126,28 @@ public void send(String address, String message) {
}
catch(Exception e) {e.printStackTrace();}
}
public void sendKey(String address,byte[] key) {
public void sendKey(String address,byte[] key) throws Exception {
//send encryption key via seperate port for cryptography purposes
try {


Socket s = new Socket(address,secretport);
DataOutputStream out=new DataOutputStream(s.getOutputStream());
out.write(key,0,key.length);
s.close();
}
catch(Exception e) {e.printStackTrace();}


}
public void sendKeyAndMod(String address, byte key[],byte mod[]) {
public void sendKeyAndMod(String address, byte key[],byte mod[]) throws Exception{
//sends this if the user is the one starting the conversation
//shares modulus for purposes of encoding
try {


Socket s = new Socket(address,secretport);
DataOutputStream out=new DataOutputStream(s.getOutputStream());
out.write(key,0,key.length);
out.write(mod,0,mod.length);
s.close();
}
catch(Exception e) {e.printStackTrace();}
Socket s = new Socket(address,secretport);
DataOutputStream out=new DataOutputStream(s.getOutputStream());
out.write(key,0,key.length);
out.write(mod,0,mod.length);
s.close();

}
public void kill() {
//kill server
Expand Down
42 changes: 26 additions & 16 deletions Contacts.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Contacts {
private ArrayList<String> name;

public Contacts() {
address=new ArrayList<String>();
name=new ArrayList<String>();
address=new ArrayList<String>();
name=new ArrayList<String>();
}
public void addContact(String name, String address) {
//adds contacts to arraylist
Expand All @@ -25,19 +25,29 @@ public ArrayList<String> getContacts() {
//in order of name, then address
//e.g. [Bob,192.168.2.1,Charley,127.0.0.1,Cameron,192.222.3.1..etc]
ArrayList<String> contacts=new ArrayList<String>();
int i=0,j=0;
while(i<name.size()||j<address.size()){
if(i<name.size()){
int i=0;
for(;i<name.size();i++){

contacts.add(name.get(i));
i++;
}
if(i<address.size()){
contacts.add(address.get(j));
j++;
}

contacts.add(address.get(i));


}
return contacts;
}
public String getAddress(int ind) {
if (ind>=0 && ind<address.size()) {
return address.get(ind);
}
return null;
}
public String getName(int ind) {
if (ind>=0 && ind<name.size()) {
return name.get(ind);
}
return null;
}
public int indexOfName(String n){
//return index of a given name
return name.indexOf(n);
Expand All @@ -46,7 +56,7 @@ public int indexOfAddress(String a){
//return index of a given address
return address.indexOf(a);
}
public Boolean containsAddress(String address){
public boolean containsAddress(String address){
// If it contains a given address will return true;
if (this.address.size() != 0){
for (int i = 0; i < this.address.size(); i++){
Expand All @@ -56,10 +66,10 @@ public Boolean containsAddress(String address){
return false;
}
}
} else {
}
return false;
}

// will never reach this simply adding it to stop error
return null;

}
}
}
Loading