| |
|
|
Paul Glatz | Smartcards sur winscard.dll verwenden
cela Beispielprogramm liest cela One-Time-Password aus einem YubiKey aus.
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&
declare memory SCARD_PCI_T0, SCARD_PCI_T1
Faible SCARD_PCI_T0, SCARD_IO_REQUEST
SCARD_PCI_T0.dwProtocol& = %SCARD_PROTOCOL_T0
SCARD_PCI_T0.cbPciLength& = SizeOf(SCARD_PCI_T0)
Faible SCARD_PCI_T1, SCARD_IO_REQUEST
SCARD_PCI_T1.dwProtocol& = %SCARD_PROTOCOL_T1
SCARD_PCI_T1.cbPciLength& = SizeOf(SCARD_PCI_T1)
declare handle hContext, hCard/* Resource Manager Context *//* Verbindug zur Smartcard */
declare pointer pReaders, pSendPci/* liste qui Lesegeräte */
declare memory mResponse/* Antwort qui carte */
declare int dwReaders, dwProtocol, dwRecv/* Longueur de pReaders *//* Verwendetes Protokoll *//* Longueur qui Antwort */
declare string reader/* Gewählter Leser */
dim mResponse, 255
// Resource Manager Context erstellen
err "SCardEstablishContext", SCardEstablishContext(%SCARD_SCOPE_USER, %NULL, %NULL, ADDR(hContext))
// liste qui verfügbaren Kartenleser
dwReaders = %SCARD_AUTOALLOCATE
err "SCardListReaders", SCardListReadersA(hContext, %NULL, ADDR(pReaders), ADDR(dwReaders))
// nom des trop verwendenden Lesers
var string rdrSel = "SCM Microsystems"
// Leser chercher
imprimer "Gefundene Kartenleser:"
var int offs = 0
declare string crdr// Aktueller Listeneintrag
tandis que (dwReaders - offs)
crdr = String$(pReaders, offs)// Listeneintrag lesen
casenot len(crdr) : pause// Ende erreicht
offs = offs + len(crdr) + 1// Offset pour nachsten Eintrag
// vérifier si gewählter Leser
si lower$(left$(crdr, len(rdrSel))) = lower$(rdrSel)
reader = crdr
imprimer "*", crdr
d'autre
imprimer " ", crdr
endif
endwhile
// liste freigeben
err "SCardFreeMemory", SCardFreeMemory(hContext, pReaders)
si reader = »
MessageBox "Kartenleser pas trouvé!", », 0
end
endif
imprimer
imprimer "Verwende Kartenleser:", reader
imprimer
// liaison avec carte herstellen
err "SCardConnect", SCardConnectA(hContext, reader, %SCARD_SHARE_SHARED, %SCARD_PROTOCOL_T0 | %SCARD_PROTOCOL_T1, ADDR(hCard), ADDR(dwProtocol))
si dwProtocol = %SCARD_PROTOCOL_T0
pSendPci = SCARD_PCI_T0
d'autre
pSendPci = SCARD_PCI_T1
endif
// communication avec carte
dwRecv = scTransmit("00 A4 04 00 07 D2 76 00 00 85 01 01 00", mResponse)// NDEF Applet choisir
dwRecv = scTransmit("00 A4 00 0C 02 E1 04", mResponse)// Dossier avec NDEF nouvelle choisir
dwRecv = scTransmit("00 B0 00 00 00", mResponse)// Dossier lesen
// OTP aus nouvelle
var string ndef = string$(mResponse, 25)
ndef = mid$(ndef, 1, len(ndef) - 1)
// OTP ausgeben et dans Zwischenablage
imprimer "OTP:", ndef
ClearClip
PutClip ndef
// liaison trennen
err "SCardDisconnect", SCardDisconnect(hCard, %SCARD_LEAVE_CARD)
// Resource Manager Context freigeben
err "SCardReleaseContext", SCardReleaseContext(hContext)
dispose SCARD_PCI_T0
dispose SCARD_PCI_T1
waitend
// Befehl à carte senden
proc scTransmit
Paramètres string hexApdu, memory response
hexApdu = translate$(hexApdu, " ", »)
si (len(hexApdu) mod 2)
MessageBox "Ungültiger Hex String", », 0
end
endif
declare memory apdu
dim apdu, len(hexApdu)/2
declare 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)
imprimer "Sende: ", hexString(apdu, sizeOf(apdu))
err "SCardTransmit", SCardTransmit(hCard, pSendPci, apdu, sizeOf(apdu), %NULL, response, ADDR(recv))
imprimer "Antwort:", hexString(response, recv)
imprimer
dispose apdu
return recv
endproc
proc hexString
Paramètres memory buffer, int size
declare string out
declare int i
for i, 0, size - 1
out = out + right$("00" + hex$(byte(buffer, i)), 2) + " "
endfor
return out
endproc
proc err
parameters string func, long result
si result
MessageBox func + ": " + str$(result), », 0
end
endif
endproc
|
|
|
| |
|
|