Example #1
0
        bool AuthBlock(IsoCard card, int block, bool isAKey = true)
        {
            byte type = 0x60;

            if (!isAKey)
            {
                type = 0x61;
            }

            CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case3Short);

            apdu.CLA  = 0xFF;
            apdu.INS  = 0x86;
            apdu.P1   = 0x00;
            apdu.P2   = 0x00;
            apdu.Data = new byte[] { 0x01, //version
                                     0x00,
                                     (byte)block,
                                     type,   //Key type 0x60 TYPE_A, 0x61 TYPE_B
                                     0x00 }; //Key number 0x00 ~ 0x1F

            Response response = card.Transmit(apdu);

            if (response.SW1 == (byte)SW1Code.Normal)
            {
                return(true);
            }

            return(false);
        }
Example #2
0
        bool WriteBlock(IsoCard card, int block, byte[] data)
        {
            CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case3Short);

            apdu.CLA         = 0xFF;
            apdu.Instruction = InstructionCode.UpdateBinary;
            apdu.P1          = 0x00;
            apdu.P2          = (byte)block;
            apdu.Data        = data;

            Response response = card.Transmit(apdu);

            if (response.SW1 == (byte)SW1Code.Normal)
            {
                return(true);
            }

            return(false);
        }
Example #3
0
        bool LoadKey(IsoCard card, byte[] key)
        {
            CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case3Short);

            apdu.CLA         = 0xFF;
            apdu.Instruction = InstructionCode.ExternalAuthenticate;
            apdu.P1          = 0x20;
            apdu.P2          = 0x00;
            apdu.Data        = key;

            Response response = card.Transmit(apdu);

            if (response.SW1 == (byte)SW1Code.Normal)
            {
                return(true);
            }

            return(false);
        }
Example #4
0
        string ReadUID(IsoCard card)
        {
            string ouput = "";

            CommandApdu apdu = card.ConstructCommandApdu(IsoCase.Case2Short);

            apdu.CLA         = 0xFF;
            apdu.Instruction = InstructionCode.GetData;
            apdu.P1          = 0x00;
            apdu.P2          = 0x00;
            apdu.Le          = 0x00;

            Response response = card.Transmit(apdu);

            if (response.SW1 == (byte)SW1Code.Normal)
            {
                byte[] data = response.GetData();
                if (data != null)
                {
                    ouput = ToHex(data);
                }
            }
            return(ouput);
        }
Example #5
0
        static void Main(string[] args)
        {
            // Establish PC/SC context
            SCardContext ctx = new SCardContext();

            ctx.Establish(SCardScope.System);

            // Create a reader object
            SCardReader reader = new SCardReader(ctx);

            // Use the first reader that is found
            string firstreader = ctx.GetReaders()[0];

            // Connect to the card
            IsoCard card = new IsoCard(reader);

            card.Connect(firstreader, SCardShareMode.Shared, SCardProtocol.Any);

            // Build a ATR fetch case
            CommandApdu apdu = card.ConstructCommandApdu(
                IsoCase.Case2Short);

            apdu.CLA = 0x00; // Class
            apdu.INS = 0x84; // Instruction: GET CHALLENGE
            apdu.P1  = 0x00; // Parameter 1
            apdu.P2  = 0x00; // Parameter 2
            apdu.Le  = 0x08; // Expected length of the returned data

            // Transmit the Command APDU to the card and receive the response
            Response resp = card.Transmit(apdu);

            // Show SW1SW2 from the last response packet (if more than one has been received).
            Console.WriteLine("SW1: {0:X2} SW2: {1:X2}", resp.SW1, resp.SW2);

            byte[] data;

            // First test - get the data from all response APDUs
            data = resp.GetData();
            if (data != null)
            {
                Console.Write("CHALLENGE:");

                foreach (byte b in data)
                {
                    Console.Write(" {0:X2}", b);
                }
                Console.WriteLine();
            }

            // Second test - get the data from each response APDU.
            int i = 0;

            foreach (ResponseApdu respApdu in resp.ResponseApduList)
            {
                data = respApdu.GetData();

                if (data != null)
                {
                    Console.Write("APDU ({0}), DATA:", i);
                    foreach (byte b in data)
                    {
                        Console.Write(" {0:X2}", b);
                    }
                    Console.WriteLine();
                    i++;
                }
            }
            return;
        }