使用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 SelectAPDU As Byte() = {0, 164, 4, 0, 16, 209, 88, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 0}
Dim ReadProfileAPDU As Byte() = {0, 202, 17, 0, 2, 0, 0}
Dim SelectRecvBytes As Byte() = New Byte(1) {}
'應回 90 00
Dim SelectRecvLength As Integer = 2
Dim ProfileRecvBytes As Byte() = New Byte(58) {}
'接收Profile的 Byte Array
Dim ProfileRecvLength As Integer = 59
'建立 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 Profile 檔的 APDU
If SCardTransmit(CardHandle, SendPci, SelectAPDU, SelectAPDU.Length, RecvPci, SelectRecvBytes(0), SelectRecvLength) = 0 Then
'下達讀取Profile指令
If SCardTransmit(CardHandle, SendPci, ReadProfileAPDU, ReadProfileAPDU.Length, RecvPci, ProfileRecvBytes(0), ProfileRecvLength) = 0 Then
Console.WriteLine("健保卡ID:{0}" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " 姓名:{1}" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " 身份証字號:{2}" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " 生日:{3}/{4}/{5}" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " 姓別:{6} " & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " 發卡日期:{7}/{8}/{9}", Encoding.[Default].GetString(ProfileRecvBytes, 0, 12), Encoding.[Default].GetString(ProfileRecvBytes, 12, 6), Encoding.[Default].GetString(ProfileRecvBytes, 32, 10), Encoding.[Default].GetString(ProfileRecvBytes, 43, 2), Encoding.[Default].GetString(ProfileRecvBytes, 45, 2), _
Encoding.[Default].GetString(ProfileRecvBytes, 47, 2), Encoding.[Default].GetString(ProfileRecvBytes, 49, 1), Encoding.[Default].GetString(ProfileRecvBytes, 51, 2), Encoding.[Default].GetString(ProfileRecvBytes, 53, 2), Encoding.[Default].GetString(ProfileRecvBytes, 55, 2))
End If
End If
End If
End If
End If
Console.ReadKey()
End Sub
End Class

留言

這個網誌中的熱門文章

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