/**
   * Attempts to log a client in using the authentication server. Authentication server needs to run
   * on the same host as the file server.
   *
   * @throws IOException Error reading from socket.
   */
  private void login() throws IOException {
    // set up required variables
    DatagramSocket clientSocket = new DatagramSocket();
    InetAddress authServerIP =
        InetAddress
            .getLocalHost(); // because authentication server runs on same host as file server
    byte[] dataToSend;
    byte[] receivedData = new byte[BUFFER_SIZE];

    // get username and password
    String userName = inFromClient.readLine().trim(); // get username
    String password = inFromClient.readLine().trim(); // get password
    dataToSend = new String(userName + " " + password).getBytes();

    // send the username and password for processing by authentication server
    DatagramPacket packetToSend =
        new DatagramPacket(dataToSend, dataToSend.length, authServerIP, AUTHENTICATION_PORT);
    clientSocket.send(packetToSend);

    // receive the response from the authentication server
    DatagramPacket receivedPacket = new DatagramPacket(receivedData, receivedData.length);
    clientSocket.receive(receivedPacket);
    String receivedString = new String(receivedPacket.getData()).trim();
    receivedData = receivedString.getBytes();
    if (receivedString.equals("yes")) {
      outToClient.writeBytes(receivedString); // successful login
    } else {
      outToClient.writeBytes("no"); // unsuccessful login
    }
  }
Exemple #2
0
 public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
   DataOutputStream output = new DataOutputStream(new BufferedOutputStream(System.out));
   LRC lrc = new LRC(output);
   DataInputStream input = new DataInputStream(new BufferedInputStream(System.in));
   lrc.writeStream(input);
   output.flush();
 }
Exemple #3
0
 private void flushRaw() throws IOException {
   if (currentRaw != null && currentRaw.size() > 0) {
     if (mode == Mode.Block) {
       output.writeInt(-1);
     }
     System.err.println("Writing raw chunk : " + currentRaw.size());
     rawChunks += currentRaw.size();
     output.writeInt(currentRaw.size());
     currentRaw.writeTo(output);
     mode = Mode.Raw;
   }
   currentRaw = null;
 }
Exemple #4
0
 public void run() // loop utama, utk nerima user input n kirim ke server&client laen
     {
   while (thread != null) {
     try {
       String a = console.readLine();
       String b = cryptor.encrypt(a);
       streamOut.writeUTF(cryptor.encrypt(a));
       streamOut.flush();
     } catch (IOException ioe) {
       System.out.println("Sending error: " + ioe.getMessage());
       stop();
     }
   }
 }
 /** Sends the user a list of the files contained in the same folder as the server. */
 private void listFiles() throws IOException {
   File folder = new File(".");
   File[] listOfFiles = folder.listFiles();
   for (File f : listOfFiles) {
     outToClient.writeBytes(f.getName() + '\n');
   }
 }
Exemple #6
0
  /**
   * Compute the hash an IP address. The hash is the first 8 bytes of the SHA digest of the IP
   * address.
   */
  private static byte[] computeAddressHash() {

    /*
     * Get the local host's IP address.
     */
    byte[] addr =
        (byte[])
            java.security.AccessController.doPrivileged(
                new PrivilegedAction() {
                  public Object run() {
                    try {
                      return InetAddress.getLocalHost().getAddress();
                    } catch (Exception e) {
                    }
                    return new byte[] {0, 0, 0, 0};
                  }
                });

    byte[] addrHash;
    final int ADDR_HASH_LENGTH = 8;

    try {
      /*
       * Calculate message digest of IP address using SHA.
       */
      MessageDigest md = MessageDigest.getInstance("SHA");
      ByteArrayOutputStream sink = new ByteArrayOutputStream(64);
      DataOutputStream out = new DataOutputStream(new DigestOutputStream(sink, md));
      out.write(addr, 0, addr.length);
      out.flush();

      byte digest[] = md.digest();
      int hashlength = Math.min(ADDR_HASH_LENGTH, digest.length);
      addrHash = new byte[hashlength];
      System.arraycopy(digest, 0, addrHash, 0, hashlength);

    } catch (IOException ignore) {
      /* can't happen, but be deterministic anyway. */
      addrHash = new byte[0];
    } catch (NoSuchAlgorithmException complain) {
      throw new InternalError(complain.toString());
    }
    return addrHash;
  }
Exemple #7
0
 private void exchangeKeys() {
   try {
     output.write(modulus.toByteArray());
     byte[] buffer = new byte[ciphertextBlockSize];
     input.read(buffer);
     recipModulus = new BigInteger(1, buffer);
   } catch (IOException ioe) {
     System.err.println("Error establishing keys");
   }
 }
Exemple #8
0
 // Close socket and IO streams, change appearance/functionality of some components
 private void closeAll() throws IOException {
   displayArea.append("\nConnection closing");
   output.close();
   input.close();
   connection.close();
   // We are no longer connected
   connection = null;
   // Change components
   serverField.setEditable(true);
   connectButton.setLabel("Connect to server above");
   enterField.setEnabled(false);
 }
Exemple #9
0
 public void stop() {
   if (thread != null) {
     thread.stop();
     thread = null;
   }
   try {
     if (console != null) console.close();
     if (streamOut != null) streamOut.close();
     if (socket != null) socket.close();
   } catch (IOException ioe) {
     System.out.println("Error closing ...");
   }
   client.close();
   client.stop();
 }
 /** Generates a device- and invocation-specific seed to be mixed into the Linux PRNG. */
 private static byte[] generateSeed() {
   try {
     ByteArrayOutputStream seedBuffer = new ByteArrayOutputStream();
     DataOutputStream seedBufferOut = new DataOutputStream(seedBuffer);
     seedBufferOut.writeLong(System.currentTimeMillis());
     seedBufferOut.writeLong(System.nanoTime());
     seedBufferOut.writeInt(Process.myPid());
     seedBufferOut.writeInt(Process.myUid());
     seedBufferOut.write(BUILD_FINGERPRINT_AND_DEVICE_SERIAL);
     seedBufferOut.close();
     return seedBuffer.toByteArray();
   } catch (IOException e) {
     throw new SecurityException("Failed to generate seed", e);
   }
 }
  public static void main(String[] args) throws Exception {
    // prompt user to enter a port number

    System.out.print("Enter the port number: ");
    Scanner scan = new Scanner(System.in);
    int port = scan.nextInt();
    scan.nextLine();
    System.out.print("Enter the host name: ");
    String hostName = scan.nextLine();

    // Initialize a key pair generator with the SKIP parameters we sepcified, and genrating a pair
    // This will take a while: 5...15 seconrds

    System.out.println("Generating a Diffie-Hellman keypair: ");
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH");
    kpg.initialize(PARAMETER_SPEC);
    KeyPair keyPair = kpg.genKeyPair();
    System.out.println("key pair has been made...");

    // one the key pair has been generated, we want to listen on
    // a given port for a connection to come in
    // once we get a connection, we will get two streams, One for input
    // and one for output
    // open a port and wait for a connection

    ServerSocket ss = new ServerSocket(port);
    System.out.println("Listeining on port " + port + " ...");
    Socket socket = ss.accept();

    // use to output and input primitive data type

    DataOutputStream out = new DataOutputStream(socket.getOutputStream());

    // next thing to do is send our public key and receive client's
    // this corresponds to server step 3 and step 4 in the diagram

    System.out.println("Sending my public key...");
    byte[] keyBytes = keyPair.getPublic().getEncoded();
    out.writeInt(keyBytes.length);
    out.write(keyBytes);
    System.out.println("Server public key bytes: " + CryptoUtils.toHex(keyBytes));

    // receive the client's public key

    System.out.println("Receiving client's public key...");
    DataInputStream in = new DataInputStream(socket.getInputStream());
    keyBytes = new byte[in.readInt()];
    in.readFully(keyBytes);

    // create client's public key

    KeyFactory kf = KeyFactory.getInstance("DH");
    X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(keyBytes);
    PublicKey clientPublicKey = kf.generatePublic(x509Spec);

    // print out client's public key bytes

    System.out.println(
        "Client public key bytes: " + CryptoUtils.toHex(clientPublicKey.getEncoded()));

    // we can now use the client's public key and
    // our own private key to perform the key agreement

    System.out.println("Performing the key agreement ... ");
    KeyAgreement ka = KeyAgreement.getInstance("DH");
    ka.init(keyPair.getPrivate());
    ka.doPhase(clientPublicKey, true);

    // in a chat application, each character is sendt over the wire, separetly encrypted,
    // Instead of using ECB, we are goin to use CFB, with a block size of 8 bits(1byte)
    // to send each character. We will encrypt the same character in a different way
    // each time. But in order to use CFB8, we need an IVof 8 bytes. We will create
    // that IV randomly and and send it to the client. It doesn't matter if somoene
    // eavesdrops on the IV when it is sent over the wire. it's not sensitive info

    // creating the IV and sending it corresponds to step 6 and 7

    byte[] iv = new byte[8];
    SecureRandom sr = new SecureRandom();
    sr.nextBytes(iv);
    out.write(iv);

    // we generate the secret byte array we share with the client and use it
    // to create the session key (Step 8)

    byte[] sessionKeyBytes = ka.generateSecret();

    // create the session key

    SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
    DESedeKeySpec DESedeSpec = new DESedeKeySpec(sessionKeyBytes);
    SecretKey sessionKey = skf.generateSecret(DESedeSpec);

    // printout session key bytes

    System.out.println("Session key bytes: " + CryptoUtils.toHex(sessionKey.getEncoded()));

    // now use tha that session key and IV to create a CipherInputStream. We will use them to read
    // all character
    // that are sent to us by the client

    System.out.println("Creating the cipher stream ...");
    Cipher decrypter = Cipher.getInstance("DESede/CFB8/NoPadding");
    IvParameterSpec spec = new IvParameterSpec(iv);
    decrypter.init(Cipher.DECRYPT_MODE, sessionKey, spec);
    CipherInputStream cipherIn = new CipherInputStream(socket.getInputStream(), decrypter);

    // we just keep reading the input and print int to the screen, until -1 sent over

    int theCharacter = 0;
    theCharacter = cipherIn.read();
    while (theCharacter != -1) {
      System.out.print((char) theCharacter);
      theCharacter = cipherIn.read();
    }
    // once -1 is received we want to close up our stream and exit

    cipherIn.close();
    in.close();
    out.close();
    socket.close();
  }
Exemple #12
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);
        }
      }
    }
  }
Exemple #13
0
  public static void main(String[] args) {
    try {
      if (args[0].equals("-genkey")) {
        KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
        SecureRandom random = new SecureRandom();
        pairgen.initialize(KEYSIZE, random);
        KeyPair keyPair = pairgen.generateKeyPair();
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1]));
        out.writeObject(keyPair.getPublic());
        out.close();
        out = new ObjectOutputStream(new FileOutputStream(args[2]));
        out.writeObject(keyPair.getPrivate());
        out.close();
      } else if (args[0].equals("-encrypt")) {
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        SecureRandom random = new SecureRandom();
        keygen.init(random);
        SecretKey key = keygen.generateKey();

        // wrap with RSA public key
        ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
        Key publicKey = (Key) keyIn.readObject();
        keyIn.close();

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.WRAP_MODE, publicKey);
        byte[] wrappedKey = cipher.wrap(key);
        DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2]));
        out.writeInt(wrappedKey.length);
        out.write(wrappedKey);

        InputStream in = new FileInputStream(args[1]);
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        crypt(in, out, cipher);
        in.close();
        out.close();
      } else {
        DataInputStream in = new DataInputStream(new FileInputStream(args[1]));
        int length = in.readInt();
        byte[] wrappedKey = new byte[length];
        in.read(wrappedKey, 0, length);

        // unwrap with RSA private key
        ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3]));
        Key privateKey = (Key) keyIn.readObject();
        keyIn.close();

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.UNWRAP_MODE, privateKey);
        Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);

        OutputStream out = new FileOutputStream(args[2]);
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);

        crypt(in, out, cipher);
        in.close();
        out.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    } catch (GeneralSecurityException e) {
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
Exemple #14
0
 private void writeBlock(int offset) throws IOException {
   flushRaw();
   blockChunks += BLOCK_SIZE;
   output.writeInt(offset);
   mode = Mode.Block;
 }
Exemple #15
0
 public LRC(DataOutputStream output) throws IOException {
   this.output = output;
   output.writeInt(BLOCK_SIZE);
   check = new RollingChecksum(BLOCK_SIZE);
   mode = Mode.Raw;
 }