1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OracleClient;
using System.Threading;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace apdu_comand
{
public partial class Form1 : Form
{
#region Win32
// WinSCard APIs to be imported.
[DllImport("WinScard.dll")]
static extern int SCardEstablishContext(uint dwScope,
IntPtr notUsed1,
IntPtr notUsed2,
out IntPtr hContext);
[DllImport("WinScard.dll")]
static extern int SCardReleaseContext(IntPtr hContext);
[DllImport("WinScard.dll")]
static extern int SCardConnect(IntPtr hContext,
string cReaderName,
uint dwShareMode,
uint dwPrefProtocol,
ref IntPtr phCard,
ref IntPtr ActiveProtocol);
[DllImport("WinScard.dll")]
static extern int SCardDisconnect(IntPtr hCard, int Disposition);
// [DllImport("WinScard.dll")]
// static extern int SCardListReaderGroups(IntPtr hContext,
// ref string cGroups,
// ref int nStringSize);
[DllImport("WinScard.dll", EntryPoint = "SCardListReadersA", CharSet = CharSet.Ansi)]
static extern int SCardListReaders(
IntPtr hContext,
byte[] mszGroups,
byte[] mszReaders,
ref UInt32 pcchReaders
);
[DllImport("WinScard.dll")]
internal static extern int SCardTransmit(
IntPtr phCard,
ref SCARD_IO_REQUEST pioSendPci,
byte[] pbSendBuffer,
UInt32 cbSendLength,
IntPtr pioRecvPci,
byte[] pbRecvBuffer,
ref int pcbRecvLength );
[DllImport("WINSCARD.DLL")]
internal static extern Int32 SCardReconnect(
IntPtr phCard,
uint dwShareMode,
uint dwPrefProtocol,
uint dwInitialization,
ref IntPtr ActiveProtocol);
[DllImport("winscard.dll", SetLastError = true)]
static extern Int32 SCardGetAttrib(
IntPtr phCard, // Reference value returned from SCardConnect
UInt32 dwAttrId, // Identifier for the attribute to get
byte[] pbAttr, // Pointer to a buffer that receives the attribute
ref int pcbAttrLen // Length of pbAttr in bytes
);
[DllImport("WinScard.dll")]
static extern int SCardFreeMemory(
IntPtr hContext,
string cResourceToFree);
/// <summary>
/// //-------------------------------------------------------------------------------------------------//
/// </summary>
#endregion
[StructLayout(LayoutKind.Sequential)]
public struct SCARD_IO_REQUEST
{
public int dwProtocol;
public int cbPciLength;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add(SmartCard.SmartCardInserted());
bool cardInserted = false;
List<string> readersList = new List<string>();
IntPtr hContext = IntPtr.Zero;
uint pcchReaders = 0;
long ret = 0;
int nullindex = -1;
char nullchar = (char)0;
ret=SCardEstablishContext(2, IntPtr.Zero, IntPtr.Zero, out hContext);
ret=SCardListReaders(hContext, null, null, ref pcchReaders);
byte[] mszReaders = new byte[pcchReaders];
ret = SCardListReaders(hContext, null, mszReaders, ref pcchReaders);
ASCIIEncoding ascii = new ASCIIEncoding();
string currbuff = ascii.GetString(mszReaders);
listBox1.Items.Add(currbuff);
int len = (int)pcchReaders;
if (len > 0)
{
while (currbuff[0] != nullchar)
{
nullindex = currbuff.IndexOf(nullchar); // Get null end character.
string reader = currbuff.Substring(0, nullindex);
readersList.Add(reader);
len = len - (reader.Length + 1);
currbuff = currbuff.Substring(nullindex + 1, len);
listBox1.Items.Add(reader);
}
}
IntPtr phCard = IntPtr.Zero;
IntPtr ActiveProtocol = IntPtr.Zero;
IntPtr pioRecvPci = IntPtr.Zero;
IntPtr pioSendPci = IntPtr.Zero;
SCARD_IO_REQUEST iorequest= new SCARD_IO_REQUEST();
iorequest.dwProtocol=1;
iorequest.cbPciLength = 8;
int result = 0;
// byte[] pbSendBuffer = {0xA0, 0xA4, 0x00, 0x00, 0x00};
byte[] pbSendBuffer = new byte[]{ 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x3F, 0x00/*, 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x2F, 0x77, 0xA0, 0xB0, 0x00, 0x00, 0x0F */ };
byte[] pbSendBuffer2 = new byte[] { 0xA0, 0xA4, 0x00, 0x00, 0x02, 0x2F, 0xE2 };
byte[] pbSendBuffer3 = new byte[] { 0xA0, 0xB0, 0x00, 0x00, 0x0A };
byte[] reciveBytes = new byte[255];
int pcbRecvLength = reciveBytes.Length;
byte[] pbRecvBuffer = new byte[255];
// byte pbRecvBuffer [20];
foreach (string readerName in readersList)
{
try
{
result = SCardConnect(hContext,
readerName,
2,
3,
ref phCard,
ref ActiveProtocol);
if (result == 0)
{
//---ATR---
const UInt32 SCARD_ATTR_ATR_STRING = 0x00090303;
byte[] pbAttr = new byte[255];
int pcbAttrLen = pbAttr.Length;
ret = SCardGetAttrib(phCard, SCARD_ATTR_ATR_STRING, pbAttr, ref pcbAttrLen);
listBox1.Items.Add("Apdu <--- ATR " + ret + " " + BitConverter.ToString(pbAttr, 0, pcbAttrLen));
//---------
cardInserted = true;
//--1
pcbRecvLength = pbRecvBuffer.Length;
ret = SCardTransmit(phCard,
ref iorequest,
pbSendBuffer,
(uint) pbSendBuffer.Length,
IntPtr.Zero,
pbRecvBuffer,
ref pcbRecvLength);
listBox1.Items.Add("Apdu ---> " + BitConverter.ToString(pbSendBuffer));
listBox1.Items.Add("Apdu <--- " + BitConverter.ToString(pbRecvBuffer, 0, pcbRecvLength));
//listBox1.Items.Add("Apdu <--- " + ret + " " + pcbRecvLength);
//ret = SCardReconnect(phCard, 2, 3, 0, ref ActiveProtocol);
//--2
ret = SCardTransmit(phCard,
ref iorequest,
pbSendBuffer2,
(uint)pbSendBuffer2.Length,
IntPtr.Zero,
pbRecvBuffer,
ref pcbRecvLength);
listBox1.Items.Add("Apdu ---> " + BitConverter.ToString(pbSendBuffer2));
listBox1.Items.Add("Apdu <--- " + BitConverter.ToString(pbRecvBuffer, 0, pcbRecvLength));
//listBox1.Items.Add("Apdu <--- " + ret + " " + pcbRecvLength);
//ret = SCardReconnect(phCard, 2, 3, 0, ref ActiveProtocol);
//listBox1.Items.Add("Apdu <--- " + ret );
//--3
ret = SCardTransmit(phCard,
ref iorequest,
pbSendBuffer3,
(uint)pbSendBuffer3.Length,
IntPtr.Zero,
pbRecvBuffer,
ref pcbRecvLength );
listBox1.Items.Add("Apdu ---> " + BitConverter.ToString(pbSendBuffer3));
listBox1.Items.Add("Apdu <--- " + BitConverter.ToString(pbRecvBuffer, 0, pcbRecvLength));
listBox1.Items.Add("Apdu <--- " + ret + " " + pcbRecvLength);
break;
}
}
finally
{
SCardDisconnect(phCard, 0);
}
}
} |