Beispiel #1
0
 private void go() {
   try {
     // Read as long as there is input
     byte[] buffer = new byte[ciphertextBlockSize];
     boolean disconnectMsgSent = false;
     while (connection != null && !disconnectMsgSent && input.read(buffer) != -1) {
       // Decipher the bytes read in
       byte[] plaintext = Ciphers.RSADecipherWSalt(buffer, decipherExp, modulus);
       if (plaintext.length == 1 && plaintext[0] == 0) {
         disconnectMsgSent = true;
         closeAll();
       } else {
         // convert to a string and display
         message = new String(plaintext);
         displayArea.append("\n" + message);
       }
     }
   } catch (IOException ioe) {
     // Server disconnected-we can reconnect if we wish
   }
 }
Beispiel #2
0
  public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    // Client pressed enter in the message entry field-send it
    if (source == enterField) {
      // Get the message
      message = e.getActionCommand();
      try {
        // Encipher the message
        if (message.length() > plaintextBlockSize)
          message = message.substring(0, plaintextBlockSize);
        byte[] ciphertext =
            Ciphers.RSAEncipherWSalt(message.getBytes(), BigIntegerMath.THREE, recipModulus, sr);
        // Send to the server
        output.write(ciphertext);
        output.flush();
        // Display same message in client output area
        displayArea.append("\n" + message);
        enterField.setText("");
      } catch (IOException ioe) {
        displayArea.append("\nError writing message");
      }
    } else if (source == connectButton) {
      if (connection != null) { // Already connected-button press now means disconnect
        try {
          // Send final message of 0
          byte[] lastMsg = new byte[1];
          lastMsg[0] = 0;
          output.write(Ciphers.RSAEncipherWSalt(lastMsg, BigIntegerMath.THREE, recipModulus, sr));
          output.flush();
          // close connection and IO streams, change some components
          closeAll();
        } catch (IOException ioe) {
          displayArea.append("\nError closing connection");
        }
      } else { // Not connected-connect
        // Get name of server to connect to
        chatServer = serverField.getText();
        displayArea.setText("Attempting connection to " + chatServer);
        try {
          // Set up the socket
          connection = new Socket(chatServer, 55555);

          displayArea.append("\nConnected to: " + connection.getInetAddress().getHostName());

          // Set up the IO streams
          output = new DataOutputStream(connection.getOutputStream());
          output.flush();
          input = new DataInputStream(connection.getInputStream());

          // Exchange public keys with the server-send yours, get theirs
          exchangeKeys();

          // Change appearance/functionality of some components
          serverField.setEditable(false);
          connectButton.setLabel("Disconnect from server above");
          enterField.setEnabled(true);
          // Set up a thread to listen for the connection
          listener =
              new Thread(
                  new Runnable() {
                    public void run() {
                      go();
                    }
                  });
          listener.start();
        } catch (IOException ioe) {
          displayArea.append("\nError connecting to " + chatServer);
        }
      }
    }
  }
  /**
   * Iterates through an array of ciphers and runs the same test case for every entry.
   *
   * @param ciphers - Array of cipher names.
   * @return - Number of tests failed.
   */
  protected int testSomeCiphers(Ciphers ciphers) {
    int failedNum = 0;
    String description = ciphers.description;
    System.out.println("===============================================");
    System.out.println(description + " ciphers testing");
    System.out.println("===========================================");
    for (String cs : ciphers.ciphers) {
      System.out.println("---------------------------------------");
      System.out.println("Testing cipher suite " + cs);
      System.out.println("---------------------------------------");
      Throwable error = null;

      // Reset global mutable static variables
      net = null;
      doUnwrapForNotHandshakingStatus = false;
      endHandshakeLoop = false;

      try {
        testOneCipher(cs);
      } catch (Throwable t) {
        error = t;
      }
      switch (ciphers) {
        case SUPPORTED_NON_KRB_CIPHERS:
        case SUPPORTED_NON_KRB_NON_SHA_CIPHERS:
        case SUPPORTED_KRB_CIPHERS:
        case ENABLED_NON_KRB_NOT_ANON_CIPHERS:
          if (error != null) {
            System.out.println("Test Failed: " + cs);
            System.err.println("Test Exception for " + cs);
            error.printStackTrace();
            failedNum++;
          } else {
            System.out.println("Test Passed: " + cs);
          }
          break;
        case UNSUPPORTED_CIPHERS:
          if (error == null) {
            System.out.println("Test Failed: " + cs);
            System.err.println(
                "Test for "
                    + cs
                    + " should have thrown "
                    + "IllegalArgumentException, but it has not!");
            failedNum++;
          } else if (!(error instanceof IllegalArgumentException)) {
            System.out.println("Test Failed: " + cs);
            System.err.println("Test Exception for " + cs);
            error.printStackTrace();
            failedNum++;
          } else {
            System.out.println("Test Passed: " + cs);
          }
          break;
        default:
          throw new Error("Test issue: unexpected ciphers: " + ciphers.name());
      }
    }

    return failedNum;
  }