Ejemplo n.º 1
0
        public static void WriteCode()
        {
            // Creating the device class through serial: /dev/ttyS0
            try
            {
                string device    = "/dev/ttyS0";
                var    reader    = new Pn532(device);
                byte[] writeUser = null;
                writeUser = reader.ListPassiveTarget(MaxTarget.One, TargetBaudRate.B106kbpsTypeA);
                Thread.Sleep(200);

                do
                {
                    var decrypted = reader.TryDecode106kbpsTypeA(writeUser.AsSpan().Slice(1));
                    var uid       = BitConverter.ToString(decrypted.NfcId);

                    Console.WriteLine("Enter Username:");
                    var    userName  = Console.ReadLine();
                    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(userName);
                    byte[] cardData  = null;
                    reader.Transceive(1, byteArray, cardData);


                    MifareCard mifareCard = new MifareCard(reader, decrypted.TargetNumber)
                    {
                        BlockNumber = 0, Command = MifareCardCommand.AuthenticationA
                    };
                    mifareCard.SetCapacity(decrypted.Atqa, decrypted.Sak);
                    mifareCard.SerialNumber = decrypted.NfcId;
                    mifareCard.KeyA         = new byte[6] {
                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                    };
                    mifareCard.KeyB = new byte[6] {
                        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
                    };

                    for (byte block = 0; block < 1; block++)
                    {
                        mifareCard.BlockNumber = block;
                        mifareCard.Command     = MifareCardCommand.AuthenticationB;
                        var ret = mifareCard.RunMifiCardCommand();
                        if (ret < 0)
                        {
                            // Try another one
                            mifareCard.Command = MifareCardCommand.AuthenticationA;
                            ret = mifareCard.RunMifiCardCommand();
                        }

                        if (ret == 1)
                        {
                            mifareCard.BlockNumber = block;
                            mifareCard.Command     = MifareCardCommand.Write16Bytes;

                            ret = mifareCard.RunMifiCardCommand();
                            Console.WriteLine(BitConverter.ToString(cardData));
                            continue;
                        }
                    }

                    reader.ReleaseTarget(1);
                    break;
                }while(writeUser != null);
            }

            catch (System.Exception)
            {
                WriteCode();
            }
        }
Ejemplo n.º 2
0
        static void TestCCDirect(Pn532 pn532, byte Tg)
        {
            var toSend = new byte[] { 0x00, 0xA4, 0x04, 0x00,                                                             // CLA - INS - P1 - P2
                                      0x0e,                                                                               // Length
                                      0x32, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31, // 1PAY.SYS.DDF01 (PPSE)
                                      0x00 };
            Span <byte> span = stackalloc byte[260];
            var         ret  = pn532.Transceive(Tg, toSend, span);
            //if (ret >= 0)
            //{
            //    //Console.WriteLine($"{BitConverter.ToString(span.ToArray())}");
            //    FileControlInformation fs = new FileControlInformation(span.Slice(0, ret));
            //    Console.WriteLine($"Dedicated Name: {BitConverter.ToString(fs.DedicatedFileName)}");
            //    foreach (var app in fs.ProprietaryTemplate.ApplicationTemplates)
            //    {
            //        Console.WriteLine($"APPID: {BitConverter.ToString(app.ApplicationIdentifier)}, Priority: {app.ApplicationPriorityIndicator}, {app.ApplicationLabel}");
            //        //var test = new byte[] { 0x00, 0xB2, 0x01, 0x0C, 0x00 };
            //        // This is working and give some data:
            //        //var test = new byte[] { 0x00, 0xA4, 0x04, 0x00, 0x07, 0xA0, 0x00, 0x00, 0x00, 0x42, 0x10, 0x10, 0x00 };
            //        var tests = new byte[6 + app.ApplicationIdentifier.Length];
            //        tests[0] = 0x00;
            //        tests[1] = 0xA4;
            //        tests[2] = 0x04;
            //        tests[3] = 0x00;
            //        tests[4] = (byte)app.ApplicationIdentifier.Length;
            //        app.ApplicationIdentifier.CopyTo(tests, 5);
            //        tests[5 + app.ApplicationIdentifier.Length] = 0x00;

            //        ret = pn532.WriteRead(Tg, tests, span);
            //        if (ret >= 0)
            //        {
            //            //Console.WriteLine($"{BitConverter.ToString(span.ToArray())}");
            //            // AddEntryPaylog(pn532, span, decrypted.Tg)
            //            var recordInfos = new byte[] {0x00,0xB2, // READ RECORD
            //     0x00,0x00, // count + (SFI/flags),  edited dynamically
            //     0x00};

            //            for (int sfi = 0; sfi <= 2; sfi++)
            //            {

            //                recordInfos[3] = (byte)((sfi << 3) | (1 << 2));
            //                for (byte record = 0; record <= 2; record++)
            //                {
            //                    recordInfos[2] = record;

            //                    ret = pn532.WriteRead(Tg, recordInfos, span);
            //                    if (ret >= 0)
            //                    {
            //                        try
            //                        {
            //                            var Emv = new EmvProprietaryTemplate(span.Slice(0, ret));
            //                            Console.WriteLine($"{BitConverter.ToString(span.ToArray())}");
            //                            Console.WriteLine($"Name: {Emv.CardHolder}, Extended Name: {Emv.CardHolderExtended}");
            //                            if (Emv.Track1DiscretionaryData != null)
            //                                Console.WriteLine($"Track1: { BitConverter.ToString(Emv.Track1DiscretionaryData)}");
            //                        }
            //                        catch (Exception)
            //                        {
            //                            Console.WriteLine($"{BitConverter.ToString(span.ToArray())}");
            //                        }
            //                    }
            //                }
            //            }
            //        }
            //        //var gpoheaders = new byte[6 + app.ApplicationIdentifier.Length];
            //        //gpoheaders[0] = 0x00;
            //        //gpoheaders[1] = 0xA4;
            //        //gpoheaders[2] = 0x04;
            //        //gpoheaders[3] = 0x00;
            //        //gpoheaders[4] = (byte)app.ApplicationIdentifier.Length;
            //        //app.ApplicationIdentifier.CopyTo(gpoheaders, 4);
            //        //gpoheaders[4 + app.ApplicationIdentifier.Length] = 0x00;
            //        ////gpoheaders[4 + app.ApplicationIdentifier.Length + 1] = 0x90;
            //        ////gpoheaders[4 + app.ApplicationIdentifier.Length + 2] = 0x00;

            //        //ret = pn532.InDataExchange(decrypted.Tg, gpoheaders, span);
            //        //if (ret)
            //        //{
            //        //    Console.WriteLine($"{BitConverter.ToString(span.ToArray())}");
            //        //    for (byte sfi = 1; sfi <= 31; sfi++)
            //        //    {
            //        //        for (byte rec = 1; rec <= 16; rec++)
            //        //        {
            //        //            var details = new byte[5] { 0x00, 0xB2, rec, (byte)((sfi << 3) | 4), 0x00 };
            //        //            ret = pn532.InDataExchange(decrypted.Tg, details, span);
            //        //            //var tlv = card.sendApdu(0x00, 0xB2, rec, (sfi << 3) | 4, 0x00);
            //        //            if (ret)
            //        //            {
            //        //                Console.WriteLine($"{BitConverter.ToString(span.ToArray())}");
            //        //                //if (card.SW == 0x9000)
            //        //                //{
            //        //                //    print("SFI " + sfi.toString(16) + " record #" + rec);
            //        //                //    try
            //        //                //    {
            //        //                //        var asn = new ASN1(tlv);
            //        //                //        print(asn);
            //        //                //    }
            //        //                //    catch (e)
            //        //                //    {
            //        //                //        print(tlv.toString(HEX));
            //        //                //    }
            //        //                //}
            //        //            }
            //        //        }
            //        //    }

            //        //}
            //    }
            //    //          var gpoheaders = new byte[] {   0x80,0xCA, // GET DATA
            //    //0x9F,0x4F, // 9F4F asks for the log format
            //    //0x00};

            //}
        }