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 be
String 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.
20 comments: on "How to secure Java Applications by using VBScript to get a Unique Computer Serial Number"
thanks
Its very good topic and really useful because i search for it before
At All
Special thanks for u
Dear Reader,
you are welcome and wait the coming soon :)
Hope to enjoy your time here.
Thank you Islam for your efforts, nice starting topic.I suggest the next article shows how it done on a linux machine
Keep moving forward ;)
Yours,
Usama Fath
Dear Usama,
You are welcome and I'm very happy for your comment as I really trust your view of point :)
I'll try to find how to do something like that on Linux soon isA.
Hope to enjoy your time here.
Great Article Eng Islam
waiting your new ;) Go On
thx brother
good job
u r the best
Thnx A7med
pleased to hear that and wait the new stuff ;)
@ Anonymous
Thnx man, I'll do my best to get you the best :)
Thank you Islam for your effort and i wait for the next topic
Thanks Islam, a very good topic.
@*
Thnx dears for your support :)
gamd ya smsm masha2aAllah we rabna ywafa2k wemestanyn el gdeeeed mnk
مشكووووووووووووووووووووووووووووووووووووووووووووووووووووووووور
@Fifi
Thanks for your support :)
@Anonymous
You are welcome
Sorry for being late Islam.I was sick
I hoped that i was the first one comment on your topic.
This is topic is very good.I was searching how to get the serial Number of the Computer but you bring this to me. :)
Thanx Islam.
@Beso
You shouldn't be sorry at all, you are always welcome.
We are very happy that you liked it and we will do our best to get you the best
Thanks for your support, beso.
thanx alot
please, can U post full source code Or complete example
@Anonymous
You can find The Full Source Code at the end of the topic Just search for this at the end {Here is the full source code with sample VBScript:} you can call the "getMotherboardSN()" static method whenever you want, and for sure you may customize the VBScript code.
If you still need any help please feel free to contact us here or at : javafromegyptblog[AT]gmail[DOT]com
Thanks for your consideration ... hope to enjoy the blog
thanks its work
@Anonymous
Thanks for your support ..
Post a Comment