示例#1
0
        /// <summary>
        /// Extension method to SmartCardConnection class to perform a transparent exchange to the ICC
        /// </summary>
        /// <param name="connection">
        /// SmartCardConnection object
        /// </param>
        /// <param name="commandData">
        /// Command object to send to the ICC
        /// </param>
        /// <returns>Response received from the ICC</returns>
        public static Windows.Foundation.IAsyncOperation <IEnumerable <byte> > TransparentExchangeAsync(this SmartCardConnection connection, [ReadOnlyArray] byte[] commandData)
        {
            return(AsyncInfo.Run <IEnumerable <byte> >(async(cancel) =>
            {
                byte[] responseData = null;
                ManageSessionResponse apduRes = await TransceiveAsync(connection, new ManageSession(new byte[2] {
                    (byte)ManageSession.DataObjectType.StartTransparentSession, 0x00
                })) as ManageSessionResponse;

                if (!apduRes.SucceededManaged)
                {
                    throw new Exception("Failure to start transparent session, " + apduRes.ToString());
                }

                using (DataWriter dataWriter = new DataWriter())
                {
                    dataWriter.WriteByte((byte)TransparentExchange.DataObjectType.Transceive);
                    dataWriter.WriteByte((byte)commandData.Length);
                    dataWriter.WriteBytes(commandData);

                    var task2 = TransceiveAsync(connection, new TransparentExchange(dataWriter.DetachBuffer().ToArray()));
                    TransparentExchangeResponse apduRes1 = task2.GetResults() as TransparentExchangeResponse;

                    if (!apduRes1.SucceededTransparent)
                    {
                        throw new Exception("Failure transceive with card, " + apduRes1.ToString());
                    }

                    responseData = apduRes1.IccResponse;
                }

                var task3 = TransceiveAsync(connection, new ManageSession(new byte[2] {
                    (byte)ManageSession.DataObjectType.EndTransparentSession, 0x00
                }));

                ManageSessionResponse apduRes2 = task3.GetResults() as ManageSessionResponse;
                if (!apduRes2.SucceededManaged)
                {
                    throw new Exception("Failure to end transparent session, " + apduRes2.ToString());
                }

                return responseData;
            }));
        }
示例#2
0
        } // TransceiveAsync

        /// <summary>
        /// Extension method to SmartCardConnection class to perform a transparent exchange to the ICC
        /// </summary>
        /// <param name="connection">
        /// SmartCardConnection object
        /// </param>
        /// <param name="commandData">
        /// Command object to send to the ICC
        /// </param>
        /// <returns>Response received from the ICC</returns>
        public static async Task <byte[]> TransparentExchangeAsync(this SmartCardConnection connection, byte[] commandData)
        {
            byte[] responseData           = null;
            ManageSessionResponse apduRes = await TransceiveAsync(connection, new ManageSession(new byte[2] {
                (byte)ManageSession.DataObjectType.StartTransparentSession, 0x00
            })) as ManageSessionResponse;

            if (!apduRes.Succeeded)
            {
                throw new Exception("Failure to start transparent session, " + apduRes.ToString());
            }

            using (DataWriter dataWriter = new DataWriter())
            {
                dataWriter.WriteByte((byte)TransparentExchange.DataObjectType.Transceive);
                dataWriter.WriteByte((byte)commandData.Length);
                dataWriter.WriteBytes(commandData);

                TransparentExchangeResponse apduRes1 = await TransceiveAsync(connection, new TransparentExchange(dataWriter.DetachBuffer().ToArray())) as TransparentExchangeResponse;

                if (!apduRes1.Succeeded)
                {
                    throw new Exception("Failure transceive with card, " + apduRes1.ToString());
                }

                responseData = apduRes1.IccResponse;
            }

            ManageSessionResponse apduRes2 = await TransceiveAsync(connection, new ManageSession(new byte[2] {
                (byte)ManageSession.DataObjectType.EndTransparentSession, 0x00
            })) as ManageSessionResponse;

            if (!apduRes2.Succeeded)
            {
                throw new Exception("Failure to end transparent session, " + apduRes2.ToString());
            }

            return(responseData);
        }