Updated to .NET 7

This commit is contained in:
TheJoKlLa 2023-01-29 18:43:53 +01:00
parent 4420dbf1b1
commit d03c9cacdb
9 changed files with 152 additions and 11 deletions

View File

@ -1,9 +1,24 @@
namespace NFC using System;
namespace NFC
{ {
public class APDUResponse public class APDUResponse
{ {
public byte SW1 { get; set; } public byte SW1 { get; set; }
public byte SW2 { get; set; } public byte SW2 { get; set; }
public byte[] Body { get; set; } public byte[] Body { get; set; }
public override string ToString()
{
if(Body != null)
{
return string.Format("SW: {0:x} {1:x} Body: {2:x}", SW1, SW2, BitConverter.ToString(Body).Replace("-", "").ToLower());
}
else
{
return string.Format("SW: {0:x} {1:x} Body: null", SW1, SW2);
}
}
} }
} }

View File

@ -5,6 +5,10 @@ using NFC.Helper;
using NFC.Helper.Crypto.Cipher; using NFC.Helper.Crypto.Cipher;
using NFC.Helper.Crypto.CRC; using NFC.Helper.Crypto.CRC;
using NFC.Interfaces; using NFC.Interfaces;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Parameters;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -738,6 +742,28 @@ namespace NFC.Cards.NXP_MIFARE_DESFire
}; };
_Log.Debug(cmd_WriteData.ToString()); _Log.Debug(cmd_WriteData.ToString());
IBlockCipher cipher = new AesEngine();
CMac mac = new CMac(cipher, 8 * 8);
KeyParameter keyParameter = new KeyParameter(_SessionKey);
mac.Init(keyParameter);
byte[] mac_buffer = ByteOperation.GenerateEmptyArray(8);
byte[] cmd_raw_buffer = cmd_WriteData.ToArray();
mac.BlockUpdate(ByteOperation.Concatenate(new byte[] { 0x90, 0x3D }, cmd_WriteData.Data), 0, cmd_WriteData.Data.Length);
mac.DoFinal(mac_buffer, 0);
cmd_WriteData.Data = ByteOperation.Concatenate(cmd_WriteData.Data, mac_buffer);
_Log.Debug(cmd_WriteData.ToString());
APDUResponse response = _Card.Transmit(cmd_WriteData); APDUResponse response = _Card.Transmit(cmd_WriteData);
_Log.Debug(response.ToString()); _Log.Debug(response.ToString());

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net7.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net7.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -423,9 +423,9 @@ namespace NFC_Test.Cards
SW2 = 0x00 SW2 = 0x00
}; };
card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "905a00000333221100")).Returns(response); card.Transmit(Arg.Is<APDUCommand>(x => HexConverter.ConvertToHexString(x.ToArray()) == "905a00000342414600")).Returns(response);
desfire.SelectApplication(0x112233); desfire.SelectApplication(0x464142);
} }
[Test] [Test]

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net7.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,100 @@
using NFC;
using NFC.Cards.NXP_MIFARE_DESFire;
using NFC.Cards.NXP_MIFARE_DESFire.Enums;
using NFC.Helper;
using NFC.Helper.Crypto;
using NFC.Interfaces;
using NFC_PCSC;
using NUnit.Framework;
using System;
using System.Text;
namespace NFC_Test.REAL
{
/// <summary>
/// Test all DESFire Commands with an Empty Card
/// 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>
public readonly string ReaderID = "ACS ACR122U PICC Interface 0";
#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()
{
IHardware hardware = new Hardware_PCSC();
IReader reader = hardware.OpenReader(ReaderID);
bool test_successfully = false;
ReaderEventHandler handler = (sender, card) =>
{
card.Connect();
NXP_MIFARE_DESFire desfire = new NXP_MIFARE_DESFire(card);
desfire.SelectApplication(0x000000);
CipherKey key = new CipherKey(CipherType.TDES);
desfire.AuthenticateISO_DES(0x00, key._Key);
desfire.Format();
desfire.AuthenticateISO_DES(0x00, key._Key);
byte keysetting1 = desfire.GenerateKeySetting1(ChangeApplicationKey.MASTERKEY, ChangeMasterKeySettings.WITHMASTERKEY, CreateDeleteFile.ONLYMASTERKEY, FileDirectoryAccess.NOKEY, ChangeMasterKey.CHANGEABLE);
byte keysetting2 = desfire.GenerateKeySetting2(CryptoOperationsType.AES, FileIdentifies.NOTUSED, 2);
desfire.CreateApplication(ApplicationID, keysetting1, keysetting2);
desfire.SelectApplication(ApplicationID);
CipherKey key_aes = new CipherKey(CipherType.AES);
desfire.AuthenticateISO_AES(0x00, key_aes._Key);
UInt16 accesRights = desfire.GenerateFileAccessRights((byte)FileAccessRights.FREE, 0x00, 0x00, 0x00);
desfire.CreateFile_Standard(FileID, FileCommunication.ENCRYPT, accesRights, FileSize);
desfire.WriteData(FileID, 0, Encoding.ASCII.GetBytes("Test1234"));
APDUCommand cmd_WriteData = new APDUCommand(IsoCase.Case4Short)
{
CLA = 0x90,
INS = 0x3D,
Data = ByteOperation.GenerateEmptyArray(8)
};
Console.WriteLine(cmd_WriteData.ToString());
APDUResponse response = card.Transmit(cmd_WriteData);
Console.WriteLine(response.ToString());
byte[] data = desfire.ReadData(FileID, 0, FileSize);
Console.WriteLine(Encoding.ASCII.GetString(data).Replace("\u0000", ""));
test_successfully = true;
card.Disconnect();
};
reader.CardDiscovered += handler;
reader.Start();
Assert.AreEqual(true, test_successfully);
reader.Stop();
reader.CardDiscovered -= handler;
}
}
}

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net7.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>