http://blog.csdn.net/coolwzjcool/article/details/6698327

版权声明:本文为博主原创文章,未经博主允许不得转载。

为了达到软件注册,或者说软件和电脑绑定的目的,需要将电脑上的固定编号进行一系列的算法计算,并生成唯一和软件匹配的号码。

那么使用java如何达到这个目的呢?

通常做法都是通过java的Runtime来完成,通过 process的输入流,进行获取相关的信息。

下面列举具体的例子:

1、DiskUtils 获取硬盘编号

  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. class DiskUtils {
  6. private DiskUtils() {
  7. }
  8. public static String getSerialNumber(String drive) {
  9. String result = "";
  10. try {
  11. File file = File.createTempFile("damn", ".vbs");
  12. file.deleteOnExit();
  13. FileWriter fw = new java.io.FileWriter(file);
  14. String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
  15. + "Set colDrives = objFSO.Drives\n"
  16. + "Set objDrive = colDrives.item(\""
  17. + drive
  18. + "\")\n"
  19. + "Wscript.Echo objDrive.SerialNumber"; // see note
  20. fw.write(vbs);
  21. fw.close();
  22. Process p = Runtime.getRuntime().exec(
  23. "cscript //NoLogo " + file.getPath());
  24. BufferedReader input = new BufferedReader(new InputStreamReader(
  25. p.getInputStream()));
  26. String line;
  27. while ((line = input.readLine()) != null) {
  28. result += line;
  29. }
  30. input.close();
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. return result.trim();
  35. }
  36. }

2、MacUtils 获取MAC地址

  1. import java.io.InputStreamReader;
  2. import java.io.LineNumberReader;
  3. public class MacUtils {
  4. public static void  getMac(){
  5. try {
  6. Process process = Runtime.getRuntime().exec("ipconfig /all");
  7. InputStreamReader ir = new InputStreamReader(process.getInputStream());
  8. LineNumberReader input = new LineNumberReader(ir);
  9. String line;
  10. while ((line = input.readLine()) != null)
  11. if (line.indexOf("Physical Address") > 0) {
  12. String MACAddr = line.substring(line.indexOf("-") - 2);
  13. System.out.println("MAC address = [" + MACAddr + "]");
  14. }
  15. } catch (java.io.IOException e) {
  16. System.err.println("IOException " + e.getMessage());
  17. }
  18. }
  19. }

3、 测试程序:

  1. import java.io.InputStreamReader;
  2. import java.io.LineNumberReader;
  3. import java.net.NetworkInterface;
  4. import java.net.SocketException;
  5. import java.util.Enumeration;
  6. import java.util.Vector;
  7. public class TestMain {
  8. /**
  9. * @param args
  10. */
  11. public static void main(String[] args) {
  12. // TODO Auto-generated method stub
  13. //****************获取MAC地址*****************//
  14. System.out.println("***MAC地址***");
  15. MacUtils.getMac();
  16. //****************获取硬盘ID*****************//
  17. String sn = DiskUtils.getSerialNumber("C");
  18. System.out.println("***硬盘编号***");
  19. System.out.println(sn);
  20. }
  21. }

4、执行结果(我电脑上有几个VPN,所以就有多个MAC;为了防止别人搞我的电脑,数字和字母用*号代替)

***MAC地址***
MAC address = [**-**-**-**-**-**]
MAC address = [**-**-**-**-**-**]
MAC address =[**-**-**-**-**-**]
MAC address = [**-**-**-**-**-**]
***硬盘编号***
1290******

 
 
 

package com.sunbin.test;

import java.io.*;

import java.util.regex.*;

public class ReadMacByJava {

private String mPhysicalAddress = "";

private int mPhysicalMacNumber = 0;

private boolean isInit = false;

public void init() {
try {
String line;
String os = System.getProperty("os.name");
Process process = null;
if (os != null && os.startsWith("Windows")) {
process = Runtime.getRuntime().exec("cmd /c ipconfig /all");
} else {
process = Runtime.getRuntime().exec("ifconfig eth0");
}
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
Pattern macPattern = Pattern
.compile("([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}");
Matcher macMatcher;
boolean result;
while ((line = bufferedReader.readLine()) != null) {
if ("".equals(line)) {
continue;
}
macMatcher = macPattern.matcher(line);
result = macMatcher.find();
if (result) {
mPhysicalMacNumber++;
if ("".equals(mPhysicalAddress)) {
mPhysicalAddress = macMatcher.group(0);
} else {
mPhysicalAddress += ("," + macMatcher.group(0));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
isInit = true;
}

public String getPhysicalAddress() {
if (isInit) {
return this.mPhysicalAddress;
} else {
return "Mac is not init.";
}
}

public static void main(String[] args){
ReadMacByJava mac = new ReadMacByJava();
mac.init();
System.out.println("MAC address :"+ mac.getPhysicalAddress());
}
}

java获取硬盘ID以及MAC地址的更多相关文章

  1. Java获取主板序列号、MAC地址、CPU序列号工具类

    import java.io.File; import java.io.FileWriter; import java.io.BufferedReader; import java.io.IOExce ...

  2. JAVA获取本机的MAC地址

    /** * 获取本机的Mac地址 * @return */ public String getMac() { InetAddress ia; byte[] mac = null; try { // 获 ...

  3. java支持跨平台获取cpuid、主板id、硬盘id、mac地址 (兼容windows、Linux)

    windows: package cn.net.comsys.helper.system.info;   import java.io.BufferedReader; import java.io.F ...

  4. java 获取计算机名称, ip, mac地址

    写在前面 通常所说查询本机mac地址是以以太网为准的, 也就是网线那个口..这种描述略捞. 但是通过java的getHostAddress获取ip以及getHardwareAddress()方法获取m ...

  5. C#获取cpu序列号 硬盘ID 网卡硬地址以及操作注册表 .

    转:http://blog.csdn.net/smartsmile2012/article/details/8682295 #region 获取cpu序列号 硬盘ID 网卡硬地址 /**/ /// & ...

  6. java获取客户端ID地址

    转:http://zhenchengchagangzi.iteye.com/blog/1199300#bc2372048 在JSP里,获取客户端的IP地址的方法是:request.getRemoteA ...

  7. C# 中获取CPU序列号/网卡mac地址

    1.cpu序列号2.mac序列号3.硬盘id在给软件加序列号时这三个应该是最有用的,可以实现序列号和机器绑定,对保护软件很有好处.哈哈.   using System; using System.Ma ...

  8. ASP.NET获取客户端IP及MAC地址

    朋友最近问如何获取客户端IP及MAC地址,一直想把这段给整理一下,契机来了:下边分为了C#后台获取的方法和前台Javascript(调用ActiveX)获取的方法,大家如果有好的方法一起讨论撒O(∩_ ...

  9. 如何获取公网IP的mac地址

    如何获取远程IP的mac地址 思路分析 由于java本身没有相关的jar包进行获取,所以这里介绍从其他的方面进行入手和实践 使用的工具对比: tcpdump tshark pcap4j 都可以达到抓包 ...

随机推荐

  1. The List ADT

    1.Definiation A list is a sequence.  a0, a1,a2,..., aN (N>0) 2.Character For any list except the ...

  2. override the hashcode and equals method in java

    http://howtodoinjava.com/2012/10/09/working-with-hashcode-and-equals-methods-in-java/

  3. text-size-adjust的值为100% 代替值 none

    iPhone 横屏默认会放大文字,设置text-size-adjust会解决这个问题 一般用text-size-adjust:none 但建议用100%代替none text-size-adjust: ...

  4. Jquery.Linq用法

    下载:http://linqjs.codeplex.com/ LINQ Pad Enumerable.Range(0, 20).Where("$ % 3 == 0").Select ...

  5. 如何创建自定义ASP.NET MVC5脚手架模板?

    I'm using ASP.NET MVC5 and VS2013 I've tried to copy CodeTemplates folder from C:\Program Files (x86 ...

  6. php过滤函数

    addcslashes — 以 C 语言风格使用反斜线转义字符串中的字符 addslashes — 使用反斜线引用字符串 strip_tags — 从字符串中去除 HTML 和 PHP 标记 stri ...

  7. MVC(@html.action)调用子操作方法

    1*简单调用 子操作方法: 服务端 客户端 2.带有参数的调用 子操作方法 服务端 客户端

  8. adb端口被占用

    程序不能执行,kill掉任务管理器里面adb服务,重新连接设备仍然有错 查到可能是adb端口被占用 查看adb用的是哪个端口:C:\Users\wanglin>adb nodaemon serv ...

  9. Notification使用笔记

    之前在项目中使用了Notification,现分享出来: checkNotification() function checkNotification(){ //判断是否支持Notification ...

  10. Android Camera(一)

    最近老大交给了一个任务,说是要在本地视频端很够调节摄像头焦距. 碰到了一些问题: 1.手机支不支持摄像头变焦 2.系统自带摄像软件可以变焦,但是自己编写的程序不支持变焦, 这个问题网上也有很多童鞋碰到 ...