2020-09-15 15:27:01 +02:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NFC;
|
|
|
|
|
using NFC.Readers.PCSC;
|
2020-09-17 16:07:58 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using NFC.Mifare_DESFire;
|
2020-09-26 18:02:44 +02:00
|
|
|
|
using NFC.Mifare_DESFire.Enums;
|
2020-09-15 15:27:01 +02:00
|
|
|
|
|
|
|
|
|
namespace NFC_Test
|
|
|
|
|
{
|
|
|
|
|
[TestFixture, Explicit]
|
|
|
|
|
public class REAL_Windows
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void GetReaders()
|
|
|
|
|
{
|
2020-09-17 16:07:58 +02:00
|
|
|
|
IHardware hardware = new Hardware();
|
2020-09-15 15:27:01 +02:00
|
|
|
|
string[] readers = hardware.GetReaders();
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Readers detected: {0}", readers.Length);
|
|
|
|
|
|
|
|
|
|
if(readers.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("List of ReaderIDs:");
|
|
|
|
|
foreach (string readerID in readers)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("{0}", readerID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-17 16:07:58 +02:00
|
|
|
|
[TestCase("ACS ACR122U PICC Interface 0")]
|
2020-09-15 15:27:01 +02:00
|
|
|
|
public void Connect(string readerID)
|
|
|
|
|
{
|
2020-09-17 16:07:58 +02:00
|
|
|
|
IHardware hardware = new Hardware();
|
|
|
|
|
IReader reader = hardware.OpenReader(readerID);
|
2020-09-15 15:27:01 +02:00
|
|
|
|
|
2020-09-17 16:07:58 +02:00
|
|
|
|
bool connected_successfully = false;
|
|
|
|
|
|
|
|
|
|
ReaderEventHandler handler = (sender, card) =>
|
|
|
|
|
{
|
|
|
|
|
card.Connect();
|
|
|
|
|
|
|
|
|
|
connected_successfully = true;
|
|
|
|
|
|
|
|
|
|
card.Disconnect();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
reader.CardDiscovered += handler;
|
|
|
|
|
reader.Start();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(true, connected_successfully);
|
|
|
|
|
|
|
|
|
|
reader.Stop();
|
|
|
|
|
reader.CardDiscovered -= handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCase("ACS ACR122U PICC Interface 0")]
|
|
|
|
|
public void GetApplicationIDs(string readerID)
|
|
|
|
|
{
|
|
|
|
|
IHardware hardware = new Hardware();
|
|
|
|
|
IReader reader = hardware.OpenReader(readerID);
|
|
|
|
|
|
|
|
|
|
bool transmit_successfully = false;
|
|
|
|
|
|
|
|
|
|
ReaderEventHandler handler = (sender, card) =>
|
|
|
|
|
{
|
|
|
|
|
card.Connect();
|
|
|
|
|
|
2020-09-26 18:02:44 +02:00
|
|
|
|
MifareDESFire desfire = new MifareDESFire(card);
|
2020-09-17 16:07:58 +02:00
|
|
|
|
|
|
|
|
|
APDUCommand cmd = desfire.GetApplicationIDs();
|
|
|
|
|
|
|
|
|
|
APDUResponse response = card.Transmit(cmd);
|
|
|
|
|
|
|
|
|
|
if (response.StatusWord == NFC.Mifare_DESFire.APDUStatusWords.OK)
|
|
|
|
|
{
|
|
|
|
|
UInt32[] ApplicationIDs = desfire.ConvertApplicationIDs(response);
|
|
|
|
|
|
|
|
|
|
foreach(UInt32 id in ApplicationIDs)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("0x{0:X3}", id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
transmit_successfully = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
card.Disconnect();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
reader.CardDiscovered += handler;
|
|
|
|
|
reader.Start();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(true, transmit_successfully);
|
|
|
|
|
|
|
|
|
|
reader.Stop();
|
|
|
|
|
reader.CardDiscovered -= handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCase("ACS ACR122U PICC Interface 0", (UInt32)0xC0FFEE)]
|
|
|
|
|
public void SelectApplication(string readerID, UInt32 applicationID)
|
|
|
|
|
{
|
|
|
|
|
IHardware hardware = new Hardware();
|
|
|
|
|
IReader reader = hardware.OpenReader(readerID);
|
|
|
|
|
|
|
|
|
|
bool transmit_successfully = false;
|
|
|
|
|
|
|
|
|
|
ReaderEventHandler handler = (sender, card) =>
|
|
|
|
|
{
|
|
|
|
|
card.Connect();
|
|
|
|
|
|
2020-09-26 18:02:44 +02:00
|
|
|
|
MifareDESFire desfire = new MifareDESFire(card);
|
2020-09-17 16:07:58 +02:00
|
|
|
|
|
|
|
|
|
APDUCommand cmd = desfire.SelectApplication(applicationID);
|
|
|
|
|
|
|
|
|
|
cmd.ToArray();
|
|
|
|
|
|
|
|
|
|
APDUResponse response = card.Transmit(cmd);
|
|
|
|
|
|
|
|
|
|
if (response.StatusWord == NFC.Mifare_DESFire.APDUStatusWords.OK)
|
|
|
|
|
{
|
|
|
|
|
transmit_successfully = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
card.Disconnect();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
reader.CardDiscovered += handler;
|
|
|
|
|
reader.Start();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(true, transmit_successfully);
|
|
|
|
|
|
|
|
|
|
reader.Stop();
|
|
|
|
|
reader.CardDiscovered -= handler;
|
2020-09-15 15:27:01 +02:00
|
|
|
|
}
|
2020-09-25 20:23:33 +02:00
|
|
|
|
|
|
|
|
|
[TestCase("ACS ACR122U PICC Interface 0", (UInt32)0xC0FFEE)]
|
|
|
|
|
public void DeleteApplication(string readerID, UInt32 applicationID)
|
|
|
|
|
{
|
|
|
|
|
IHardware hardware = new Hardware();
|
|
|
|
|
IReader reader = hardware.OpenReader(readerID);
|
|
|
|
|
|
|
|
|
|
bool transmit_successfully = false;
|
|
|
|
|
|
|
|
|
|
ReaderEventHandler handler = (sender, card) =>
|
|
|
|
|
{
|
|
|
|
|
card.Connect();
|
|
|
|
|
|
2020-09-26 18:02:44 +02:00
|
|
|
|
MifareDESFire desfire = new MifareDESFire(card);
|
2020-09-25 20:23:33 +02:00
|
|
|
|
|
|
|
|
|
APDUCommand cmd = desfire.DeleteApplication(applicationID);
|
|
|
|
|
|
|
|
|
|
cmd.ToArray();
|
|
|
|
|
|
|
|
|
|
APDUResponse response = card.Transmit(cmd);
|
|
|
|
|
|
|
|
|
|
if (response.StatusWord == NFC.Mifare_DESFire.APDUStatusWords.OK)
|
|
|
|
|
{
|
|
|
|
|
transmit_successfully = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
card.Disconnect();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
reader.CardDiscovered += handler;
|
|
|
|
|
reader.Start();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(true, transmit_successfully);
|
|
|
|
|
|
|
|
|
|
reader.Stop();
|
|
|
|
|
reader.CardDiscovered -= handler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestCase("ACS ACR122U PICC Interface 0", (UInt32)0xC0FFEE)]
|
|
|
|
|
public void CreateApplication(string readerID, UInt32 applicationID)
|
|
|
|
|
{
|
|
|
|
|
IHardware hardware = new Hardware();
|
|
|
|
|
IReader reader = hardware.OpenReader(readerID);
|
|
|
|
|
|
|
|
|
|
bool transmit_successfully = false;
|
|
|
|
|
|
|
|
|
|
ReaderEventHandler handler = (sender, card) =>
|
|
|
|
|
{
|
|
|
|
|
card.Connect();
|
|
|
|
|
|
2020-09-26 18:02:44 +02:00
|
|
|
|
MifareDESFire desfire = new MifareDESFire(card);
|
2020-09-25 20:23:33 +02:00
|
|
|
|
|
2020-09-26 18:02:44 +02:00
|
|
|
|
byte keysetting1 = desfire.GenerateKeySetting1(ChangeApplicationKey.SAMEKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.NOKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE);
|
|
|
|
|
byte keysetting2 = desfire.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 0x01);
|
2020-09-25 20:23:33 +02:00
|
|
|
|
|
|
|
|
|
APDUCommand cmd = desfire.CreateApplication(applicationID, keysetting1, keysetting2);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(cmd.ToArray());
|
|
|
|
|
|
|
|
|
|
APDUResponse response = card.Transmit(cmd);
|
|
|
|
|
|
|
|
|
|
if (response.StatusWord == NFC.Mifare_DESFire.APDUStatusWords.OK)
|
|
|
|
|
{
|
|
|
|
|
transmit_successfully = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
card.Disconnect();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
reader.CardDiscovered += handler;
|
|
|
|
|
reader.Start();
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(true, transmit_successfully);
|
|
|
|
|
|
|
|
|
|
reader.Stop();
|
|
|
|
|
reader.CardDiscovered -= handler;
|
|
|
|
|
}
|
2020-09-15 15:27:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|