天天看點

程式設計實作檢視Windows無線網密碼

最近研究了一下通過程式設計實作在windows檢視wifi密碼的方法

主要核心是調用cmd指令來實作

核心功能隻有兩段cmd指令

1:netsh wlan show profile//檢視電腦儲存的wifi資訊清單

2:netsh wlan show profile name=wifiname key=clear//檢視具體的wifi資訊  name是上面一段指令查到的wifi名

這種方式隻能檢視電腦已經連接配接過的wifi的密碼!!!

下面是具體的java代碼(寫的不好的地方歡迎指出來):

public class NewTest {

    public static void main(String[] args) {
        String command = "cmd.exe /c netsh wlan show profile";
        String command1 = "cmd.exe /c netsh wlan show profile name=%s key=clear";
        try {
            String temp;
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = getCMD(command);
            if(reader==null){
                return;
            }
            while((temp = reader.readLine())!=null){
                if(temp.contains("所有使用者配置檔案")){
                    //截取無線網名
                    String netName = temp.substring(temp.indexOf(":") + 1);
                    //根據無線網名查找密碼
                    BufferedReader cmd = getCMD(String.format(command1, netName));
                    if(cmd==null){
                        return;
                    }
                    String t;
                    while ((t=cmd.readLine())!=null){
                        if(t.contains("關鍵内容")){
                            sb.append(netName).append(":").append(t.substring(t.indexOf(":") + 1)).append("\n");
                            cmd.close();
                            break;
                        }
                    }
                }
            }
            reader.close();
            newTest.textArea1.setText(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static BufferedReader getCMD(String command){
        try {
            //修改亂碼問題
            return new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec(command).getInputStream(), Charset.forName("gbk")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}