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?
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?
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 < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 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();
}
I actually do believe you should use something from hardware profile.
A computer can be considered as a set of pieces of hardware, including the network interface.
A typical pattern can be to have combination of a MAC address and a generated ID by a management system that manages computers over the network.
The MAC address to identify uniquely the machine during a registration process to the management system.
As a result of the registration, the management system can return a generated UniqueId,
to be stored on the computer that registered to it, and will later on be used.
After a successful registration, you can replace the network interface card, as the computer does not depend on the MAC address to be identified.
You can also consider using the linux dmidecode utility
(for linux machines,
as you provided a win-based solution,
so for our linux readers,
I would like to suggest a linux alternaties) (if the machine you want to uniquely identify has linux and dmidecoe installed).
Using dmidecoe you can get more hardware profile, and perform some hash function on it, and generate a unique ID that will identify uniquely (with high probability, to be precise) your machine.
Read more about dmidecode here.
Of course, in case you go to “get information on hardware from the operating system” approach (which is dmidecode or what you suggested at the part after getting the MAC address,
You need for a cross platform code to check what is the OS the java program runs on, you do that using this: