Did you tried many times to get a unique computer ID to use it in securing your java application?!
Do you want to know the easy way to get that ID in just 5 minutes?!!
Yes you can, We will learn how to get basic hardware serial numbers at windows platform and you will find the complete code at the end of the tutorial.
before we start any one may ask why I even want to get this number?
Well, consider that scenario :
you have developed some application and want to distribute it for some of money. We here have a point, how we will be sure that if I sold it for someone, he will not redistribute it, So we will use that Unique ID to give him a copy that will run only on his PC.Make sense !, So lets dance.
We will use the VBScript codes on the fly to get the system info like CPU information or Motherboard information Like this:
Here is the Java source code to get the Unique ID divided on to two parts.
First Part of Java Code:
- We here open temp File to put the script on it, We use temp folder on C:\ Drive to execute the script and then delete it.
Second Part of Java Code:
- We here execute the ".vbs" file after writing the script on it and then we retrieve the results and close the file.
Take on your mind that:
- When you put your VBScript Code as String you must convert all special characters(e.g the " will be \" ).
If the VBScript is
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
The String will beString vbs="Set objReg = GetObject(\"winmgmts:{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\default:StdRegProv\")";
- You can customize the VBScript code as you want. For more VBScript Code use TechNet Script Center Gallery.
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class VBSUtil {
private VBSUtil() {
}
static String result = "";
private static String getMotherboardSN() {
try {
File f = new File("C:\\");
File file = File.createTempFile("realhowto", ".vbs", f);
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "On Error Resume Next\n" + "strComputer = \".\"\n" + "Set objWMIService = GetObject(\"winmgmts:\\\\\" & strComputer & \"\\root\\cimv2\")\n" + "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\")\n" + "For Each objItem in colItems\n" + " Wscript.Echo \"Processor Id: \" & objItem.ProcessorId\n" + "Next\n" + "strComputer = \".\"\n" + "Set objWMIService = GetObject(\"winmgmts:\" _\n" + " & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\")\n" + "Set colBIOS = objWMIService.ExecQuery _\n" + "(\"Select * from Win32_BIOS\")\n" + "For each objBIOS in colBIOS\n" + " Wscript.Echo \"Release Date: \" & objBIOS.ReleaseDate\n" + "next";
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();
}
}
Thanks for your time and hope this was helpful. For any questions, Please feel free to comment the post.

