title: Computer specific ID?
body: <p>I'm trying to generate a specific computer id using Java. I've thought about things like Hard Drive Serials, or Windows Serial Keys, CPU ID's, or MAC Addresses, but other computers could have the same ones.. For instance, If someone pirates a copy of Windows 7 they could have the same serial as someone.. I was wondering if someone could give me a way to generate a computer specific ID that is never changed and is retrievable using Java?</p>
  
  <p>I did some research and found some useful functions. And, I was thinking something like this. But if they change their hardware, It will change the computer ID. Anyone know of something I can use?</p>
  
  <pre><code>public String getComputerID(){
      InetAddress ip = InetAddress.getLocalHost();
      NetworkInterface network = NetworkInterface.getByInetAddress(ip);
      byte[] mac = network.getHardwareAddress();
      String sn = getSerialNumber("C");
      String cpuId = getMotherboardSN();
      return MD5(mac + sn + cpuId);
  }
  
  
  public String MD5(String md5) {
  try {
          java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
          byte[] array = md.digest(md5.getBytes());
          StringBuffer sb = new StringBuffer();
          for (int i = 0; i &lt; array.length; ++i) {
          sb.append(Integer.toHexString((array[i] &amp; 0xFF) | 0x100).substring(1,3));
      }
          return sb.toString();
      } catch (java.security.NoSuchAlgorithmException e) {
      }
      return null;
  }
  
  public String getSerialNumber(String drive) {
  String result = "";
      try {
      File file = File.createTempFile("realhowto",".vbs");
      file.deleteOnExit();
      FileWriter fw = new java.io.FileWriter(file);
  
    StringBufferring vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                  +"Set colDrives = objFSO.Drives\n"
                  +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
                  +"Wscript.Echo objDrive.SerialNumber";  // see note
      fw.write(vbs);
      FileWriter.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        BufferedReader input =
          new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = input.readLine()) != null) {
          result += line;
      }
      input.close();
      }
      catch(Exception e){
          e.printStackTrace();
      }
      return result.trim();
  }
  
  public String getMotherboardSN() {
  String result = "";
  try {
  File file = File.createTempFile("realhowto",".vbs");
  file.deleteOnExit();
  FileWriter fw = new java.io.FileWriter(file);
  
  String vbs =
  "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
  + "Set colItems = objWMIService.ExecQuery _ \n"
  + " (\"Select * from Win32_BaseBoard\") \n"
  + "For Each objItem in colItems \n"
  + " Wscript.Echo objItem.SerialNumber \n"
  + " exit for ' do the first cpu only! \n"
  + "Next \n";
  
  fw.write(vbs);
  fw.close();
  Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
  BufferedReader input =
  new BufferedReader
  (new InputStreamReader(p.getInputStream()));
  String line;
  while ((line = input.readLine()) != null) {
  result += line;
  }
  input.close();
  }
  catch(Exception e){
  e.printStackTrace();
  }
  return result.trim();
  }
  </code></pre>