📜  获取已连接 wifi windows 10 的密码 (1)

📅  最后修改于: 2023-12-03 15:11:51.833000             🧑  作者: Mango

获取已连接 WiFi Windows 10 的密码

对于Windows 10系统中已经连接过的WiFI网络,我们可以通过一些API获取它们的密码。以下是获取已连接WiFi Windows 10的密码的步骤:

步骤1. 获取已连接网络的属性

首先,我们需要使用NetworkListManager类来获取已连接的网络属性。这可以通过以下代码完成:

using System;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Security;
using System.Text;

namespace GetConnectedWifiPassword
{
    static class NetworkListManagerInterop
    {
        [ComImport, Guid("DCB00C01-570F-4A9B-8D69-199FDBA5723B")]
        class NetworkListManager { }

        [ComImport, Guid("DCB00000-570F-4A9B-8D69-199FDBA5723B")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [SuppressUnmanagedCodeSecurity]
        [ComVisible(true)]
        public interface INetworkListManager
        {
            [return: MarshalAs(UnmanagedType.Interface)]
            INetwork[] GetNetworks(NetworkConnectivityLevels Flags);

            [return: MarshalAs(UnmanagedType.Interface)]
            INetwork GetNetwork(Guid gdNetworkId);
        }

        [ComImport, Guid("DCB00002-570F-4A9B-8D69-199FDBA5723B")]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [SuppressUnmanagedCodeSecurity]
        [ComVisible(true)]
        public interface INetwork
        {

            Guid GetNetworkId();

            [return: MarshalAs(UnmanagedType.LPWStr)]
            string GetName();

            [return: MarshalAs(UnmanagedType.LPWStr)]
            string GetDescription();

            void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string szDescription);

            [return: MarshalAs(UnmanagedType.Interface)]
            IPropertyBag GetProperties();
        }
    }

    public class NetworkListItem
    {
        private Guid _id;
        private string _name;
        private string _description;
        private bool _isConnected;
        private string _connectionStatus;

        public Guid Id { get { return _id; } }
        public string Name { get { return _name; } }
        public string Description { get { return _description; } }
        public bool IsConnected { get { return _isConnected; } }
        public string ConnectionStatus { get { return _connectionStatus; } }

        public override string ToString()
        {
            return _name;
        }

        public NetworkListItem(Guid id, string name, string description, bool isConnected, string connectionStatus)
        {
            _id = id;
            _name = name;
            _description = description;
            _isConnected = isConnected;
            _connectionStatus = connectionStatus;
        }
    }

    public static class NetworkListManagerHelper
    {
        public static NetworkListItem[] GetNetworkListItems()
        {
            var manager = new NetworkListManagerInterop.INetworkListManager();
            var networks = manager.GetNetworks(NetworkConnectivityLevels.All);
            NetworkListItem[] result = new NetworkListItem[networks.Length];
            for (int i = 0; i < networks.Length; i++)
            {
                var network = networks[i];
                Guid id = network.GetNetworkId();
                string name = network.GetName();
                string description = network.GetDescription();
                bool isConnected = network.IsConnected;
                string connectionStatus = GetConnectionStatus(network);
                result[i] = new NetworkListItem(id, name, description, isConnected, connectionStatus);
            }

            return result;
        }

        private static string GetConnectionStatus(NetworkListManagerInterop.INetwork network)
        {
            var properties = network.GetProperties();
            object obj;
            if (properties != null)
            {
                properties.GetProperty("NLM_CONNECTION_STATUS", out obj);
                int connectionStatus = Convert.ToInt32(obj);
                switch (connectionStatus)
                {
                    case 0:
                        return "Disconnected";
                    case 1:
                        return "Connecting";
                    case 2:
                        return "Connected";
                    case 3:
                        return "Disconnecting";
                    case 4:
                        return "Authenticating";
                    case 5:
                        return "Authentication Failed";
                    default:
                        return "Unknown";
                }
            }

            return "";
        }
    }
}
步骤2. 搜索已连接网络的密码

一旦我们有了这些信息,我们就可以对已连接的网络进行进一步的操作,包括搜索它的密码。以下是搜索已连接网络的密码的代码:

public static string GetWifiPassword(string ssid)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "netsh.exe";
    psi.Arguments = "wlan show profile name=\"" + ssid + "\" key=clear";
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.CreateNoWindow = true;
    Process p = Process.Start(psi);
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

    string[] lines = output.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
    foreach (string line in lines)
    {
        if (line.Trim().StartsWith("Key Content"))
        {
            string[] parts = line.Split(':');
            if (parts.Length == 2)
            {
                return parts[1].Trim();
            }
            else
            {
                return "";
            }
        }
    }

    return "";
}
步骤3. 示例代码

以下是使用上述代码的示例:

using System;

namespace GetConnectedWifiPassword
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Connected WiFi list:");

            NetworkListItem[] items = NetworkListManagerHelper.GetNetworkListItems();
            foreach (var item in items)
            {
                if (item.IsConnected)
                {
                    Console.WriteLine(item.Name + " : " + GetWifiPassword(item.Name));
                }
            }

            Console.ReadLine();
        }
    }
}
步骤4. 结论

使用上述步骤,我们可以轻松地获取Windows 10系统中已连接WiFi网络的密码。