Use this RFID library in DotNet (C#)

Adrien JUND edited this page Aug 10, 2017 · 1 revision

License and download

LibLogicalAccess .NET component is distributed under the commercial ISLOG LibLogicalAccess License, which has to be purchased by each company or individual. We love open source ; such project can request a free license. In any case, contact us to order a license or for more information.

Examples

Initialize a reader connection

The first step you do for any operation is to initialize the reader communication.

// Explicitly use the PC/SC Reader Provider.
IReaderProvider readerProvider = new PCSCReaderProvider();

// Create the default reader unit. On PC/SC, we will listen on all readers.
IReaderUnit readerUnit = readerProvider.CreateReaderUnit();

if (readerUnit.ConnectToReader())
{
    // DO CARD STUFF HERE
    // DO CARD STUFF HERE
    // DO CARD STUFF HERE
    
    readerUnit.DisconnectFromReader();
}
Communicate with a chip

Once connected to the reader, we want to communicate with the chip. We wait a card insertion on the reader, get the associated chip object and we are ready to send command to it.

Console.WriteLine("Waiting 15 seconds for a card insertion...");
if (readerUnit.WaitInsertion(15000))
{                       
    if (readerUnit.Connect())
    {
        Console.WriteLine("Card inserted on reader '{0}.'", readerUnit.ConnectedName);

        chip chip = readerUnit.GetSingleChip();
        Console.WriteLine("Card type: {0}", chip.Type);
        Console.WriteLine("Card unique manufacturer number: {0}", readerUnit.GetNumber(chip));

        // DO CHIP COMMAND HERE
        // DO CHIP COMMAND HERE
        // DO CHIP COMMAND HERE

        readerUnit.Disconnect();
    }

    Console.WriteLine("Logical automatic card removal in 15 seconds...");
    if (!readerUnit.WaitRemoval(15000))
    {
        Console.WriteLine("Card removal forced."); 
    }

    Console.WriteLine("Card removed.");
}
else
{
    Console.WriteLine("No card inserted.");
}
Read Wiegand 26 (H10301) HID iClass card
if (chip is IHIDiClassChip)
{
    Wiegand26Format formatToRead = new Wiegand26Format();
    IAccessControlCardService acService = chip.GetService(CardServiceType.CST_ACCESS_CONTROL) as IAccessControlCardService;

    IFormat formatRead = acService.ReadFormat(formatToRead, null, null);
    if (formatRead != null)
    {
        Console.WriteLine(String.Format("Card id: {0}", formatRead.identifier));
    }
}
Encode Wiegand 37 on Mifare Classic card
if (chip is IMifareChip)
{
    MifareLocation location = new MifareLocation();
    location.Sector = 1;

    MifareAccessInfo aiToUse = new MifareAccessInfo();
    MifareAccessInfo aiToWrite = new MifareAccessInfo();
    aiToWrite.KeyA.Value = "A0 A1 A2 A3 A4 A5";
    aiToWrite.KeyB.Value = "B0 B1 B2 B3 B4 B5";

    Wiegand37WithFacilityFormat format = new Wiegand37WithFacilityFormat();
    format.FacilityCode = 100;
    format.Uid = 1234;

    IAccessControlCardService acService = chip.CardProvider.GetService(CardServiceType.CST_ACCESS_CONTROL) as IAccessControlCardService;
    if (acService.WriteFormat(format, location, aiToUse, aiToWrite))
    {
        Console.WriteLine("Format wrote successfully.");
    }
    else
    {
        Console.WriteLine("Error: Cannot write the format.");
    }
}
Create a new application on Mifare DESFire EV1 card
if (chip is IDESFireChip)
{
    ICommands cmd = chip.Commands;
    if (cmd is IDESFireEV1Commands)
    {
        IDESFireEV1Commands ev1cmd = cmd as IDESFireEV1Commands;
        ev1cmd.CreateApplication(0x000521, DESFireKeySettings.KS_DEFAULT, 3);
    }
}
Read / Write data using the generic way
// The excepted memory tree
IDESFireLocation location = new DESFireLocation();
// The Application ID to use
location.aid = 0x000521;
// File 0 into this application
location.File = 0;
// File communication requires encryption
location.SecurityLevel = CardBehavior.CM_ENCRYPT;
 
// Keys to use for authentication
IDESFireAccessInfo aiToUse = new DESFireAccessInfo();
aiToUse.MasterCardKey.Value = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
 
// Change keys with the following ones
IDESFireAccessInfo aiToWrite = new DESFireAccessInfo();
aiToWrite.MasterCardKey.Value = "ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff";
aiToWrite.MasterApplicationKey.Value = "aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa";
aiToWrite.ReadKey.Value = "22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22";
aiToWrite.ReadKeyno = 2;
aiToWrite.WriteKey.Value = "11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11";
aiToWrite.WriteKeyno = 1;

// Get the card storage service
IStorageCardService storage = (IStorageCardService)chip.GetService(CardServiceType.CST_STORAGE);

// Data to write
byte[] writedata = new byte[10];
writedata[0] = 0x11;       // Doomy value 
writedata[9] = 0xff;       // Doomy value

// Write data on the specified location with the specified key
storage.WriteData(location, aiToUse, aiToWrite, writedata, writedata.Length, CardBehavior.CB_DEFAULT);
 
// We read the data on the same location. Remember, the key is now changed.
byte[] readdata = (byte[])storage.ReadData(location, aiToWrite, 10, CardBehavior.CB_DEFAULT);
Read All 16 sectors of a Mifare
IMifareCommands cmd = chip.Commands as IMifareCommands;

MifareKey key = new MifareKey();
key.Value = "ff ff ff ff ff ff";
cmd.LoadKeyNo(1, key, MifareKeyType.KT_KEY_A);
cmd.AuthenticateKeyNo(1, 0, MifareKeyType.KT_KEY_A);
cmd.ReadBinary(1, 16);

SectorAccessBits sab = new SectorAccessBits();

for (int i = 0; i < 16; i++)
{
    (chip.Profile as IMifareProfile).SetKey(i, MifareKeyType.KT_KEY_A, "ff ff ff ff ff ff");
}

var list = new List<List<char>>();

for (int i = 0; i < 16; i++)
{
    try
    {
        object data = cmd.ReadSector(i, sab, true);
        IEnumerable enumerable = data as IEnumerable;
        if (enumerable != null)
        {
            var listChar = new List<char>();

            foreach (object element in enumerable)
            {
                int intValue = Convert.ToInt32(element.ToString(), 10);
                char c = Convert.ToChar(intValue);
                listChar.Add(c);
            }
            list.Add(listChar);
            var completeString = string.Concat(list);
        }
    }
    catch (Exception ex)
    {
        if (ex.Message == "No key found.")
        {
            Debug.WriteLine("No key found for sector: " + i);
        }
        else
        {
            throw;
        }
    }
}