天天看点

编程实现查看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;
    }
}