borepin/NFC/Mifare DESFire/MifareDESFire.cs
2020-09-17 16:07:58 +02:00

112 lines
2.9 KiB
C#

using PCSC.Iso7816;
using System;
using System.Collections.Generic;
using System.Text;
namespace NFC.Mifare_DESFire
{
public class MifareDESFire
{
/// <summary>
/// Create new Application with AID
/// </summary>
/// <param name="aid">Appilication ID</param>
public APDUCommand CreateApplication(UInt64 aid)
{
throw new NotImplementedException();
}
public APDUCommand GetApplicationIDs()
{
APDUCommand cmd = new APDUCommand(IsoCase.Case2Short)
{
CLA = 0x90,
INS = (byte)APDUInstructions.GET_APPLICATION_IDS
};
return cmd;
}
public UInt32[] ConvertApplicationIDs(APDUResponse response)
{
if(response.Body.Length % 3 != 0)
{
throw new Exception("Invalid Body Length.");
}
List<UInt32> applicationIDs = new List<UInt32>();
for(int i = 0; i < response.Body.Length; i += 3)
{
UInt32 new_applicationID = 0;
new_applicationID = (UInt32)((response.Body[i] << 16) + (response.Body[i + 1] << 8) + response.Body[i + 2]);
applicationIDs.Add(new_applicationID);
}
return applicationIDs.ToArray();
}
/// <summary>
/// Select Application by ID
/// </summary>
/// <param name="id">3 Byte ID</param>
public APDUCommand SelectApplication(UInt32 id)
{
byte[] id_byte = BitConverter.GetBytes(id);
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short)
{
CLA = 0x90,
INS = (byte)APDUInstructions.SELECT_APPLICATION,
Data = new byte[]
{
id_byte[0],
id_byte[1],
id_byte[2]
},
Le = 0x00
};
return cmd;
}
/// <summary>
/// Select Application by ID
/// </summary>
/// <param name="id">3 Byte ID</param>
public APDUCommand CreateApplication(UInt32 id)
{
byte[] id_byte = BitConverter.GetBytes(id);
APDUCommand cmd = new APDUCommand(IsoCase.Case4Short)
{
CLA = 0x90,
INS = (byte)APDUInstructions.CREATE_APPLICATION,
Data = new byte[]
{
id_byte[0],
id_byte[1],
id_byte[2]
},
Le = 0x00
};
return cmd;
}
public byte GenerateKeySetting1()
{
return 0x00;
}
/// <summary>
///
/// </summary>
public enum ChangeKey : byte
{
MASTERKEY = 0x00,
}
}
}