2023-01-29 18:43:53 +01:00
|
|
|
|
using NFC;
|
|
|
|
|
using NFC.Helper;
|
|
|
|
|
using NFC.Helper.Crypto;
|
|
|
|
|
using NFC.Interfaces;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
2023-01-30 23:27:00 +01:00
|
|
|
|
using NFC.PCSC;
|
|
|
|
|
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
|
|
|
|
|
using NFC.Cards.NXP_MIFARE_DESFire;
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
|
|
|
|
namespace NFC_Test.REAL
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2023-01-30 23:27:00 +01:00
|
|
|
|
/// Test all DESFire Commands with an Empty nfcService
|
2023-01-29 18:43:53 +01:00
|
|
|
|
/// The Test are ordered to check the Commands one by one
|
|
|
|
|
/// </summary>
|
|
|
|
|
[TestFixture, Explicit]
|
|
|
|
|
public class REAL_EncFile
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Set ReaderID for PCSC Interface
|
|
|
|
|
/// You can get the ID from REAL_Reader_PCSC
|
|
|
|
|
/// </summary>
|
2023-01-30 23:27:00 +01:00
|
|
|
|
public readonly string ReaderID = "ACS ACR122 0";// "ACS ACR122U PICC Interface 0";
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
|
|
|
|
#region Fixed Config Properties
|
|
|
|
|
public readonly UInt32 ApplicationID = 0xAAFFEE;
|
|
|
|
|
public readonly string ApplicationMasterKey = "25432A462D4A614E645267556B587032";
|
|
|
|
|
public readonly string ApplicationKey_1 = "25432A462D4A614E645267556B587032";
|
|
|
|
|
public readonly byte FileID = 0x01;
|
|
|
|
|
public readonly byte FileSize = 0xF0;
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
[Test, Order(1)]
|
|
|
|
|
public void CreateFile()
|
|
|
|
|
{
|
2023-01-30 23:27:00 +01:00
|
|
|
|
INFCService nfcService = new NFCService();
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
nfcService.Connect(ReaderID);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(nfcService);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
desfire.SelectApplication(0x000000);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
CipherKey key = new CipherKey(CipherType.TDES);
|
|
|
|
|
desfire.AuthenticateISO_DES(0x00, key._Key);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
desfire.Format();
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
desfire.AuthenticateISO_DES(0x00, key._Key);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
byte keysetting1 = desfire.GenerateKeySetting1(ChangeApplicationKey.MASTERKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.ONLYMASTERKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE);
|
|
|
|
|
byte keysetting2 = desfire.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 2);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
desfire.CreateApplication(ApplicationID, keysetting1, keysetting2);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
desfire.SelectApplication(ApplicationID);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
CipherKey key_aes = new CipherKey(CipherType.AES);
|
|
|
|
|
desfire.AuthenticateISO_AES(0x00, key_aes._Key);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00);
|
|
|
|
|
desfire.CreateFile_Standard(FileID, FileCommunication.ENCRYPT, accesRights, FileSize);
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
desfire.WriteData(FileID, 0, Encoding.ASCII.GetBytes("Test1234"));
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
APDUCommand cmd_WriteData = new APDUCommand(IsoCase.Case4Short)
|
|
|
|
|
{
|
|
|
|
|
CLA = 0x90,
|
|
|
|
|
INS = 0x3D,
|
|
|
|
|
Data = ByteOperation.GenerateEmptyArray(8)
|
2023-01-29 18:43:53 +01:00
|
|
|
|
};
|
2023-01-30 23:27:00 +01:00
|
|
|
|
Console.WriteLine(cmd_WriteData.ToString());
|
|
|
|
|
APDUResponse response = nfcService.Transmit(cmd_WriteData);
|
|
|
|
|
Console.WriteLine(response.ToString());
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
byte[] data = desfire.ReadData(FileID, 0, FileSize);
|
|
|
|
|
Console.WriteLine(Encoding.ASCII.GetString(data).Replace("\u0000", ""));
|
2023-01-29 18:43:53 +01:00
|
|
|
|
|
2023-01-30 23:27:00 +01:00
|
|
|
|
nfcService.Disconnect();
|
2023-01-29 18:43:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|