Use This RFID Library in DotNet (C#) Islog - Liblogicalaccess Wiki GitHub
Use This RFID Library in DotNet (C#) Islog - Liblogicalaccess Wiki GitHub
1 of 5
https://github.com/islog/liblogicalaccess/wiki/Use-this-RFID-library-in-...
Skip to content
Watch 13
Star 65
Fork 18
islog/liblogicalaccess
Code
Issues 10
Pull requests 0
Projects 0
Wiki
Pulse
Graphs
Pages 27
Home
Abstraction layer description
Build Liblogicalaccess
Contribute & Contact
Debian Ubuntu Build Packages
Frequent issues and questions
Frequent issues and questions of LibLogicalAccess developers
HID iClass examples
HID Omnikey readers (PC SC) examples
HID Prox examples
How it work Why so many plugins
How to Use Liblogicalaccess
Install LibLogicalAccess
LibLogicalAccess.exe
Mifare Classic examples
Mifare DESFire EV1 examples
Mifare DESFire EV2 examples
Mifare DESFire examples
MIFARE SAM AV1 AV2 examples
NFC Reader
OSDP v2 examples
Private plugins
STid STR readers examples
Supported hardware
Use this RFID library in Delphi
Use this RFID library in DotNet (C#)
Use this RFID library in VB6
Show 12 more pages
Home
Supported hardware
Abstraction layer description
How it work ? Why so many plugins ?
Examples:
Quick How To (C++)
Chip:
Mifare Classic
Mifare DESFire
Mifare DESFire EV1
Mifare DESFire EV2
Mifare SAM (Secure Access Modules)
HID iClass
HID Prox
Reader:
HID Omnikey readers (PC/SC)
11/9/2016 3:07 PM
2 of 5
https://github.com/islog/liblogicalaccess/wiki/Use-this-RFID-library-in-...
Clone in Desktop
Use this RFID library in DotNet (C#)
On Windows, this library can be addressed directly into another language like C# as a regular SDK, thanks to the COM Wrapper. In fact the only prerequisite is that this
language support COM objects and the DotNet platform provides this capability.\ Please see this chapter for more information about the COM component installation.
Get started in few seconds
The best way to get started is to try it directly!
Download and install liblogicalaccess.exe from the download page.
Download, build and run the C# basic sample.
Configure your project
Add reference to the installed ''LibLogicalAccess'' COM component with the excepted version.
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();
11/9/2016 3:07 PM
3 of 5
https://github.com/islog/liblogicalaccess/wiki/Use-this-RFID-library-in-...
// 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 =
MifareAccessInfo aiToWrite
aiToWrite.KeyA.Value = "A0
aiToWrite.KeyB.Value = "B0
new MifareAccessInfo();
= new MifareAccessInfo();
A1 A2 A3 A4 A5";
B1 B2 B3 B4 B5";
11/9/2016 3:07 PM
4 of 5
https://github.com/islog/liblogicalaccess/wiki/Use-this-RFID-library-in-...
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;
}
}
}
11/9/2016 3:07 PM
5 of 5
https://github.com/islog/liblogicalaccess/wiki/Use-this-RFID-library-in-...
11/9/2016 3:07 PM