private static void Main() { // Establish Smartcard context using (var context = new SCardContext()) { context.Establish(SCardScope.System); var readerNames = context.GetReaders(); if (readerNames == null || readerNames.Length < 1) { Console.WriteLine("You need at least one reader in order to run this example."); Console.ReadKey(); return; } var readerName = ChooseReader(readerNames); if (readerName == null) { return; } using (var isoReader = new IsoReader(context, readerName, SCardShareMode.Shared, SCardProtocol.Any, false)) { // Build a GET CHALLENGE command var apdu = new CommandApdu(IsoCase.Case2Short, isoReader.ActiveProtocol) { CLA = 0x00, // Class Instruction = InstructionCode.GetChallenge, P1 = 0x00, // Parameter 1 P2 = 0x00, // Parameter 2 Le = 0x08 // Expected length of the returned data }; Console.WriteLine("Send APDU with \"GET CHALLENGE\" command: {0}", BitConverter.ToString(apdu.ToArray())); var response = isoReader.Transmit(apdu); Console.WriteLine("SW1 SW2 = {0:X2} {1:X2}", response.SW1, response.SW2); if (!response.HasData) { Console.WriteLine("No data. (Card does not understand \"GET CHALLENGE\")"); } else { var data = response.GetData(); Console.WriteLine("Challenge: {0}", BitConverter.ToString(data)); } } } Console.ReadKey(); }
public byte[] GetCardId(IsoReader reader) { var command = new CommandApdu(IsoCase.Case2Short, SCardProtocol.Any) { CLA = 0xFF, Instruction = InstructionCode.GetData, P1 = 0x00, P2 = 0x00, Le = 0x00 }; var response = reader.Transmit(command); return response.GetData(); }
private void WriteAllCardBytes(IsoReader isoReader, byte[] bytes, int packetSize) { var bytesToWrite = new List<byte>(bytes); //while (bytesToWrite.Count < 38 * 4) // bytesToWrite.Add(0x00); while (bytesToWrite.Count % packetSize != 0) bytesToWrite.Add(0x00); for (int i = 0; i < bytesToWrite.Count / packetSize; i++) { var updateBinaryCmd = new CommandApdu(IsoCase.Case3Short, SCardProtocol.Any) { CLA = 0xFF, Instruction = InstructionCode.UpdateBinary, P1 = 0x00, P2 = (byte)((16 / packetSize) + i), Data = bytesToWrite.Skip(i * packetSize).Take(packetSize).ToArray() }; var response = isoReader.Transmit(updateBinaryCmd); Console.WriteLine("UpdateBinary: {0},{1}", response.SW1, response.SW2); } }
private byte[] GetAllCardBytes(IsoReader reader, int packetSize) { try { var firstDataBlock = 16 / packetSize; var readSize = 16; var bytesToRead = 0; var buffer = new List<byte>(); while (true) { var blockToRead = (byte)(firstDataBlock + (buffer.Count / packetSize)); var readBinaryCmd = new CommandApdu(IsoCase.Case2Short, SCardProtocol.Any) { CLA = 0xFF, Instruction = InstructionCode.ReadBinary, P1 = 0x00, P2 = blockToRead, Le = readSize }; var response = reader.Transmit(readBinaryCmd); var data = response.GetData(); if (buffer.Count == 0) bytesToRead = data[1] + 1 + 1; buffer.AddRange(data.Take(bytesToRead - buffer.Count < readSize ? bytesToRead - buffer.Count : readSize).ToArray()); if (buffer.Count >= bytesToRead) break; } Log.Debug(String.Format("ReadBinary: {0}", BitConverter.ToString(buffer.ToArray()))); Log.Debug(String.Format("Buffersize: Reported: {0}, Actual: {1}", bytesToRead, buffer.Count)); return buffer.ToArray(); } catch (Exception ex) { Log.Error(ex); } return new byte[0]; }