Ejemplo n.º 1
0
  public void initiateSeesion() {
    int public_key;

    String RecText;
    String SendText;
    String HashText = "";
    String SharedSecret = "secret";
    String Ciphers = "CFB CBC PCBC RC4";
    String Cipher_Chosen;
    String receivedHashValue;

    try {
      sockIn = socket.getInputStream();
      sockOut = socket.getOutputStream();
      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(sockIn));
      PrintWriter outToServer = new PrintWriter(sockOut, true);
      InputStreamReader in = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(in);

      // genrating alice random number
      Random R = new Random();
      RAlice = R.nextInt(100);

      // sending command 1
      SendText = Ciphers + ",,," + RAlice;
      outToServer.println(SendText);
      HashText = HashText + SendText;
      System.out.println("Ciphers, RAlice :" + SendText);

      // receiving command 2
      RecText = inFromServer.readLine();
      System.out.println("session id, my certificate, cipher, RBob: " + RecText);
      HashText = HashText + RecText;
      String Temp[] = RecText.split(",,,");
      Session_id = Integer.parseInt(Temp[0]);
      public_key = Integer.parseInt(Temp[1]);
      Cipher_Chosen = Temp[2];
      RBob = Integer.parseInt(Temp[3]);

      // sending command3
      // generating keys
      key1 = MD5hash(RAlice + RBob + SharedSecret + 1);
      key2 = MD5hash(RAlice + RBob + SharedSecret + 2);
      System.out.println("Generated key1 : " + new String(key1));
      System.out.println("Generated key2 : " + new String(key2));

      // calculating hash value
      String hashValue = new String(MD5hash(HashText));
      System.out.println(hashValue);
      // hashValue = hashValue.replaceAll("(\\r|\\n)", "");
      hashValue = encHashValue(Cipher_Chosen, hashValue);

      String encSharedSecret = getEncryptedSharedSecret(SharedSecret);
      SendText = encSharedSecret + ",,," + hashValue;
      outToServer.println(SendText);
      HashText = HashText + SendText;
      System.out.println("{S}bob, K{hash value} : " + SendText);

      // receiving command 4
      RecText = inFromServer.readLine();
      // RecText = RecText.replaceAll("(\\r|\\n)", "");
      System.out.println("keyed hash of msgs: " + RecText);
      receivedHashValue = RecText;

      receivedHashValue = decHashValue(Cipher_Chosen, receivedHashValue);
      // receivedHashValue = receivedHashValue.replaceAll("(\\r|\\n)", "");

      hashValue = new String(MD5hash(HashText));
      // hashValue = hashValue.replaceAll("(\\r|\\n)", "");

      //            System.out.println("Received -- " + receivedHashValue);
      //            System.out.println("Caluculated -- " + hashValue);
      boolean blnResult = Arrays.equals(hashValue.getBytes(), receivedHashValue.getBytes());
      //            System.out.println("Are two hash values equal ? : " + blnResult);
      System.out.println("Hash values are comapred");

      //            if (blnResult) {
      System.out.println("Handshake is successful");
      //            } else {
      //                System.out.println("Handshake is not successful");
      //            }

      // file transfering
      fileTransfer(Cipher_Chosen, sockIn, sockOut);

      // closing stream readers/writers
      System.out.println("closing streams and client socket...");
      inFromServer.close();
      outToServer.close();
      socket.close();
    } catch (Exception e) {
      System.out.println("Server might not be up and running....");
    }
  }
Ejemplo n.º 2
0
  public void resumeSession() {
    int public_key;

    String RecText;
    String SendText;
    String HashText = "";
    String SharedSecret = "secret";
    String Ciphers = "CFB CBC PCBC RC4";
    String Cipher_Chosen;
    String receivedHashValue;

    try {
      sockIn = socket.getInputStream();
      sockOut = socket.getOutputStream();
      BufferedReader inFromServer = new BufferedReader(new InputStreamReader(sockIn));
      PrintWriter outToServer = new PrintWriter(sockOut, true);
      InputStreamReader in = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(in);

      // genrating alice random number
      Random R = new Random();
      RAlice = R.nextInt(100);

      // sending command 1
      SendText = Session_id + ",,," + Ciphers + ",,," + RAlice;
      outToServer.println(SendText);
      HashText = HashText + SendText;
      System.out.println("Session-id, Ciphers, RAlice :" + SendText);

      // receiving command 2
      RecText = inFromServer.readLine();
      System.out.println("session id, cipher, RBob, keyed hash : " + RecText);
      byte[] calHashValue = MD5hash(HashText);
      HashText = HashText + RecText;
      String Temp[] = RecText.split(",,,");
      Session_id = Integer.parseInt(Temp[0]);
      Cipher_Chosen = Temp[1];
      RBob = Integer.parseInt(Temp[2]);
      receivedHashValue = Temp[3];
      //            receivedHashValue = receivedHashValue.replaceAll("(\\r|\\n)", "");

      // genreating keys
      key1 = MD5hash(RAlice + RBob + SharedSecret + 1);
      key2 = MD5hash(RAlice + RBob + SharedSecret + 2);
      System.out.println("Generated key1 : " + new String(key1));
      System.out.println("Generated key2 : " + new String(key2));

      receivedHashValue = decHashValue(Cipher_Chosen, receivedHashValue);
      boolean Result = Arrays.equals(receivedHashValue.getBytes(), calHashValue);
      // System.out.println("Hash value comaprison : " + Result);
      System.out.println("Hash values are comapred");

      // sending command3
      // calculating hash value
      String hashValue = new String(MD5hash(HashText));
      //            hashValue = hashValue.replaceAll("(\\r|\\n)", "");
      hashValue = encHashValue(Cipher_Chosen, hashValue);
      outToServer.println(hashValue);
      System.out.println("{Keyed hash of msgs} : " + hashValue);

      //            if (Result) {
      System.out.println("Handshake is successful");
      //            } else {
      //                System.out.println("Handshake is not successful");
      //            }

      // file transfering
      fileTransfer(Cipher_Chosen, sockIn, sockOut);

      // closing stream readers/writers
      System.out.println("closing streams and client socket...");
      inFromServer.close();
      outToServer.close();
      socket.close();

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