【发布时间】:2019-10-10 18:55:28
【问题描述】:
我正在创建一个 VBA 软件来读取和写入 NFC 标签。我设法与标签通信以获取基本数据(例如,我得到了它的 ID)。但是当我为 READ RECORD 命令发送 APDU 时,它不起作用。我想我需要先发送一个 SELECT FILE 命令;但我认为我需要正确的 DF。
我为 READ RECORD 命令尝试了“00 B2 01 05 00”,为 SELECT FILE 命令尝试了“00 A4 00 00 00”。
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 apdu mifare smartcard-reader pcsc