使用VB讀取自然人評証卡號

Imports System
Imports System.Text
Imports System.Runtime.InteropServices
Class CitizenDigitalCertificateCardNoReader
Public Structure SCARD_IO_REQUEST
Public dwProtocol As Integer
Public cbPciLength As Integer
End Structure
'引用 PC/SC(Personal Computer/Smart Card) API WinScard.dll
_
Public Shared Function SCardEstablishContext(ByVal dwScope As UInteger, ByVal nNotUsed1 As Integer, ByVal nNotUsed2 As Integer, ByRef phContext As Integer) As Integer
End Function
_
Public Shared Function SCardReleaseContext(ByVal phContext As Integer) As Integer
End Function
_
Public Shared Function SCardConnect(ByVal hContext As Integer, ByVal cReaderName As String, ByVal dwShareMode As UInteger, ByVal dwPrefProtocol As UInteger, ByRef phCard As Integer, ByRef ActiveProtocol As Integer) As Integer
End Function
_
Public Shared Function SCardDisconnect(ByVal hCard As Integer, ByVal Disposition As Integer) As Integer
End Function
_
Public Shared Function SCardListReaders(ByVal hContext As Integer, ByVal cGroups As String, ByRef cReaderLists As String, ByRef nReaderCount As Integer) As Integer
End Function
_
Public Shared Function SCardTransmit(ByVal hCard As Integer, ByRef pioSendPci As SCARD_IO_REQUEST, ByVal pbSendBuffer As Byte(), ByVal cbSendLength As Integer, ByRef pioRecvPci As SCARD_IO_REQUEST, ByRef pbRecvBuffer As Byte, _
ByRef pcbRecvLength As Integer) As Integer
End Function
Private Shared Sub Main(ByVal args As String())
Dim ContextHandle As Integer = 0, CardHandle As Integer = 0, ActiveProtocol As Integer = 0, ReaderCount As Integer = -1
Dim ReaderList As String = String.Empty
'讀卡機名稱列表
Dim SendPci As SCARD_IO_REQUEST, RecvPci As SCARD_IO_REQUEST
Dim SelEFAPDU As Byte() = {0, 164, 2, 12, 2, 254, 20}
'Select Elementary File 的 APDU
Dim ReadSNAPDU As Byte() = {0, 176, 0, 0, 16}
'由offset 0 讀取 16 Dim Binary As 位 Dim APDU As 資料的
Dim SelEFRecvBytes As Byte() = New Byte(1) {}
'應回 90 00
Dim SelEFRecvLength As Integer = 2
Dim SNRecvBytes As Byte() = New Byte(17) {}
'接收卡號的 Byte Array
Dim SnRecvLength As Integer = 18
'建立 Smart Card API
If SCardEstablishContext(0, 0, 0, ContextHandle) = 0 Then
If SCardListReaders(ContextHandle, Nothing, ReaderList, ReaderCount) = 0 Then
If SCardConnect(ContextHandle, ReaderList, 1, 2, CardHandle, ActiveProtocol) = 0 Then
'列出可用的 Smart Card 讀卡機
'建立 Smart Card 連線
SendPci.dwProtocol = RecvPci.dwProtocol = ActiveProtocol
SendPci.cbPciLength = RecvPci.cbPciLength = 8
'下達 Select FE14 檔的 APDU
If SCardTransmit(CardHandle, SendPci, SelEFAPDU, SelEFAPDU.Length, RecvPci, SelEFRecvBytes(0), _
SelEFRecvLength) = 0 Then
If SCardTransmit(CardHandle, SendPci, ReadSNAPDU, ReadSNAPDU.Length, RecvPci, SNRecvBytes(0), _
SnRecvLength) = 0 Then
Console.WriteLine("自然人憑證卡號為{0}", Encoding.[Default].GetString(SNRecvBytes, 0, 16))
End If
'下達讀取卡號指令
End If
End If
End If
End If
Console.ReadKey()
End Sub
End Class

留言

Unknown寫道…
您好,最近我也碰上需要讀取自然人憑證的問題,請問您的方法是否可以讀到憑證的其它資料?例如:輸入pin碼後讀到姓名資料之類的?方便把vb的程式寄給我嗎?謝謝!a0910390618@gmail.com

這個網誌中的熱門文章

使用VB讀取健保卡基本資料