Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.86 KB | None | 0 0
  1. Imports System
  2. Imports System.Text
  3. Imports System.Runtime.InteropServices
  4.  
  5. Public Class ReadCDCCardT
  6.  
  7.     Private Structure SCARD_IO_REQUEST
  8.         Public dwProtocol As Integer
  9.         Public cbPciLength As Integer
  10.     End Structure
  11.  
  12.     '引用 PC/SC(Personal Computer/Smart Card) API WinScard.dll
  13.  
  14.     <DllImport("WinScard.dll")> _
  15.     Private Shared Function SCardEstablishContext(ByVal dwScope As UInteger, ByVal nNotUsed1 As Integer, ByVal nNotUsed2 As Integer, ByRef phContext As Integer) As Integer
  16.     End Function
  17.  
  18.     <DllImport("WinScard.dll")> _
  19.     Private Shared Function SCardReleaseContext(ByVal phContext As Integer) As Integer
  20.     End Function
  21.  
  22.     <DllImport("WinScard.dll")> _
  23.     Private 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
  24.     End Function
  25.  
  26.     <DllImport("WinScard.dll")> _
  27.     Private Shared Function SCardDisconnect(ByVal hCard As Integer, ByVal Disposition As Integer) As Integer
  28.     End Function
  29.  
  30.     <DllImport("WinScard.dll")> _
  31.     Private Shared Function SCardListReaders(ByVal hContext As Integer, ByVal cGroups As String, ByRef cReaderLists As String, ByRef nReaderCount As Integer) As Integer
  32.     End Function
  33.  
  34.     <DllImport("WinScard.dll")> _
  35.     Private 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
  36.     End Function
  37.  
  38.     '''
  39.     ''' 取得自然人憑證的卡號
  40.     '''
  41.     '''
  42.     '''
  43.     Public Function GetCardNumber() As String
  44.         Dim ContextHandle As Integer = 0, CardHandle As Integer = 0, ActiveProtocol As Integer = 0, ReaderCount As Integer = -1
  45.         Dim ReaderList As String = String.Empty '讀卡機名稱列表
  46.         Dim SendPci, RecvPci As SCARD_IO_REQUEST
  47.  
  48.         'SCardTransmit (handle 0xEA0A0000):
  49.         'transmitted:
  50.         '80 A4 00 00 02 3F 00
  51.         'received:
  52.         '90 00
  53.         Dim SelEFAPDU_1() As Byte = {&H80, &HA4, &H0, &H0, &H2, &H3F, &H0} 'Select Elementary File 的 APDU
  54.         'SCardTransmit (handle 0xEA0A0000):
  55.         'transmitted:
  56.         '80 A4 00 00 02 09 00
  57.         'received:
  58.         '90 00
  59.         Dim SelEFAPDU_2() As Byte = {&H80, &HA4, &H0, &H0, &H2, &H9, &H0} 'Select Elementary File 的 APDU
  60.  
  61.         'SCardTransmit (handle 0xEA0A0000):
  62.         'transmitted:
  63.         '80 A4 00 00 02 09 03
  64.         'received:
  65.         '90 00
  66.         Dim SelEFAPDU_3() As Byte = {&H80, &HA4, &H0, &H0, &H2, &H9, &H3} 'Select Elementary File 的 APDU
  67.  
  68.         'SCardTransmit (handle 0xEA0A0000):
  69.         'transmitted:
  70.         '80 B0 00 00 10
  71.         'received:
  72.         '54 50 30 30 30 30 30 30 30 31 36 31 31 31 31 31 90 00
  73.         Dim ReadSNAPDU() As Byte = {&H80, &HB0, &H0, &H0, &H10} '由offset 0 讀取 0x10位 Binary 資料的 APDU
  74.  
  75.         Dim SelEFRecvBytes(1) As Byte '應回 90 00
  76.         Dim SelEFRecvLength As Integer = 2
  77.         Dim SNRecvBytes(17) As Byte '接收卡號的 Byte Array
  78.         Dim SnRecvLength As Integer = 18
  79.  
  80.         '建立 Smart Card API
  81.         If SCardEstablishContext(0, 0, 0, ContextHandle) = 0 Then
  82.  
  83.             '列出可用的 Smart Card 讀卡機
  84.             If SCardListReaders(ContextHandle, Nothing, ReaderList, ReaderCount) = 0 Then
  85.  
  86.                 '建立 Smart Card 連線
  87.                 If SCardConnect(ContextHandle, ReaderList, 1, 2, CardHandle, ActiveProtocol) = 0 Then
  88.  
  89.                     RecvPci.dwProtocol = ActiveProtocol
  90.                     SendPci.dwProtocol = RecvPci.dwProtocol
  91.  
  92.                     RecvPci.cbPciLength = 8
  93.                     SendPci.cbPciLength = RecvPci.cbPciLength
  94.  
  95.                     '下達 Select FE14 檔的 APDU
  96.                     If SCardTransmit(CardHandle, SendPci, SelEFAPDU_1, SelEFAPDU_1.Length, RecvPci, SelEFRecvBytes(0), SelEFRecvLength) = 0 Then
  97.                         If SCardTransmit(CardHandle, SendPci, SelEFAPDU_2, SelEFAPDU_2.Length, RecvPci, SelEFRecvBytes(0), SelEFRecvLength) = 0 Then
  98.                             If SCardTransmit(CardHandle, SendPci, SelEFAPDU_3, SelEFAPDU_3.Length, RecvPci, SelEFRecvBytes(0), SelEFRecvLength) = 0 Then
  99.  
  100.                                 '下達讀取卡號指令
  101.                                 If SCardTransmit(CardHandle, SendPci, ReadSNAPDU, ReadSNAPDU.Length, RecvPci, SNRecvBytes(0), SnRecvLength) = 0 Then
  102.                                     Return Encoding.Default.GetString(SNRecvBytes, 0, 16)
  103.                                 End If
  104.                             End If
  105.                         End If
  106.                     End If
  107.                 End If
  108.             End If
  109.         End If
  110.         Return ""
  111.     End Function
  112.  
  113. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement