Esempio n. 1
0
 public bool WriteBlock(MifareCard card, byte[] data, int sector, int block)
 {
     if (data.Length == MifareCard.NumberOfBytesInABlock)
     {
         if (card.GetCurrentAuthenticatedSector() == sector)
         {
             return(WriteBlockWithoutAuth(card, data, sector, block));
         }
         else
         {
             if (AuthenticateSector(card, sector))
             {
                 return(WriteBlockWithoutAuth(card, data, sector, block));
             }
             else
             {
                 throw new Exception("Authentication failed for the specified key.");;
             }
         }
     }
     else
     {
         throw new ArgumentOutOfRangeException("Data must be 16 bytes.");
     }
 }
Esempio n. 2
0
        public bool WriteKeyA(MifareCard card, string data, int sector)
        {
            if (data.Length == MifareCard.NumberOfCharactersInKey)
            {
                if (!ReadBlock(card, sector, MifareCard.KeyBlock))
                {
                    return(false);
                }

                string originalData = card.GetDataFromLastBlockRead();
                string newData      = data + originalData.Substring(MifareCard.NumberOfCharactersInKey);

                if (card.GetCurrentAuthenticatedSector() == sector)
                {
                    return(WriteBlockWithoutAuth(card, newData, sector, MifareCard.KeyBlock)); //block #3 is sector trailer
                }
                else
                {
                    if (AuthenticateSector(card, sector))
                    {
                        return(WriteBlockWithoutAuth(card, newData, sector, MifareCard.KeyBlock));
                    }
                    else
                    {
                        throw new Exception("Authentication failed for the specified key.");;
                    }
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("The key must be 12 characters.");
            }
        }
Esempio n. 3
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.");
            }
        }
Esempio n. 4
0
 public bool ReadBlock(MifareCard card, int sector, int block)
 {
     if (card.GetCurrentAuthenticatedSector() == sector)
     {
         return(ReadBlockWithoutAuth(card, sector, block));
     }
     else
     {
         if (AuthenticateSector(card, sector))
         {
             return(ReadBlockWithoutAuth(card, sector, block));
         }
         else
         {
             return(false);
         }
     }
 }
Esempio n. 5
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.");
            }
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
 public bool WriteBlock(MifareCard card, string data, int sector, int block)
 {
     if (data.Length == MifareCard.NumberOfCharactersInABlock)
     {
         if (card.GetCurrentAuthenticatedSector() == sector)
         {
             return(WriteBlockWithoutAuth(card, data, sector, block));
         }
         else
         {
             if (AuthenticateSector(card, sector))
             {
                 return(WriteBlockWithoutAuth(card, data, sector, block));
             }
             else
             {
                 return(false);
             }
         }
     }
     return(false);
 }
Esempio n. 8
0
        public bool WriteKeys(MifareCard card, string keyA, string keyB, int sector, string accessbits)
        {
            if (keyA.Length == 12 && keyB.Length == 12)
            {
                if (!ReadBlock(card, sector, 3))
                {
                    return(false);
                }

                string keyString = keyA + accessbits + keyB;

                if (card.GetCurrentAuthenticatedSector() == sector)
                {
                    WriteBlockWithoutAuth(card, keyString, sector, 3);
                }
                else
                {
                    if (AuthenticateSector(card, sector))
                    {
                        if (WriteBlockWithoutAuth(card, keyString, sector, 3))
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            return(false);
        }
Esempio n. 9
0
 public bool WriteKeyB(MifareCard card, string data, int sector)
 {
     throw new NotImplementedException();
 }
Esempio n. 10
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");
            }
        }
 public bool WriteKeys(MifareCard card, string keyA, string keyB, int sector, string accessbits)
 {
     throw new NotImplementedException();
 }
 public bool WriteBlock(MifareCard card, byte[] data, int sector, int block)
 {
     throw new NotImplementedException();
 }
 public bool ReadBlock(MifareCard card, int sector, int block)
 {
     throw new NotImplementedException();
 }
 public bool AuthenticateSector(MifareCard card, int sector)
 {
     throw new NotImplementedException();
 }
 public bool RequestCardSerial(MifareCard card)
 {
     throw new NotImplementedException();
 }