我想给软件做个加密1 我想取得硬盘的序列号
2 然后在做加密运算
3 最后 我想把它放在一个配置文件中。以上就是我的基本思路1 我想问问大家这样行吗?
2 还有就是能给点读取硬盘序列号的代码吗?
   我对硬件这东东 不太明白谢谢大家!!!!

解决方案 »

  1.   

    感觉没有直接API支持,要用os的c/c++ API吧。
      

  2.   

    楼上说的有道理,据我了解没有直接的API可以实现,大多数都是通过DLL实现的,JNI实现。
      

  3.   

    String result = "";
    try
    {
    File file = File.createTempFile("realhowto", ".vbs");
    System.out.println(file.getName());
    System.out.println(file.getPath());
    file.deleteOnExit();
    FileWriter fw = new java.io.FileWriter(file); String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
    + "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_PhysicalMedia\") \n"
    + "For Each objItem in colItems \n"
    + "Wscript.Echo objItem.SerialNumber \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();
      

  4.   


    For Windows, Runtime.exec the dir command and parse the output 
      

  5.   

    我建议是用MAC ADDRESS
    很少人用硬盘的
    import java.io.*;
    import java.net.InetAddress;
    import java.io.InputStream;
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.text.ParseException;
    import java.util.StringTokenizer;
    import java.lang.management.MemoryUsage;
     
    import static java.lang.management.ManagementFactory.*;
    import java.lang.management.*;
    import javax.management.*;
     
    import java.lang.*;
     
    public final class NetworkInfo {
    private final static String getMacAddress() throws IOException {
    String os = System.getProperty("os.name");

    try {
    if(os.startsWith("Windows")) {
    return windowsParseMacAddress(windowsRunIpConfigCommand());
    } else if(os.startsWith("Linux")) {
    return linuxParseMacAddress(linuxRunIfConfigCommand());
    } else {
    throw new IOException("unknown operating system: " + os);
    }
    } catch(ParseException ex) {
    ex.printStackTrace();
    throw new IOException(ex.getMessage());
    }
    }

    /*
     * Windows stuff
     */
    private final static String windowsParseMacAddress(String ipConfigResponse) throws ParseException {
    String localHost = null;
    try {
    localHost = InetAddress.getLocalHost().getHostAddress();
    } catch(java.net.UnknownHostException ex) {
    ex.printStackTrace();
    throw new ParseException(ex.getMessage(), 0);
    }

    StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
    String lastMacAddress = null;

    while(tokenizer.hasMoreTokens()) {
    String line = tokenizer.nextToken().trim();

    // see if line contains IP address
    if(line.endsWith(localHost) && lastMacAddress != null) {
    return lastMacAddress;
    }

    // see if line contains MAC address
    int macAddressPosition = line.indexOf(":");
    if(macAddressPosition <= 0) continue;

    String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
    if(windowsIsMacAddress(macAddressCandidate)) {
    lastMacAddress = macAddressCandidate;
    continue;
    }
    }

    ParseException ex = new ParseException("cannot read MAC address from [" + ipConfigResponse + "]", 0);
    ex.printStackTrace();
    throw ex;
    }


    private final static boolean windowsIsMacAddress(String macAddressCandidate) {
    // TODO: use a smart regular expression
    if(macAddressCandidate.length() != 17) return false;

    return true;
    }


    private final static String windowsRunIpConfigCommand() throws IOException {
    Process p = Runtime.getRuntime().exec("ipconfig /all");
    InputStream stdoutStream = new BufferedInputStream(p.getInputStream());

    StringBuffer buffer= new StringBuffer();
    for (;;) {
    int c = stdoutStream.read();
    if (c == -1) break;
    buffer.append((char)c);
    }
    String outputText = buffer.toString();

    stdoutStream.close();

    return outputText;
    }

    private final static void loogFile(String Info, String Operating, String IP, String MAC, String javaVersion) throws IOException {
    File f = new File ("NetworkInfoLoog.txt");

    try
    {
    PrintWriter out = new PrintWriter (System.out, true);

    FileWriter fw = new FileWriter (f, true);
    PrintWriter fout = new PrintWriter (fw, true);


    fout.println ();
    fout.println (Info);
    fout.println (Operating);
    fout.println (IP);
    fout.println (MAC);
    fout.println (javaVersion);
    fout.println ();

    fout.close();
    out.close();
    }
    catch (IOException e)
    {
    e.printStackTrace ();
    }

    }

    private final static void terminalPrint(String Info, String Operating, String IP, String MAC, String javaVersion, String javaVendor, String architecture, String osVersion, String userName, String userHome, String javaVmSpecificationVersion, String javaVmSpecificationVendor)
    {
    System.out.println();
    System.out.println(Info);
    System.out.println(Operating);
    System.out.println(architecture);
    System.out.println(osVersion);
    System.out.println(IP);
    System.out.println(MAC);
    System.out.println(javaVersion);
    System.out.println(javaVendor);
    System.out.println(userName);
    System.out.println(userHome);
    System.out.println(javaVmSpecificationVersion);
    System.out.println(javaVmSpecificationVendor);

    System.out.println(System.getProperty("java.vm.version") + " Java Virtual Machine implementation version");
    System.out.println(System.getProperty("java.vm.vendor") + "   Java Virtual Machine implementation vendor");
    System.out.println(System.getProperty("java.vm.name") + "   Java Virtual Machine implementation name");
    System.out.println(System.getProperty("java.specification.version") + "   Java Runtime Environment specification version");
    System.out.println(System.getProperty("java.specification.vendor") + "   Java Runtime Environment specification vendor");
    System.out.println(System.getProperty("java.specification.name") + "   Java Runtime Environment specification name");
    System.out.println(System.getProperty("java.class.version") + "   Java class format version number");
    System.out.println(System.getProperty("java.class.path") + "   Java class path");
    System.out.println(System.getProperty("java.library.path") + "   List of paths to search when loading libraries");
    System.out.println(System.getProperty("java.io.tmpdir") + "   Default temp file path");
    System.out.println(System.getProperty("java.compiler") + "   Name of JIT compiler to use");
    System.out.println(System.getProperty("java.ext.dirs") + "   Path of extension directory or directories");
    //System.out.println(" Returns the amount of memory in bytes that is committed for the Java virtual machine to use: " +  mockXAResource.getCommitted());

    //MemoryUsage memUsage = memInfo.getUsage();

    System.out.println();
    System.out.println( "Runtime: " + getRuntimeMXBean().getUptime());
    System.out.println( "Memory: " + getMemoryMXBean().getHeapMemoryUsage() );
    System.out.println( "Memory Pool: " + getMemoryPoolMXBeans().toString());
    System.out.println( "Garbage Collector: " + getGarbageCollectorMXBeans().toString());
    System.out.println( "Available Processors: " + Runtime.getRuntime().availableProcessors());
    System.out.println( "Returns the amount of free memory in the Java Virtual Machine: " + Runtime.getRuntime().freeMemory());
    System.out.println( "Returns the maximum amount of memory that the Java virtual machine will attempt to use: " + Runtime.getRuntime().maxMemory() + " " + Runtime.getRuntime().totalMemory());
    System.out.println( "Returns kvot of the amount of memory that the Java virtual machine will attempt to use: " + ((Runtime.getRuntime().freeMemory())/(Runtime.getRuntime().totalMemory())));
    // return void System.out.println( "Runs the garbage collector: " + Runtime.getRuntime().gc());
    }
    /*
     * Main
     */
    public final static void main(String[] args) {

    try {
    String Info = "Network infos";
    String Operating = "  Operating System: " + System.getProperty("os.name");
    String architecture = "  Operating system architecture: " + System.getProperty("os.arch");
    String osVersion = "  Operating system version: " + System.getProperty("os.version");
    String IP = "  IP/Localhost: " + InetAddress.getLocalHost().getHostAddress();
    String MAC = "  MAC Address: " + getMacAddress();
    String javaVersion = "  Java Version: " + System.getProperty("java.version");
    String javaVendor = "  Java Vendor: " + System.getProperty("java.vendor");
    String javaVendorUrl = "  Java vendor URL: " + System.getProperty("java.vendor.url");
    String javaHome = "  Java installation directory: " + System.getProperty("java.home");
    String javaVmSpecificationVersion = "  Java Virtual Machine specification version: " + System.getProperty("java.vm.specification.version");
    String javaVmSpecificationVendor = "  Java Virtual Machine specification vendor: " + System.getProperty("java.vm.specification.vendor");
    String javaVmSpecificationName = "  Java Virtual Machine specification name: " + System.getProperty("java.vm.specification.name");


    String userName = "  User Name: " + System.getProperty("user.name");
    String userHome = "  User's home directory: " + System.getProperty("user.home");



    // loog file
    loogFile(Info, Operating, IP, MAC, javaVersion);
    // out print
    terminalPrint(Info, Operating, IP, MAC, javaVersion, javaVendor, architecture, osVersion, userName, userHome, javaVmSpecificationVersion, javaVmSpecificationVendor);

    } catch(Throwable t) {
    t.printStackTrace();
    }

    }
    }
      

  6.   


    /*
     * Linux stuff
     */
    private final static String linuxParseMacAddress(String ipConfigResponse) throws ParseException {
    String localHost = null;
    try {
    localHost = InetAddress.getLocalHost().getHostAddress();
    } catch(java.net.UnknownHostException ex) {
    ex.printStackTrace();
    throw new ParseException(ex.getMessage(), 0);
    }

    StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
    String lastMacAddress = null;

    while(tokenizer.hasMoreTokens()) {
    String line = tokenizer.nextToken().trim();
    boolean containsLocalHost = line.indexOf(localHost) >= 0;

    // see if line contains IP address
    if(containsLocalHost && lastMacAddress != null) {
    return lastMacAddress;
    }

    // see if line contains MAC address
    int macAddressPosition = line.indexOf("HWaddr");
    if(macAddressPosition <= 0) continue;

    String macAddressCandidate = line.substring(macAddressPosition + 6).trim();
    if(linuxIsMacAddress(macAddressCandidate)) {
    lastMacAddress = macAddressCandidate;
    continue;
    }
    }

    ParseException ex = new ParseException
    ("cannot read MAC address for " + localHost + " from [" + ipConfigResponse + "]", 0);
    ex.printStackTrace();
    throw ex;
    }


    private final static boolean linuxIsMacAddress(String macAddressCandidate) {
    // TODO: use a smart regular expression
    if(macAddressCandidate.length() != 17) return false;
    return true;
    }


    private final static String linuxRunIfConfigCommand() throws IOException {
    Process p = Runtime.getRuntime().exec("ifconfig");
    InputStream stdoutStream = new BufferedInputStream(p.getInputStream());

    StringBuffer buffer= new StringBuffer();
    for (;;) {
    int c = stdoutStream.read();
    if (c == -1) break;
    buffer.append((char)c);
    }
    String outputText = buffer.toString();

    stdoutStream.close();

    return outputText;
    }

      

  7.   

    下面这个可以满足你了Hard disk serial number
    import java.io.File;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;public class DiskUtils {
      private DiskUtils() {  }  public static String getSerialNumber(String drive) {
      String result = "";
        try {
          File file = File.createTempFile("realhowto",".vbs");
          file.deleteOnExit();
          FileWriter fw = new java.io.FileWriter(file);      String 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);
          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();
      }  public static void main(String[] args){
        String sn = DiskUtils.getSerialNumber("C");
        javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
             null, sn, "Serial Number of C:",
             javax.swing.JOptionPane.DEFAULT_OPTION);
      }
    }
      

  8.   

     String sn = DiskUtils.getSerialNumber("C");这句话是什么意识呢?
      

  9.   

    硬盘序列号和C盘标识是不一样的,C盘标识是分区,是可以改变的.一般说,硬盘序列号是不能变的,JAVA里要取得硬盘序列号,应该还是要用到DLL吧.JAVA下没取过,其他语言倒有取过.
      

  10.   


    确实,用硬盘不是个好的选择,不过MAC ADDRESS也不完美,有的机器并没有网络,而有的机器比如笔记本,拿到的MAC又是好几个;用CPU序列号的话,有的机器取不到,有的机器取到几个;有些情况下取不到,包括主板号。用这些好像不是一个特别好的解决方案。
      

  11.   

    适用mac地址,
    现在的产品就是这么弄的
      

  12.   

    记得看过。用JNI做。楼主google一下
      

  13.   

    java取这玩意儿?
    应该要用JNI吧?