Get current WiFi password programmatically

Hello,

I'm trying to find a way, to programmatically get the password, of current WiFi connection, so I could use the password to connect another device to the same WiFi.

One of the possibilities that I've found was using a terminal command: "security find-generic-password -ga TypeInYourWiFiNameHere | grep “password:”".

Is there any other, more appropriate way to do this in Objective C/C++? Asking user for permission is completely fine and understandable.

Accepted Reply

The Mac stores the Wi-Fi password in the System keychain. The best way to get it is via CoreWLAN’s CWKeychainFindWiFiPassword function. For example:

import CoreWLAN

func password(for ssid: String) throws -> String {
    let ssid = Data(ssid.utf8)
    var passwordQ: NSString? = nil
    let err = CWKeychainFindWiFiPassword(CWKeychainDomain.system, ssid, &passwordQ)
    guard err == noErr else {
        throw NSError(domain: NSOSStatusErrorDomain, code: Int(err), userInfo: nil)
    }
    return passwordQ! as String
}

This will prompt for an admin password. There’s no way to get this info without that prompt, for obvious security reasons.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Replies

I'm trying to find a way, to programmatically get the password, of current WiFi connection

On what platform?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm trying to do this for Mac.

The Mac stores the Wi-Fi password in the System keychain. The best way to get it is via CoreWLAN’s CWKeychainFindWiFiPassword function. For example:

import CoreWLAN

func password(for ssid: String) throws -> String {
    let ssid = Data(ssid.utf8)
    var passwordQ: NSString? = nil
    let err = CWKeychainFindWiFiPassword(CWKeychainDomain.system, ssid, &passwordQ)
    guard err == noErr else {
        throw NSError(domain: NSOSStatusErrorDomain, code: Int(err), userInfo: nil)
    }
    return passwordQ! as String
}

This will prompt for an admin password. There’s no way to get this info without that prompt, for obvious security reasons.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"