diff --git a/Communications.java b/Communications.java index edbbdcd..6a3fadd 100644 --- a/Communications.java +++ b/Communications.java @@ -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(); @@ -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();} } @@ -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 diff --git a/Contacts.java b/Contacts.java index 1698e50..4c9dfe7 100644 --- a/Contacts.java +++ b/Contacts.java @@ -7,8 +7,8 @@ public class Contacts { private ArrayList name; public Contacts() { - address=new ArrayList(); - name=new ArrayList(); + address=new ArrayList(); + name=new ArrayList(); } public void addContact(String name, String address) { //adds contacts to arraylist @@ -25,19 +25,29 @@ public ArrayList 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 contacts=new ArrayList(); - int i=0,j=0; - while(i=0 && ind=0 && ind encryptlist = new ArrayList(); - private Contacts contacts = new Contacts(); + private JPanel panelSouth, panelWest, panelEast, panelCenter, panelNorth; + private JButton send, addContact, removeContact;//, decrypt; + private JTextField input; + private JTextArea convo; + private JLabel labelmessage; + private JList JLcontacts; + private ArrayList encryptlist; + private Contacts contacts; private Communications comm; + private DefaultListModel tmplist; public Cryptogravisor() { - super ("ayylmao"); + //initialize lists + + encryptlist=new ArrayList(); + contacts=new Contacts(); //initialize GUI and stuff comm=new Communications(6000,6001,this); setVisible(true); + //initialize GUI + panelSouth = new JPanel(); + panelEast = new JPanel(); + panelWest = new JPanel(); + panelCenter = new JPanel(); + panelNorth = new JPanel(); + + + //create south panel + panelSouth.setLayout(new FlowLayout()); + labelmessage = new JLabel("Enter a message:"); + panelSouth.add(labelmessage); + input = new JTextField(20); + panelSouth.add(input); + send = new JButton("Send"); + panelSouth.add(send); + send.addActionListener(this); + //decrypt = new JButton ("Decrypt Message"); + //panelSouth.add(decrypt); + + //create center panel + panelCenter.setLayout(new FlowLayout()); + convo = new JTextArea("New Convo\n", 30, 35); + convo.setLineWrap(true); + convo.setWrapStyleWord(true); + panelCenter.add(convo); + //create west panel + panelWest.setLayout(new BorderLayout()); + addContact = new JButton ("Add Contact"); + panelWest.add(addContact, BorderLayout.NORTH); + addContact.addActionListener(this); + + tmplist=new DefaultListModel(); + JLcontacts = new JList(tmplist); + JLcontacts.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); + //contacts.addActionListener(this); + panelWest.add(JLcontacts, BorderLayout.CENTER); + + removeContact = new JButton ("Remove Contact"); + removeContact.addActionListener(this); + + panelWest.add(removeContact, BorderLayout.SOUTH); + + add(panelSouth, BorderLayout.SOUTH); + add(panelCenter, BorderLayout.CENTER); + add(panelWest, BorderLayout.WEST); + setTitle("Cryptography Messenger"); + setSize(655,500); + setLocationRelativeTo(null); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setVisible(true); + setResizable(false); } - public void actionPerformed() { + public void actionPerformed(ActionEvent e) { //responds to user input //delegates message sends, and user adds + if(e.getSource()==send){ + String newMessage = input.getText(); + try { + sendMSG(contacts.getAddress(JLcontacts.getSelectedIndex()),newMessage); + } + catch (Exception ex) { + System.out.println("No key existing yet!"); + } + + input.setText(""); + } + + else if(e.getSource()==addContact){ + //prompts user to input a new name and IP + String name = JOptionPane.showInputDialog(null, "Enter a name:"); + String IP = JOptionPane.showInputDialog(null, "Enter the IP:"); + addToContacts(name,IP); + } + + + else if(e.getSource()==removeContact){ + int ind = JLcontacts.getSelectedIndex(); + try { + tmplist.remove(ind); + contacts.removeContact(ind); + encryptlist.remove(ind); + JLcontacts.remove(ind); + } catch (Exception e1){ + e1.printStackTrace(); + } + } } - private void addToContacts(String name, String address) { - // Creates the big integer - BigInteger bigint = Encryption.calcMod(); - // adds the user entry to the contacts class - contacts.addContact(name, address); - encryptlist.add(new Encryption(bigint)); - // sends the key and mod to given address - comm.sendKeyAndMod(address, encryptlist.get(contacts.indexOfAddress(address)).DiffieHellmanComputeKey(), bigint); - } - private void updateContactList() { - //updates GUI, to the current contents of Contacts class + private void addToContacts(String n, String address) { + //checks if IP already in use + + if (!contacts.containsAddress(address)) { + byte[] bigint = Encryption.calcMod(); + + // adds the user entry to the contacts class + contacts.addContact(n, address); + encryptlist.add(new Encryption(bigint)); + // sends the key and mod to given address + int ind=contacts.indexOfAddress(address); + try { + System.out.println(bigint.length+" : "+encryptlist.get(ind).DiffieHellmanComputeKey().length); + comm.sendKeyAndMod(address, encryptlist.get(ind).DiffieHellmanComputeKey(), bigint); + tmplist.addElement(n+"@"+address); + } + catch (Exception e) { + System.out.println("ERROR"); + e.printStackTrace(); + //something went wrong connecting, remove user from list + encryptlist.remove(ind); + contacts.removeContact(ind); + } + } } + private void sendMSG(String address, String message) throws InvalidatedEncryptionException { //sends messaged based on the user's selected contact and their message in the textfield //encrypts for recipient, and sends to user with communications class // sends the encrypted message to the address - comm.send(address, encryptlist.get(encryptlist.indexOf(address)).encryptText(message)); + // Added a tiny thing so it won't send blank messages + if (message.length() != 0){ + convo.append("You:"+message+"\n"); + if (contacts.containsAddress(address)) { + String ret=encryptlist.get(contacts.indexOfAddress(address)).encryptText(message); + comm.send(address, ret); + convo.append("You:"+ret+"\n"); + } + } } public void handleMessage(String message, String address) throws InvalidatedEncryptionException { + + if (!contacts.containsAddress(address)) { + convo.append("UNKNOWN COMMUNICATOR: "+message); + } + + //called by server, and will be printed to GUI by this method - // Creates the variables to find the correct encryption address - int index = contacts.indexOfAddress(address); - // When message is received it will be encrypted prints out encrypted message for testing purposes - System.out.print(message); - // Decrypts the message and prints it - // Assumes all messages that are received are encrypted - System.out.print(encryptlist.get(index).decryptText(message)); + // Creates the variables to find the correct encryption address + else { + int index = contacts.indexOfAddress(address); + // When message is received it will be encrypted prints out encrypted message for testing purposes + + // Decrypts the message and prints it + // Assumes all messages that are received are encrypted + convo.append(contacts.getName(index)+" : "+message+"\n"); + convo.append(contacts.getName(index)+" : "+encryptlist.get(index).decryptText(message)+"\n"); + } } public void handleKey(byte[] info, String address) { //handles encryption info passed for public key crypt - - addToContacts("New Friend", address); + System.out.println("KEY INBOUND"); + if (contacts.containsAddress(address)) { + System.out.println("Key recieved"); + int ind=contacts.indexOfAddress(address); + encryptlist.get(ind).calcKey(info); + } } public void quit() { @@ -61,11 +191,24 @@ public void quit() { public void handleKeyAndMod(byte dat[], byte mod[], String address){ // Adds a new friend contact for the user to change the name of - addToContacts("New Friend", address); + System.out.println("rec mod"+address); + contacts.addContact("New Friend", address); encryptlist.add(new Encryption(mod)); // Calculates the key - encryptlist.get(contacts.indexOfAddress(address)).calcKey(dat); - comm.sendKey(address,encryptlist.get(contacts.indexOfAddress(address)).DiffieHellmanComputeKey()); + int ind=contacts.indexOfAddress(address); + encryptlist.get(ind).calcKey(dat); + try { + comm.sendKey(address,encryptlist.get(ind).DiffieHellmanComputeKey()); + tmplist.addElement("New Friend@"+address); + } + catch (Exception e) { + System.out.println("ERROR"); + e.printStackTrace(); + //something went wrong connecting, remove user from list + encryptlist.remove(ind); + contacts.removeContact(ind); + } + } public static void main(String args[]) { diff --git a/Encryption.java b/Encryption.java index 632a10e..27617a8 100644 --- a/Encryption.java +++ b/Encryption.java @@ -16,11 +16,11 @@ public class Encryption { - public Encryption(BigInteger mod) { + public Encryption(byte[] mod) { //initiates values SecureRandom r=new SecureRandom(); - secret=new BigInteger(128,10,r); - this.mod=mod; + secret=new BigInteger(127,10,r); + this.mod=new BigInteger(mod); this.base=BigInteger.valueOf(7); key=BigInteger.valueOf(-1); @@ -31,11 +31,26 @@ public Encryption(BigInteger mod) { int sel=(int)(Math.random()*6); return primes[sel]; }*/ - - public static BigInteger calcMod() { + public static byte[] ensureByteSize(byte [] n) { + //ensures proper 32 bit bytes before returning + if (n.length==32)return n; + else { + byte g[]=new byte[32]; + int i; + for (i=32-1;i>32-n.length;i--) { + g[i]=n[i]; + } + for (;i>0;i--) { + g[i]=0; + } + return g; + } + } + public static byte[] calcMod() { //generate a random modulus SecureRandom r=new SecureRandom(); - return new BigInteger(256,10,r); + BigInteger tmp=new BigInteger(255,10,r); + return ensureByteSize(tmp.toByteArray()); } public static String cipher(String text,String cipher) { //XOR encrypts @@ -49,17 +64,19 @@ public static String cipher(String text,String cipher) { public void changeBase(int b) { base=BigInteger.valueOf(b); } - public BigInteger DiffieHellmanComputeKey() { //will need bigInteger + public byte[] DiffieHellmanComputeKey() { //will need bigInteger //algorithm taken from http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange //UNTESTD! - return base.modPow(secret,mod); + return ensureByteSize(base.modPow(secret,mod).toByteArray()); //return base^secret %mod } - public void calcKey(BigInteger v) {//UNTESTED + public void calcKey(byte[] v) {//UNTESTED //computes key given input - key=v.modPow(secret,mod); + BigInteger tmp=new BigInteger(v); + key=tmp.modPow(secret,mod); + System.out.println("SCRET KEY IS"+key); } public String encryptText(String input) throws InvalidatedEncryptionException{ if (key.equals(-1)) {