Vba 使用哪个APDU读取/写入MIFARE Ultralight NFC标签上的记录?
我正在创建一个VBA软件来读写NFC标签。我设法与标记通信以获得基本数据(例如,我获得了它的ID)。但是,当我为读取记录命令发送APDU时,它不起作用。我想我需要先发送一个SELECT FILE命令;但我想我需要正确的DF 我尝试了“00B2 01 05 00”作为读取记录命令,而“00A4 00 00”作为选择文件命令Vba 使用哪个APDU读取/写入MIFARE Ultralight NFC标签上的记录?,vba,apdu,mifare,smartcard-reader,pcsc,Vba,Apdu,Mifare,Smartcard Reader,Pcsc,我正在创建一个VBA软件来读写NFC标签。我设法与标记通信以获得基本数据(例如,我获得了它的ID)。但是,当我为读取记录命令发送APDU时,它不起作用。我想我需要先发送一个SELECT FILE命令;但我想我需要正确的DF 我尝试了“00B2 01 05 00”作为读取记录命令,而“00A4 00 00”作为选择文件命令 VBA Sub test() Dim hContext As Long Dim hCard As Long Dim retval As Long Dim readers As
VBA
Sub test()
Dim hContext As Long
Dim hCard As Long
Dim retval As Long
Dim readers As String * 256
Dim groups As String * 256
Dim activeprotocol As Long
Dim readerlen As Long
Dim scard_protocol_t0_or_t1 As Long
Dim scard_share_shared As Long
Dim APDU() As Byte
Dim recvbuf() As Byte
Dim recvbuff(256) As ByteArray
Dim recvlen As Long
Dim iosendreq As SCARD_IO_REQUEST
Dim iorecvreq As SCARD_IO_REQUEST
Dim bytRecvAttr As ByteArray
Dim LenAPDU As Long
scard_protocol_t0_or_t1 = 1 Or 2
scard_share_shared = 2 '2 1
retval = SCardEstablishContext(SCARD_SCOPE_USER, 0, 0, hContext)
If retval <> 0 Then
MsgBox "erreur n." & CStr(retval)
End If
readerlen = 256
retval = SCardListReaders(hContext, groups, readers, readerlen)
If retval <> 0 Then
MsgBox "erreur n. " & CStr(retval)
End If
Do While hCard = 0
retval = SCardConnect(hContext, readers, scard_share_shared, scard_protocol_t0_or_t1, hCard, activeprotocol)
DoEvents
Loop
If retval <> 0 Then MsgBox "erreur n. " & CStr(retval)
recvlen = 256
Do While State < 2
retval = SCardStatus(hCard, readers, readerlen, State, scard_protocol_t0_or_t1, recvbuff(0), recvlen)
If retval <> 0 Then MsgBox "Erreur n. " & CStr(retval)
DoEvents
Loop
'retval = SCardGetAttrib(hCard, SCARD_ATTR_ICC_PRESENCE, bytRecvAttr, lRecLen)
'If retval <> 0 Then MsgBox "Erreur n. " & CStr(retval)
iosendreq.dwProtocol = activeprotocol
iosendreq.dwPciLength = Len(iosendreq)
iorecvreq.dwProtocol = activeprotocol
iorecvreq.dwPciLength = Len(iorecvreq)
'recherche de file
ReDim APDU(7)
APDU(0) = &H0 'cla 80
APDU(1) = &HA4 'INs B2-read record
APDU(2) = &H4 'P1 1-PREMIER RECORD
APDU(3) = &H0 'P2 5-tous les records
APDU(4) = &H2 'longueur de data
APDU(5) = &H0 'a changer pour trouver ID du DF
APDU(6) = &H0 'a changer pour trouver ID du DF
APDU(7) = &H0 'Le
LenAPDU = (UBound(APDU) + 1)
If APDU(LenAPDU - 1) = 0 Then
ReDim recvbuf(255)
Else
ReDim recvbuf(APDU(LenAPDU - 1) + 2)
End If
recvlen = UBound(recvbuf) + 1
'this is a loop to test the different DFs
For x = 0 To 255
For y = 1 To 255
APDU(5) = x
APDU(6) = y
retval = SCardTransmit(hCard, iosendreq, APDU(0), LenAPDU, iorecvreq, recvbuf(0), recvlen)
If recvbuf(4) > 0 Or recvlen > 2 Then Exit For
Next
Next
Debug.Print Hex(recvbuf(0)) & "-" & Hex(recvbuf(1)) & "-" & Hex(recvbuf(2)) & "-" & Hex(recvbuf(3)) & "-" & Hex(recvbuf(4)) & "-" & Hex(recvbuf(5)) & "-" & Hex(recvbuf(6)) & "-" & Hex(recvbuf(7)) & " " & recvlen & " CLA:" & APDU(0) & " INS:" & APDU(1) & " P1:" & APDU(2) & " P2:" & APDU(3)
If retval <> 0 Then MsgBox "Erreur n. " & CStr(Hex(retval))
retval = SCardDisconnect(hCard, scad_leave_card)
If retval <> 0 Then MsgBox "erreur n. " & CStr(retval)
retval = SCardReleaseContext(hContext)
If retval <> 0 Then MsgBox "erreur n. " & CStr(retval)
End Sub
VBA
子测试()
Dim hContext尽可能长
暗卡一样长
暗淡的后退
将读卡器变暗为字符串*256
按字符串*256对组进行调整
Dim-activeprotocol尽可能长
黯淡的读者
Dim scard_协议_t0_或_t1长度相同
暗淡的疤痕\u共享\u共享的时间尽可能长
Dim APDU()作为字节
Dim recvbuf()作为字节
Dim recvbuff(256)与ByteArray相同
暗淡无光
根据SCARD\u IO\u请求调整iosendreq
根据SCARD\u IO\u请求调暗iorecvreq
暗淡的bytRecvAttr作为ByteArray
黯淡如长
scard_协议_t0_或_t1=1或2
疤痕共享=2'2 1
retval=scardesttablishContext(SCARD\u SCOPE\u USER,0,0,hContext)
如果retval为0,则
MsgBox“错误编号”和CStr(检索)
如果结束
readerlen=256
retval=SCardListReaders(hContext、Group、readers、readerlen)
如果retval为0,则
MsgBox“错误编号”和CStr(检索)
如果结束
当hCard=0时执行此操作
retval=SCardConnect(hContext、readers、scard\u share\u shared、scard\u protocol\u t0\u或\u t1、hCard、activeprotocol)
多芬特
环
如果retval为0,则MsgBox“错误号”和CStr(retval)
recvlen=256
当状态<2时执行此操作
retval=SCardStatus(hCard、读卡器、读卡器伦、状态、scard_协议t0_或_t1、recvbuff(0)、recvlen)
如果retval为0,则MsgBox“错误号”和CStr(retval)
多芬特
环
'retval=scardgettrib(hCard、SCARD_ATTR_ICC_PRESENCE、bytRecvAttr、lRecLen)
'如果返回0,则MsgBox“Erreur n.”和CStr(返回值)
iosendrq.dwProtocol=activeprotocol
iosendreq.DWPCileLength=Len(iosendreq)
iorecvreq.dwProtocol=activeprotocol
iorecvreq.DWPCileLength=Len(iorecvreq)
“重新归档
ReDim APDU(7)
APDU(0)=&H0'类别80
APDU(1)=&HA4'INs B2读取记录
APDU(2)=&H4'P1 1-PREMIER记录
APDU(3)=&H0'P2 5-tous les记录
APDU(4)=&H2'长数据
APDU(5)=&H0'a换流器倒槽ID du DF
APDU(6)=&H0'a换流器倒槽ID du DF
APDU(7)=&H0'Le
LenAPDU=(UBound(APDU)+1)
如果APDU(LenAPDU-1)=0,则
ReDim recvbuf(255)
其他的
ReDim recvbuf(APDU(LenAPDU-1)+2)
如果结束
recvlen=UBound(recvbuf)+1
'这是一个用于测试不同DFs的循环
对于x=0到255
对于y=1到255
APDU(5)=x
APDU(6)=y
retval=SCardTransmit(hCard、iosendreq、APDU(0)、LenAPDU、iorecvreq、recvbuf(0)、recvlen)
如果recvbuf(4)>0或recvlen>2,则退出
下一个
下一个
调试。打印十六进制(recvbuf(0))&“-”和十六进制(recvbuf(1))&“-”和十六进制(recvbuf(2))&“-”和十六进制(recvbuf(3))&“-”和十六进制(recvbuf(5))&“-”和十六进制(recvbuf(6))&“-”和十六进制(recvbuf(7))&“&”和recvlen&“CLA:”&“APDU(0)”和“INS:”&“APDU:”&“P1:”和“APDU(2)&“P2:”&“APDU(3)”
如果返回0,则MsgBox“Erreur n.”和CStr(十六进制(返回))
retval=SCardDisconnect(hCard、scad\U离开卡)
如果retval为0,则MsgBox“错误号”和CStr(retval)
retval=SCardReleaseContext(hContext)
如果retval为0,则MsgBox“错误号”和CStr(retval)
端接头
MIFARE Ultralight标记不使用APDU命令。因此,您不能向标记本身发送任何apdu,并且标记不支持文件(EF/DF/MF)的概念。相反,PC/SC(非接触式)智能卡读卡器通常提供一组APDU,可以发送给读卡器,读卡器将其转换为MIFARE Ultralight命令。读取器处理的这些APDU命令通常以类byte&HFF
开始
如果读卡器实现了标准PC/SC扩展以访问非接触式存储卡,则从Ultralight标签读取的读卡器APDU如下所示:
FF B0 00XX 10
^^
\-------- Block number
ReDim APDU(5)
APDU(0) = &HFF 'CLA 0xFF = PC/SC READER COMMAND
APDU(1) = &HB0 'INS 0xB0 = READ BINARY
APDU(2) = &H00 'P1 0x00
APDU(3) = &H00 'P2 block number (potentially also in P1 if more than 255)
APDU(4) = &H10 'Le 0x10 = 16 bytes (= 4 blocks) expected, which is the standard read size for the native Ultralight READ command