gpt4 book ai didi

ios - 在 Swift 中使用 application/vnd.wfa.wsc 创建 NDEF WiFi 记录

转载 作者:行者123 更新时间:2023-11-29 11:26:55 25 4
gpt4 key购买 nike

我正在尝试在 Swift 中创建一个 application/vnd.wfa.wsc 负载。我在这里找到了这篇适用于 Android 的帖子: Creating an NDEF WiFi record using application/vnd.wfa.wsc in Android

我坚持创建我想写在 NFC 标签上的有效载荷。我尝试将 java 代码转换为 swift 并将有效负载写入 NFC 标签,但我无法使用 Android 手机读取数据。

let VERSION: [UInt8] = [0x10, 0x4A]
let CREDENTIAL: [UInt8] = [0x10, 0x0e]
let AUTH_TYPE: [UInt8] = [0x10, 0x03]
let CRYPT_TYPE: [UInt8] = [0x10, 0x0F]
let MAC_ADDRESS: [UInt8] = [0x10, 0x20]
let NETWORK_IDX: [UInt8] = [0x10, 0x26]
let NETWORK_KEY: [UInt8] = [0x10, 0x27]
let NETWORK_NAME: [UInt8] = [0x10, 0x45]
let OOB_PASSWORD: [UInt8] = [0x10, 0x2C]
let VENDOR_EXT: [UInt8] = [0x10, 0x49]
let VENDOR_WFA: [UInt8] = [0x00, 0x37, 0x2A]
let VERSION2: [UInt8] = [0x00]
let KEY_SHAREABLE: [UInt8] = [0x02]
let AUTH_OPEN: [UInt8] = [0x00, 0x01]
let AUTH_WPA_PERSONAL: [UInt8] = [0x00, 0x02]
let AUTH_SHARED: [UInt8] = [0x00, 0x04]
let AUTH_WPA_ENTERPRISE: [UInt8] = [0x00, 0x08]
let AUTH_WPA2_ENTERPRISE: [UInt8] = [0x00, 0x10]
let AUTH_WPA2_PERSONAL: [UInt8] = [0x00, 0x20]
let AUTH_WPA_WPA2_PERSONAL: [UInt8] = [0x00, 0x22]
let CRYPT_NONE: [UInt8] = [0x00, 0x01]
let CRYPT_WEP: [UInt8] = [0x00, 0x02]
let CRYPT_TKIP: [UInt8] = [0x00, 0x04]
let CRYPT_AES: [UInt8] = [0x00, 0x08]
let CRYPT_AES_TKIP: [UInt8] = [0x00, 0x0C]

let credential: [UInt8] = [0x00, 0x36]
let idx: [UInt8] = [0x00, 0x01, 0x01]

let ssid = "TestWifi"
let password = "Test12345"

let ssidBytes: [UInt8] = Array(ssid.utf8)
let passwordBytes: [UInt8] = Array(password.utf8)
let authBytes: [UInt8] = Array("WPA PSK".utf8)

let lengthSSID: [UInt8] = [UInt8(ssid.lengthOfBytes(using: .utf8))]
let lengthPassword: [UInt8] = [UInt8(password.lengthOfBytes(using: .utf8))]

let cred: [UInt8] = CREDENTIAL + credential
let index: [UInt8] = NETWORK_IDX + idx
let network: [UInt8] = NETWORK_NAME + lengthSSID + ssidBytes
let auth: [UInt8] = AUTH_TYPE + AUTH_WPA_PERSONAL + authBytes
let crypt: [UInt8] = CRYPT_TYPE + CRYPT_WEP + CRYPT_AES_TKIP
let netw: [UInt8] = NETWORK_KEY + lengthPassword + passwordBytes

let foo1 = cred + index
let foo2 = network + auth
let foo3 = crypt + netw

let payload = foo1 + foo2 + foo3

let type = Data(base64Encoded: "application/vnd.wfa.wsc".base64Encoded()!)!
let identifier = Data(base64Encoded: "".base64Encoded()!)!
let payloadData = Data(bytes: payload, count: payload.count)
let payload = NFCNDEFPayload(format: .media, type: type, identifier: identifier, payload: payloadData)

谁能指出我做错了什么?

最佳答案

1) Credential TLV 由子 TLV[ enter image description here

以下是创建负载的 Swift 代码。 TODO:(1)我可能必须检查所需的字节序并重新排序各个字段(2)您可能需要检查此 API 的其他参数

import Foundation
import Glibc

/* TotalLen is count of all bytes in parenthesis.

CREDENTIAL + totallen +
(
NETWORK_IDX + Idxlen + idx
NETWORK_NAME + Namelen + ssidBytes
AUTH_TYPE + Authlen + AUTH_WPA_PERSONAL
CRYPT_TYPE + Cryptlen + AUTH_MIXED
NETWORK_KEY + Keylen + passwordBytes
MAC_ADDRESS + Maclen + addr
)

0x100E + **TotalLen** +
(0x1026 + 0x0001 + 01 +
0x1045 + 0x0008 + TestWifi +
0x1003 + 0x0002 + 0x0002 +
0x100F + 0x0002 + 0x000C (Mixed mode supporting TKIP, AES) +
0x1027 + 0x0009 + Test12345 +
0x1020 + 0x0006 + address) */

//Attributes
let VERSION: [UInt8] = [0x10, 0x4A]
let CREDENTIAL: [UInt8] = [0x10, 0x0e]
let AUTH_TYPE: [UInt8] = [0x10, 0x03]
let CRYPT_TYPE: [UInt8] = [0x10, 0x0F]
let MAC_ADDRESS: [UInt8] = [0x10, 0x20]
let NETWORK_IDX: [UInt8] = [0x10, 0x26]
let NETWORK_KEY: [UInt8] = [0x10, 0x27]
let NETWORK_NAME: [UInt8] = [0x10, 0x45]
let OOB_PASSWORD: [UInt8] = [0x10, 0x2C]
let VENDOR_EXT: [UInt8] = [0x10, 0x49]
let VENDOR_WFA: [UInt8] = [0x00, 0x37, 0x2A]
let VERSION2: [UInt8] = [0x00]
let KEY_SHAREABLE: [UInt8] = [0x02]
let AUTH_OPEN: [UInt8] = [0x00, 0x01]
let AUTH_WPA_PERSONAL: [UInt8] = [0x00, 0x02]
let AUTH_SHARED: [UInt8] = [0x00, 0x04]
let AUTH_WPA_ENTERPRISE: [UInt8] = [0x00, 0x08]
let AUTH_MIXED: [UInt8] = [0x00, 0x0C]
let AUTH_WPA2_ENTERPRISE: [UInt8] = [0x00, 0x10]
let AUTH_WPA2_PERSONAL: [UInt8] = [0x00, 0x20]
let AUTH_WPA_WPA2_PERSONAL: [UInt8] = [0x00, 0x22]
let CRYPT_NONE: [UInt8] = [0x00, 0x01]
let CRYPT_WEP: [UInt8] = [0x00, 0x02]
let CRYPT_TKIP: [UInt8] = [0x00, 0x04]
let CRYPT_AES: [UInt8] = [0x00, 0x08]
let CRYPT_AES_TKIP: [UInt8] = [0x00, 0x0C]

//Values
let idx: [UInt8] = [0x01]
let ssid = "TestWifi"
let ssidBytes: [UInt8] = Array(ssid.utf8)
let password = "Test12345"
let passwordBytes: [UInt8] = Array(password.utf8)
let addr: [UInt8] = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66]

let authBytes: [UInt8] = Array("WPA PSK".utf8)

//Lengths
let Idxlen: [UInt8] = [0x00, 0x01]
var temp = ssid.lengthOfBytes(using: .utf8)
let Namelen: [UInt8] = [UInt8(temp >> 8), UInt8(temp)]
let Authlen: [UInt8] = [0x00, 0x02]
let Cryptlen: [UInt8] = [0x00, 0x02]
temp = password.lengthOfBytes(using: .utf8)
let Keylen: [UInt8] = [UInt8(temp >> 8), UInt8(temp)]
let Maclen: [UInt8] = [0x00, 0x06]

//Packet formation
let id = NETWORK_IDX + Idxlen + idx
let name = NETWORK_NAME + Namelen + ssidBytes
let aut = AUTH_TYPE + Authlen + AUTH_WPA_PERSONAL
let cry = CRYPT_TYPE + Cryptlen + AUTH_MIXED
let key = NETWORK_KEY + Keylen + passwordBytes
let mac = MAC_ADDRESS + Maclen + addr
var payload: [UInt8] = id + name + aut + cry + key + mac

//Prepend Credential Attribute header
let totallen = payload.count
var totallenBytes = [UInt8(totallen >> 8), UInt8(totallen)]
var cred = CREDENTIAL + totallenBytes
payload.insert(contentsOf:cred, at: 0)
print(payload)```

关于ios - 在 Swift 中使用 application/vnd.wfa.wsc 创建 NDEF WiFi 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57832849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com