示例#1
0
        public bool AuthenticateSector(MifareCard card, int sectorNumber)
        {
            int  status = -1;
            byte mode   = card.GetAuthenticationMode();
            byte sectorNumberInBytes = 0x00;

            if (IsConnected())
            {
                if (RequestCardSerial(card))
                {
                    try
                    {
                        sectorNumberInBytes = Convert.ToByte(sectorNumber);

                        IntPtr keyBuffer = Marshal.AllocHGlobal(1024);

                        byte[] bytesKey = RadioUtilities.ToDigitsBytes(card.GetKey(sectorNumber));

                        //Write key to unmanaged memory to be read by dll function
                        for (int i = 0; i < bytesKey.Length; i++)
                        {
                            Marshal.WriteByte(keyBuffer, i, bytesKey[i]);
                        }

                        status = rf_M1_authentication2(ICDEV, mode, (byte)(sectorNumberInBytes * MifareCard.NumberOfBlocksInASector), keyBuffer);

                        Marshal.FreeHGlobal(keyBuffer);
                    }
                    catch (Exception)
                    {
                        throw;
                    }

                    if (status != 0)
                    {
                        return(false);
                    }

                    return(true);
                }
                else
                {
                    throw new Exception("No card inserted.");
                }
            }
            else
            {
                throw new Exception("Card Reader not connected.");
            }
        }
示例#2
0
        private bool WriteBlockWithoutAuth(MifareCard card, string data, int sector, int block)
        {
            int  status   = -1;
            byte mode     = card.GetAuthenticationMode();
            byte sectorNo = Convert.ToByte(sector);
            byte adr;
            int  i;

            if (IsConnected())
            {
                adr = (byte)(Convert.ToByte(block) + sectorNo * MifareCard.NumberOfBlocksInASector);

                byte[] bytesBlock;
                bytesBlock = RadioUtilities.ToDigitsBytes(data);

                try
                {
                    IntPtr dataBuffer = Marshal.AllocHGlobal(1024);

                    for (i = 0; i < bytesBlock.Length; i++)
                    {
                        Marshal.WriteByte(dataBuffer, i, bytesBlock[i]);
                    }

                    status = rf_M1_write(ICDEV, adr, dataBuffer);
                    Marshal.FreeHGlobal(dataBuffer);
                }
                catch (Exception)
                {
                    throw;
                }

                if (status != 0)
                {
                    return(false);
                }

                return(true);
            }
            else
            {
                throw new Exception("Card Reader not connected.");
            }
        }
示例#3
0
        private bool ReadBlockWithoutAuth(MifareCard card, int sector, int blockNo)
        {
            int status = -1;

            byte[] dataInBytes = new byte[MifareCard.NumberOfBytesInABlock];
            byte   sectorNo    = Convert.ToByte(sector);
            int    j;
            byte   cLen = 0;

            try
            {
                IntPtr dataBuffer = Marshal.AllocHGlobal(1024);

                status = rf_M1_read(ICDEV, (byte)((sectorNo * MifareCard.NumberOfBlocksInASector) + blockNo), dataBuffer, ref cLen);

                if (status != 0 || cLen != MifareCard.NumberOfBytesInABlock)
                {
                    Marshal.FreeHGlobal(dataBuffer);
                    throw new Exception("Unable to read from card.");
                }

                for (j = 0; j < dataInBytes.Length; j++)
                {
                    dataInBytes[j] = Marshal.ReadByte(dataBuffer, j);
                }

                Marshal.FreeHGlobal(dataBuffer);
            }
            catch (Exception)
            {
                return(false);

                throw;
            }

            card.SetDataInBytes(dataInBytes);
            card.SetDataFromLastBlockRead(RadioUtilities.ToHexString(dataInBytes));
            return(true);
        }
示例#4
0
        public bool RequestCardSerial(MifareCard card)
        {
            int    status           = -1;
            string cardSerialNumber = String.Empty;

            if (IsConnected())
            {
                try
                {
                    pSnr = Marshal.AllocHGlobal(1024);  //Allocate 1Kb of memory

                    for (int i = 0; i < 2; i++)         //try 3 times
                    {
                        if (i > 0 && !SetRequestMode()) //reset antenna for second and third try only
                        {
                            ResetAntenna();             //set reader working type and reset

                            Sleep(50);
                            continue;
                        }

                        if (i < 1 && !SetRequestMode())
                        {
                            continue;
                        }

                        status = rf_anticoll(ICDEV, byteCount, pSnr, ref len);//request serial number and lenght of data
                        if (status != 0)
                        {
                            continue;
                        }

                        status = rf_select(ICDEV, pSnr, len, ref size);//selecting the card
                        if (status != 0)
                        {
                            continue;
                        }

                        byte[] seriaLNumberInBytes = new byte[len];

                        for (int j = 0; j < len; j++)
                        {
                            seriaLNumberInBytes[j] = Marshal.ReadByte(pSnr, j);
                        }

                        for (int q = 0; q < len; q++)
                        {
                            cardSerialNumber += RadioUtilities.byteToHEX(seriaLNumberInBytes[q]);
                        }

                        card.SetCardSerial(cardSerialNumber);
                        return(true);
                    }

                    return(false);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                throw new Exception("Failed to connect to reader");
            }
        }