Beispiel #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.");
            }
        }
Beispiel #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.");
            }
        }