Пример #1
0
  /**
   * Methode : fillOutTheChoiceList() allows you to fill out the choice list for the user This
   * methode will search all the device linked with this SESAME.
   */
  public void fillOutTheChoiceList() {
    System.out.println("<--- BEGIN : fillOutTheChoiceList() --->");

    String[] all_device_id;

    // Make the deserialization of the table file which is the database of the device
    IdentifiantAndKeyTable table_id_key = null;
    boolean flag_extraction = false;
    File file = new File("identifiant_and_key_table.ser");
    try (FileInputStream fileIn = new FileInputStream(file);
        ObjectInputStream in = new ObjectInputStream(fileIn)) {
      table_id_key = (IdentifiantAndKeyTable) in.readObject();

      flag_extraction = true;

    } catch (IOException i) {
      flag_extraction = false;
      System.out.println("IOException : " + i.getMessage());
    } catch (ClassNotFoundException c) {
      System.out.println("DeviceLinkingData class not found");
      flag_extraction = false;
    }

    // Check if the deserialization is done succesffully
    if (flag_extraction) {
      all_device_id = table_id_key.getAllLinkedDeviceId();
      if (all_device_id.length > 0) {
        // Fit the device array list
        for (int i = 0; i < all_device_id.length; i++) {
          liste_peripherique.add(all_device_id[i], i);
        }
      } else {
        for (int i = 0; i < device_list.length; i++) {
          liste_peripherique.add(device_list[i], i);
        }
      }
    } else {
      for (int i = 0; i < device_list.length; i++) {
        liste_peripherique.add(device_list[i], i);
      }
    }
    System.out.println("<--- END : fillOutTheChoiceList() --->");
    // liste_peripherique.addListSelectionListener(this);
  }
  /**
   * Methode : addDeviceLink() allows you to add the device in the table which containing the id and
   * the key information.
   *
   * @param data
   * @return true if the owner information is correct
   * @throws java.lang.InterruptedException
   */
  private boolean addDeviceLink(String[] data) throws InterruptedException {

    boolean flag = false;
    boolean flag_checksum = false;
    boolean flag_extract_user = false;
    boolean flag_extract_device = false;
    boolean flag_creating_device = false;
    boolean flag_serialization = false;
    boolean flag_end = false;

    int checksum_received = 0;
    int checksum_calcule = 0;

    int key_size = 0;

    String id_device = "";
    OwnerInformation accredited_user = null;
    DeviceLinkingData device_linking = null;

    DeviceLinkedData device_linked = null;

    IdentifiantAndKeyTable table_temp = null;

    if (data != null && data.length >= 2) {
      key_accredited = data[0];
      key_size = key_accredited.length();

      // Extract the checksul and convert it
      try {
        checksum_received = Integer.parseInt(data[1]);
      } catch (NumberFormatException ex) {
        checksum_received = 0;
        System.out.println("Impossible de convert String to Intger : " + ex.getMessage());
      }

      // Calcul du checksul
      checksum_calcule = (key_size * 128) + (key_size / 2) * 64 + (key_size / 4) * 32;
      checksum_calcule += (key_size / 8) * 16 + (key_size / 16) * 8 + (key_size / 32) * 4;
      checksum_calcule += (key_size / 64) * 2;

    } else {
      System.out.println("L'argument passé en paramètre est invalide");
      flag = false;
    }

    flag_checksum = checksum_calcule == checksum_received;

    // Test if the checksum is correct or not
    if (flag_checksum) {
      System.out.println("CRC Correct");

      File file = new File("accredited_information.ser");
      // Deserialization otf the OwnerInformation
      try (FileInputStream fileIn = new FileInputStream(file);
          ObjectInputStream in = new ObjectInputStream(fileIn)) {
        accredited_user = (OwnerInformation) in.readObject();
        flag_extract_user = true;

      } catch (IOException i) {
        flag_extract_user = false;
        System.out.println("IOException : " + i.getMessage());
      } catch (ClassNotFoundException c) {
        flag_extract_user = false;
        System.out.println("OwnerInformation class not found " + c.getMessage());
      }

      file = new File("linking_test.ser");
      // Deserialization of the DeviceLinkingInfo
      try (FileInputStream fileIn = new FileInputStream(file);
          ObjectInputStream in = new ObjectInputStream(fileIn)) {
        device_linking = (DeviceLinkingData) in.readObject();
        System.out.println("DEVICE LINKING DEBUG = \n" + device_linking);
        flag_extract_device = true;

      } catch (IOException i) {
        flag_extract_device = false;
        System.out.println("IOException : " + i.getMessage());
      } catch (ClassNotFoundException c) {
        flag_extract_device = false;
        System.out.println("DeviceLinkingData class not found " + c.getMessage());
      }
    } else {
      flag_extract_user = false;
      flag_extract_device = false;
      System.out.println("CRC inCorrect");
    }

    // Check if the extraction of the owner is done succesfully, create the class DeviceLinkedData
    if (flag_extract_user && flag_extract_device) {
      id_device = device_linking.getDeviceIdentifiant();

      if (id_device != null) {
        System.out.println("Identifiant du Device partagé = " + id_device);
      } else {
        System.out.println("Identifiant du Device partagé est null");
        id_device = "TOTO";
      }

      // Create the DeviceLinkedData class
      // device_linked = new DeviceLinkedData (user,device_linking, id_device, key);
      // device_linked = new DeviceLinkedData (accredited_user,device_linking, id_device,
      // key_accredited);
      device_linked =
          new DeviceLinkedData(accredited_user, device, id_sesame_sharer, key_accredited);
      flag_creating_device = true;
      System.out.println("DEVICE LINKED DEBUG = \n" + device_linked);
      System.out.println("device_linked.getDeviceId() = " + device_linked.getDeviceId());
    } else {
      flag_creating_device = false;
    }

    // Check if the class "DeviceLinkedData" is created correctly
    if (flag_creating_device) {
      // Make the deserialization of the table file which is the database of the device
      File file = new File("identifiant_and_key_table.ser");
      try (FileInputStream fileIn = new FileInputStream(file);
          ObjectInputStream in = new ObjectInputStream(fileIn)) {
        table_temp = (IdentifiantAndKeyTable) in.readObject();

        // Add the device link information on the table
        table_temp.addDeviceForLink(device_linked);
        System.out.println("Content of the table after");
        System.out.println(table_temp);

        flag_serialization = true;

      } catch (IOException i) {
        flag_serialization = false;
        System.out.println("IOException : " + i.getMessage());
      } catch (ClassNotFoundException c) {
        flag_serialization = false;
        System.out.println("IdentifiantAndKeyTable class not found");
      }
    } else {
      System.out.println(
          "'identifiant_and_key_table.ser' file is not found in the current directory");
      flag_serialization = false;
    }

    // Check if the flag serialization is correct then make the serialization in the file
    if (flag_serialization) {
      // Make the serialization to save the new added device on the table
      File file = new File("identifiant_and_key_table.ser");
      try (FileOutputStream fileOut = new FileOutputStream(file);
          ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
        out.writeObject(table_temp);
        // End of the Serialization for IdentifiantAndKeyTable
        System.out.println(
            "Serialized of the new IdentifiantAndKeyTable is saved in identifiant_and_key_table.ser");
        flag_end = true;
      } catch (IOException io) {
        flag_end = false;
        System.out.println("Exception for IdentifiantAndKeyTable : " + io.getMessage());
        System.out.println(
            "Serialized of the new IdentifiantAndKeyTable is not done : check error");
      }
    } else {
      flag_end = false;
      System.out.println(
          "impossible to read the 'identifiant_and_key_table.ser' file or the file is not found");
    }

    return flag_end;
  }
Пример #3
0
  public static void main(String[] args) {
    IdentifiantAndKeyTable table_id_key = new IdentifiantAndKeyTable();
    System.out.println(table_id_key);
    boolean flag = false;
    boolean flag_serialization = false;

    // Make the serialization to save the new added device on the table
    File file = new File("identifiant_and_key_table.ser");
    try (FileOutputStream fileOut = new FileOutputStream(file);
        ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
      out.writeObject(table_id_key);
      flag = true;
    } catch (IOException io) {
      System.out.println("Exception for IdentifiantAndKeyTable : " + io.getMessage());
      flag = false;
    }
    // End of the Serialization for IdentifiantAndKeyTable
    System.out.println(
        "Serialized of the new IdentifiantAndKeyTable is saved in identifiant_and_key_table.ser");

    String[] data = {
      "Maison", "Proprietaire", "Rez de Chaussee", "Porte d'entree principale",
          "Ma residence principale",
      "13", "Avenue Maximilien Robespierre", "94400", "Vitry sur Seine", "France"
    };
    DeviceLinkingData device_linking = new DeviceLinkingData(data);
    System.out.println(device_linking);

    String identifiant = "SESAME DOORS";
    String key =
        "AZERTYUIOPQSDKFGFGKFGJkfgjsdffsdjfsdkfjdsAZSQAZSQWXCDFRTGHVBCFDjshfjsdfksdfjdsfhsdjfdjfkjqkfjqfhqjdfjdjfdjfhdsjfhsdjfdskfjsdkf";

    DeviceLinkedData device_linked = null; // new DeviceLinkedData (device_linking, key);

    IdentifiantAndKeyTable table_temp = null;
    // Deserialization
    if (flag) {
      // Make the deserialization of the table file which is the database of the device
      // file = new File("identifiant_and_key_table.ser");
      try (FileInputStream fileIn = new FileInputStream(file);
          ObjectInputStream in = new ObjectInputStream(fileIn)) {
        table_temp = (IdentifiantAndKeyTable) in.readObject();

        // Add the device link information on the table
        System.out.println("Content of the table before");
        System.out.println(table_temp);
        table_temp.addDeviceForLink(device_linked);
        System.out.println("Content of the table after");
        System.out.println(table_temp);
        flag_serialization = true;

      } catch (IOException io) {
        // flag_serialization = false;
        System.out.println("IOException : " + io.getMessage());
      } catch (ClassNotFoundException c) {
        System.out.println("DeviceLinkingData class not found");
        // flag_serialization =false;
      }
    } else {
      System.out.println("Nothing and flag_serialization = false");
      flag_serialization = false;
    }

    if (flag_serialization) {
      System.out.println("Make the serialization to save the new added device on the table");
      // file = new File("identifiant_and_key_table.ser");
      try (FileOutputStream fileOut = new FileOutputStream(file);
          ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
        out.writeObject(table_temp);
        // End of the Serialization for IdentifiantAndKeyTable
        System.out.println(
            "Serialized of the new IdentifiantAndKeyTable is saved in identifiant_and_key_table.ser");

      } catch (IOException io) {
        System.out.println("Exception for IdentifiantAndKeyTable : " + io.getMessage());
        System.out.println(
            "Serialized of the new IdentifiantAndKeyTable is not done : check error");
      }
    }

    String key_temp = device_linked.getDeviceKey();
    System.out.println("Key = " + key_temp + ", key size = " + key_temp.length());

    String[] sampled_data = sampleDataToSend(key_temp);
    for (int i = 0; i < sampled_data.length; i++) {
      System.out.println("data[" + i + "] = " + sampled_data[i]);
    }
  }