| |
|
|
Paul Glatz | Smartcards encima winscard.dll uso
Das Beispielprogramm liest el One-Time-Password de una YubiKey de.
cls
importdll("Winscard.dll", "")
def %NULL 0
def %SCARD_SCOPE_USER 0
def %SCARD_SHARE_SHARED 2
def %SCARD_AUTOALLOCATE -1
def %SCARD_LEAVE_CARD 0
def %SCARD_PROTOCOL_T0 1
def %SCARD_PROTOCOL_T1 2
Struct SCARD_IO_REQUEST = dwProtocol&, cbPciLength&
declarar memory SCARD_PCI_T0, SCARD_PCI_T1
Dim SCARD_PCI_T0, SCARD_IO_REQUEST
SCARD_PCI_T0.dwProtocol& = %SCARD_PROTOCOL_T0
SCARD_PCI_T0.cbPciLength& = SizeOf(SCARD_PCI_T0)
Dim SCARD_PCI_T1, SCARD_IO_REQUEST
SCARD_PCI_T1.dwProtocol& = %SCARD_PROTOCOL_T1
SCARD_PCI_T1.cbPciLength& = SizeOf(SCARD_PCI_T1)
declarar handle hContext, hCard/* Resource Manager Context *//* Verbindug a Smartcard */
declarar pointer pReaders, pSendPci/* Liste el Lesegeräte */
declarar memory mResponse/* Antwort el Karte */
declarar int dwReaders, dwProtocol, dwRecv/* Longitud de pReaders *//* Verwendetes Protokoll *//* Longitud el Antwort */
declarar cadena reader/* Gewählter Leser */
dim mResponse, 255
// Resource Manager Context redactar
err "SCardEstablishContext", SCardEstablishContext(%SCARD_SCOPE_USER, %NULL, %NULL, ADDR(hContext))
// Liste el verfügbaren Kartenleser
dwReaders = %SCARD_AUTOALLOCATE
err "SCardListReaders", SCardListReadersA(hContext, %NULL, ADDR(pReaders), ADDR(dwReaders))
// Name des a verwendenden Lesers
var cadena rdrSel = "SCM Microsystems"
// Leser suchen
imprimir "Gefundene Kartenleser:"
var int offs = 0
declarar cadena crdr// Aktueller Listeneintrag
mientras que (dwReaders - offs)
crdr = String$(pReaders, offs)// Listeneintrag lesen
casenot len(crdr) : romper// Ende erreicht
offs = offs + len(crdr) + 1// Offset para nachsten Eintrag
// Prüfen si gewählter Leser
if lower$(left$(crdr, len(rdrSel))) = lower$(rdrSel)
reader = crdr
imprimir "*", crdr
más
imprimir " ", crdr
endif
endwhile
// Liste liberación
err "SCardFreeMemory", SCardFreeMemory(hContext, pReaders)
if reader = ""
MessageBox "Kartenleser No se ha encontrado!", "", 0
end
endif
imprimir
imprimir "Verwende Kartenleser:", reader
imprimir
// Verbindung con Karte herstellen
err "SCardConnect", SCardConnectA(hContext, reader, %SCARD_SHARE_SHARED, %SCARD_PROTOCOL_T0 | %SCARD_PROTOCOL_T1, ADDR(hCard), ADDR(dwProtocol))
if dwProtocol = %SCARD_PROTOCOL_T0
pSendPci = SCARD_PCI_T0
más
pSendPci = SCARD_PCI_T1
endif
// Kommunikation con Karte
dwRecv = scTransmit("00 A4 04 00 07 D2 76 00 00 85 01 01 00", mResponse)// NDEF Applet wählen
dwRecv = scTransmit("00 A4 00 0C 02 E1 04", mResponse)// Expediente con NDEF Nachricht wählen
dwRecv = scTransmit("00 B0 00 00 00", mResponse)// Expediente lesen
// OTP de Nachricht
var cadena ndef = cadena$(mResponse, 25)
ndef = mid$(ndef, 1, len(ndef) - 1)
// OTP ausgeben y Zwischenablage
imprimir "OTP:", ndef
ClearClip
PutClip ndef
// Verbindung trennen
err "SCardDisconnect", SCardDisconnect(hCard, %SCARD_LEAVE_CARD)
// Resource Manager Context liberación
err "SCardReleaseContext", SCardReleaseContext(hContext)
disponer SCARD_PCI_T0
disponer SCARD_PCI_T1
waitend
// Befehl a Karte senden
proc scTransmit
Parámetros cadena hexApdu, memory response
hexApdu = translate$(hexApdu, " ", "")
if (len(hexApdu) mod 2)
MessageBox "Ungültiger Hex String", "", 0
end
endif
declarar memory apdu
dim apdu, len(hexApdu)/2
declarar int i
for i, 0, (len(hexApdu)/2) - 1
byte apdu, i = val("$" + mid$(hexApdu, 1 + 2 * i, 2))
endfor
clear response
var int recv = sizeOf(response)
imprimir "Sende: ", hexString(apdu, sizeOf(apdu))
err "SCardTransmit", SCardTransmit(hCard, pSendPci, apdu, sizeOf(apdu), %NULL, response, ADDR(recv))
imprimir "Antwort:", hexString(response, recv)
imprimir
disponer apdu
volver recv
ENDPROC
proc hexString
Parámetros memory buffer, int size
declarar cadena out
declarar int i
for i, 0, size - 1
out = out + right$("00" + hex$(byte(buffer, i)), 2) + " "
endfor
volver out
ENDPROC
proc err
parámetros cadena func, long resultado
if resultado
MessageBox func + ": " + str$(resultado), "", 0
end
endif
ENDPROC
|
|
|
| |
|
|