Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

APDU Commands For Smart Card

Status
Not open for further replies.

Gwena

Programmer
Mar 11, 2004
42
IL
Here are some Smart Card Functions...
Smart Card Drivers Can be found in
Code:
Option Explicit
Public Status As String
Public L As Long
Public Declare Function SCardComand Lib "SCARD32" (Handle As Long, ByVal Cmd As String, CmdLen As Long, ByVal DataIn As String, DataInLen As Long, ByVal DataOut As String, DataOutLen As Long) As Long

'Checking Card Status
'---------------------
'Active=Card is in the Terminal

Public Function CardStatus() As String
  
  Status = String(200, 0)
  L = SCardComand(0, "Card,Info,Status", 0, 0, 0, Status, 200)
  Status = Left(Status, InStr(Status, Chr(0)) - 1)
     

 CardStatus = Status
End Function


'Changing Smart Card PinCode
'----------------------------
'Default PinCode=ffff

Public Function ChangePin(oldPin As String, newPin As String) As Integer
Dim Str As String

  Str = "Card,MemChangePin," & oldPin & "," & newPin

  L = SCardComand(0, "Card,MemChangePin," & oldPin & "," & newPin, 0, 0, 0, gStatus, 200)

ChangePin = L
End Function


'Writing on the Card
'--------------------
'First You need to Verify the PinCode by using VerifyPin Fucntion

Public Function WriteBin(StartPos As Integer, data As String) As Integer
    Dim sTmp As String
    Dim Buf As String
    Dim Block As String
    Buf = String(255, 0)
    Buf = data
    '==============================
    If (StartPos > 255) Then
        StartPos = StartPos - 256
        Block = Chr(&H1)
    Else
        Block = Chr(&H0)
    End If
    '===============================
    
 sTmp = String(255, 0)
 sTmp = Chr(&H0) & Chr(&HD0) & Block & Chr(StartPos) & Chr(Len(data)) & Buf
 
 L = SCardComand(0, "Card,APDU", 0, sTmp, Len(sTmp), Status, 255)
 WriteBin = L
End Function

'Reading form The Card
'------------------------
Public Function ReadBin(StartPos As Integer, ByteToRead As Integer) As String

    Dim sTmp As String
    Dim Buf As String
    Dim Block As String
    Buf = String(255, 0)
    sTmp = String(200, 0)
    
    If (StartPos > 255) Then
        StartPos = StartPos - 255
        Block = Chr(&H1)
    Else
        Block = Chr(&H0)
    End If
   
    sTmp = Chr(&H0) & Chr(&HB0) & Block & Chr(StartPos) & Chr(ByteToRead)
    L = SCardComand(0, "Card,APDU", 0, sTmp, Len(sTmp), Buf, 255)
    ReadBin = Buf
    
End Function

'Verifing PinCode
'-----------------
'Not APDU Command
Public Function VerifyPin(pin As String) As Integer
  L = SCardComand(0, "Card,MemVerifyPin," & pin, 0, 0, 0, gStatus, 200)
  VerifyPin = L
End Function
 
I forgot to mention that the function applies to memory cards. CPU cards' function are little different
 
Is this code from a vendor?
Could it be covered under a copyright?

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
This code is not from a vendor
I wrote the functions with the help of some of my friends based of standard Smard Card Commands (You can find them anywhere).
In order to work with smard card you need drivers and you need a device. I got my drivers from the site I'd mentioned but not the device
 
If you want I can post the commands
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top