Esempio n. 1
0
        private byte[] Transmit(byte[] send)
        {
            try
            {
                reader.Reconnect(SCardShareMode.Shared, SCardProtocol.Any, SCardReaderDisposition.Leave);
            }
            catch (NoSmartcardException)
            {
                return(null);
            }

            byte[] recv = new byte[1024];

            int recvLength = reader.Transmit(send, recv);

            recv = recv.Where((val, idx) => idx < recvLength).ToArray();
            Console.WriteLine(string.Join("", send.Select(val => $"{val:x02}")));
            Console.WriteLine(string.Join("", recv.Select(val => $"{val:x02}")));
            if (!(recv[recv.Length - 2] == 0x90 && recv[recv.Length - 1] == 0x00))
            {
                return(null);
            }

            var response = recv.Where((val, idx) => 1 <= idx && idx < recv.Length - 2).ToArray();

            return(response);
        }
Esempio n. 2
0
        /// <summary>
        /// Extension method to SmartCardConnection class similar to Transmit asyc method, however it accepts PCSC SDK commands.
        /// </summary>
        /// <param name="apduCommand">
        /// APDU command object to send to the ICC
        /// </param>
        /// <param name="reader">
        /// SmartCardConnection object
        /// </param>
        /// <returns>APDU response object of type defined by the APDU command object</returns>
        public static Iso7816.ApduResponse Transceive(this ICardReader reader, Iso7816.ApduCommand apduCommand)
        {
            Iso7816.ApduResponse apduRes = Activator.CreateInstance(apduCommand.ApduResponseType) as Iso7816.ApduResponse;

            byte[] resp          = new byte[256];
            int    bytesReceived = reader.Transmit(apduCommand.ToByteArray(), resp);

            Array.Resize(ref resp, bytesReceived);

            apduRes.ExtractResponse(resp);

            return(apduRes);
        }