mirror of
https://gitlab.com/fabinfra/fabaccess/borepin.git
synced 2025-06-11 11:03:23 +02:00
Finished: NFC Lib v0.9, TODO: CMAC check, Change PICC Key
This commit is contained in:
@ -71,10 +71,10 @@ namespace NFC.ISO7816_4
|
||||
return string.Format(pattern_case2, CLA, INS, P1, P2, Le);
|
||||
case IsoCase.Case3Short:
|
||||
case IsoCase.Case3Extended:
|
||||
return string.Format(pattern_case3, CLA, INS, P1, P2, Lc, BitConverter.ToString(Data).Replace("-", " "));
|
||||
return string.Format(pattern_case3, CLA, INS, P1, P2, Lc, BitConverter.ToString(Data).Replace("-", "").ToLower());
|
||||
case IsoCase.Case4Short:
|
||||
case IsoCase.Case4Extended:
|
||||
return string.Format(pattern_case4, CLA, INS, P1, P2, Lc, BitConverter.ToString(Data).Replace("-", " "), Le);
|
||||
return string.Format(pattern_case4, CLA, INS, P1, P2, Lc, BitConverter.ToString(Data).Replace("-", "").ToLower(), Le);
|
||||
default:
|
||||
throw new Exception("Unknown IsoCase");
|
||||
}
|
||||
|
@ -146,8 +146,15 @@ namespace NFC.ISO7816_4
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string pattern = "SW1: 0x{0:x} | SW2: 0x{1:x} | Body: 0x{2:x}";
|
||||
return string.Format(pattern, SW1, SW2, Body);
|
||||
if(Body == null)
|
||||
{
|
||||
return string.Format("SW1: 0x{0:x} | SW2: 0x{1:x}", SW1, SW2);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format("SW1: 0x{0:x} | SW2: 0x{1:x} | Body: {2:x}", SW1, SW2, BitConverter.ToString(Body).Replace("-", "").ToLower());
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
@ -412,40 +412,54 @@ namespace NFC.Mifare_DESFire
|
||||
|
||||
#region DESFire Commands
|
||||
/// <summary>
|
||||
/// Format PICC
|
||||
/// Works after PICC Authentication
|
||||
/// Select Application by ApplicationID (AID)
|
||||
/// </summary>
|
||||
public void Format()
|
||||
/// <param name="aid">3 Byte AID</param>
|
||||
public void SelectApplication(UInt32 aid)
|
||||
{
|
||||
_Log.Debug("Start Format");
|
||||
if(aid > 0xFFFFFF)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("AID is too large");
|
||||
}
|
||||
|
||||
APDUCommand cmd_format = new APDUCommand(IsoCase.Case2Short)
|
||||
byte[] id_byte = BitConverter.GetBytes(aid);
|
||||
_Log.InfoFormat("Select Application: {0}", HexConverter.ConvertToHexString(id_byte.ToArray()));
|
||||
|
||||
APDUCommand cmd_SelectApplication = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = 0xFC,
|
||||
INS = (byte)APDUInstructions.SELECT_APPLICATION,
|
||||
Data = new byte[]
|
||||
{
|
||||
id_byte[0],
|
||||
id_byte[1],
|
||||
id_byte[2]
|
||||
}
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_format): {0}", HexConverter.ConvertToHexString(cmd_format.ToArray()));
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_format);
|
||||
_Log.DebugFormat("APDU_RES(cmd_format): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.Debug(cmd_SelectApplication.ToString());
|
||||
HexConverter.ConvertToHexString(cmd_SelectApplication.ToArray());
|
||||
APDUResponse response = _Card.Transmit(cmd_SelectApplication);
|
||||
_Log.DebugFormat(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
_Log.Debug("End Format");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate to PICC, with ISO Authenticate
|
||||
/// Authenticate to PICC, with ISO Authenticate for DES Key
|
||||
/// </summary>
|
||||
/// <param name="key_id">0x01 - 0x0D</param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="key">Array of 8/16 Bytes</param>
|
||||
/// <param name="rndA">!!! WARNING For Testing only !!!</param>
|
||||
/// <exception cref="AuthenticationDelayException">Retry after short Time</exception>
|
||||
public void AuthenticateISO_DES(byte key_id, byte[] key, byte[] rndA = null)
|
||||
{
|
||||
_Log.Debug("Start AuthenticateISO_DES");
|
||||
if(key_id >= 0x0E)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("KeyID is invalid");
|
||||
}
|
||||
|
||||
// Sepearte InitialisationVector for Authentication
|
||||
_Log.InfoFormat("Authenticate with DES Key No: 0x{0:x}", key_id);
|
||||
|
||||
// Sepearte Initialisation Vector for Authentication Process
|
||||
byte[] iv = new byte[8];
|
||||
|
||||
APDUCommand cmd_challange_request = new APDUCommand(IsoCase.Case4Short)
|
||||
@ -457,10 +471,10 @@ namespace NFC.Mifare_DESFire
|
||||
key_id
|
||||
}
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_challange_request): {0}", HexConverter.ConvertToHexString(cmd_challange_request.ToArray()));
|
||||
_Log.Debug(cmd_challange_request.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_challange_request);
|
||||
_Log.DebugFormat("APDU_RES(cmd_challange_request): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
@ -476,7 +490,7 @@ namespace NFC.Mifare_DESFire
|
||||
byte[] rndB_rl = RotateLeft(rndB);
|
||||
_Log.DebugFormat("rndB_enc: {0}", HexConverter.ConvertToHexString(rndB_rl));
|
||||
|
||||
if(rndA == null)
|
||||
if (rndA == null)
|
||||
{
|
||||
Random rnd = new Random();
|
||||
rndA = new byte[8];
|
||||
@ -488,7 +502,7 @@ namespace NFC.Mifare_DESFire
|
||||
_Log.DebugFormat("rndAB: {0}", HexConverter.ConvertToHexString(rndAB));
|
||||
|
||||
byte[] rndAB_enc = des.Encrypt(rndAB, key, rndB_enc);
|
||||
_Log.DebugFormat("rndA_rndB_enc: {0}", HexConverter.ConvertToHexString(rndAB_enc));
|
||||
_Log.DebugFormat("rndAB_enc: {0}", HexConverter.ConvertToHexString(rndAB_enc));
|
||||
iv = ExtractLastBlock(rndAB_enc, 8);
|
||||
|
||||
APDUCommand cmd_challange_response = new APDUCommand(IsoCase.Case4Short)
|
||||
@ -497,10 +511,10 @@ namespace NFC.Mifare_DESFire
|
||||
INS = 0xAF,
|
||||
Data = rndAB_enc
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_challange_response): {0}", HexConverter.ConvertToHexString(cmd_challange_response.ToArray()));
|
||||
_Log.Debug(cmd_challange_response.ToString());
|
||||
|
||||
response = _Card.Transmit(cmd_challange_response);
|
||||
_Log.DebugFormat("APDU_RES(cmd_challange_response): {0}", HexConverter.ConvertToHexString(cmd_challange_response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
@ -515,28 +529,52 @@ namespace NFC.Mifare_DESFire
|
||||
|
||||
if (!rndA.SequenceEqual(rndAFromCard))
|
||||
{
|
||||
throw new Exception("Authentication failed, PICC Challenge is not corret");
|
||||
throw new Exception("Authentication failed, PICC Challenge is invalid.");
|
||||
}
|
||||
|
||||
_Log.Info("Authenticated");
|
||||
|
||||
_SessionKey = GenerateSesionKey_DES(rndA, rndB);
|
||||
_Log.DebugFormat("_SessionKey: {0}", HexConverter.ConvertToHexString(_SessionKey));
|
||||
_Log.DebugFormat("SessionKey: {0}", HexConverter.ConvertToHexString(_SessionKey));
|
||||
|
||||
_IV = GenerateEmptyArray(8);
|
||||
_Log.DebugFormat("_IV: {0}", HexConverter.ConvertToHexString(_IV));
|
||||
|
||||
_Log.Debug("End AuthenticateISO_DES");
|
||||
_Log.DebugFormat("IV: {0}", HexConverter.ConvertToHexString(_IV));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create Application for ID
|
||||
/// Format PICC
|
||||
/// Need Authentication for PICC / Application 0x000000
|
||||
/// </summary>
|
||||
public void Format()
|
||||
{
|
||||
_Log.Info("Format PICC");
|
||||
|
||||
APDUCommand cmd_format = new APDUCommand(IsoCase.Case2Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = 0xFC,
|
||||
};
|
||||
_Log.Debug(cmd_format.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_format);
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create Application for ApplicationID
|
||||
/// </summary>
|
||||
/// <param name="aid">3 Byte ID</param>
|
||||
public void CreateApplication(UInt32 aid, byte keysetting1, byte keysetting2)
|
||||
{
|
||||
_Log.Debug("Start CreateApplication");
|
||||
if (aid > 0xFFFFFF)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("AID is too large");
|
||||
}
|
||||
|
||||
byte[] id_byte = BitConverter.GetBytes(aid);
|
||||
_Log.DebugFormat("AID: {0}", HexConverter.ConvertToHexString(id_byte.ToArray()));
|
||||
_Log.InfoFormat("Create Application: {0}", HexConverter.ConvertToHexString(id_byte.ToArray()));
|
||||
|
||||
APDUCommand cmd_CreateApplication = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
@ -551,58 +589,28 @@ namespace NFC.Mifare_DESFire
|
||||
keysetting2
|
||||
}
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_CreateApplication): {0}", HexConverter.ConvertToHexString(cmd_CreateApplication.ToArray()));
|
||||
_Log.Debug(cmd_CreateApplication.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_CreateApplication);
|
||||
_Log.DebugFormat("APDU_RES(cmd_CreateApplication): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
_Log.Debug("End CreateApplication");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Select Application by AID
|
||||
/// </summary>
|
||||
/// <param name="aid">3 Byte AID</param>
|
||||
public void SelectApplication(UInt32 aid)
|
||||
{
|
||||
_Log.Debug("Start SelectApplication");
|
||||
|
||||
byte[] id_byte = BitConverter.GetBytes(aid);
|
||||
_Log.DebugFormat("AID: {0}", HexConverter.ConvertToHexString(id_byte.ToArray()));
|
||||
|
||||
APDUCommand cmd_SelectApplication = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = (byte)APDUInstructions.SELECT_APPLICATION,
|
||||
Data = new byte[]
|
||||
{
|
||||
id_byte[0],
|
||||
id_byte[1],
|
||||
id_byte[2]
|
||||
}
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_SelectApplication): {0}", HexConverter.ConvertToHexString(cmd_SelectApplication.ToArray()));
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_SelectApplication);
|
||||
_Log.DebugFormat("APDU_RES(cmd_SelectApplication): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
_Log.Debug("End SelectApplication");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authenticate to PICC, with ISO Authenticate
|
||||
/// </summary>
|
||||
/// <param name="key_id">0x01 - 0x0D</param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="key">Array of 16 Bytes</param>
|
||||
/// <param name="rndA">!!! WARNING For Testing only !!!</param>
|
||||
/// <exception cref="AuthenticationDelayException">Retry after short Time</exception>
|
||||
public void AuthenticateISO_AES(byte key_id, byte[] key, byte[] rndA = null)
|
||||
{
|
||||
_Log.Debug("Start AuthenticateISO_AES");
|
||||
if (key_id >= 0x0E)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("KeyID is invalid");
|
||||
}
|
||||
|
||||
_Log.InfoFormat("Authenticate with AES Key No: 0x{0:x}", key_id);
|
||||
|
||||
// Sepearte InitialisationVector for Authentication
|
||||
byte[] iv = new byte[16];
|
||||
@ -616,10 +624,10 @@ namespace NFC.Mifare_DESFire
|
||||
key_id
|
||||
}
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_challange_request): {0}", HexConverter.ConvertToHexString(cmd_challange_request.ToArray()));
|
||||
_Log.Debug(cmd_challange_request.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_challange_request);
|
||||
_Log.DebugFormat("APDU_RES(cmd_challange_request): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
@ -647,7 +655,7 @@ namespace NFC.Mifare_DESFire
|
||||
_Log.DebugFormat("rndAB: {0}", HexConverter.ConvertToHexString(rndAB));
|
||||
|
||||
byte[] rndAB_enc = aes.Encrypt(rndAB, key, rndB_enc);
|
||||
_Log.DebugFormat("rndA_rndB_enc: {0}", HexConverter.ConvertToHexString(rndAB_enc));
|
||||
_Log.DebugFormat("rndAB_enc: {0}", HexConverter.ConvertToHexString(rndAB_enc));
|
||||
iv = ExtractLastBlock(rndAB_enc, 16);
|
||||
|
||||
APDUCommand cmd_challange_response = new APDUCommand(IsoCase.Case4Short)
|
||||
@ -656,10 +664,10 @@ namespace NFC.Mifare_DESFire
|
||||
INS = 0xAF,
|
||||
Data = rndAB_enc
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_challange_response): {0}", HexConverter.ConvertToHexString(cmd_challange_response.ToArray()));
|
||||
_Log.Debug(cmd_challange_response.ToString());
|
||||
|
||||
response = _Card.Transmit(cmd_challange_response);
|
||||
_Log.DebugFormat("APDU_RES(cmd_challange_response): {0}", HexConverter.ConvertToHexString(cmd_challange_response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
@ -674,83 +682,30 @@ namespace NFC.Mifare_DESFire
|
||||
|
||||
if (!rndA.SequenceEqual(rndAFromCard))
|
||||
{
|
||||
throw new Exception("Authentication failed, PICC Challenge is not corret");
|
||||
throw new Exception("Authentication failed, PICC Challenge is invalid.");
|
||||
}
|
||||
|
||||
_SessionKey = GenerateSesionKey_AES(rndA, rndB);
|
||||
_Log.DebugFormat("_SessionKey: {0}", HexConverter.ConvertToHexString(_SessionKey));
|
||||
_Log.DebugFormat("SessionKey: {0}", HexConverter.ConvertToHexString(_SessionKey));
|
||||
|
||||
_IV = GenerateEmptyArray(16);
|
||||
_Log.DebugFormat("_IV: {0}", HexConverter.ConvertToHexString(_IV));
|
||||
|
||||
_Log.Debug("End AuthenticateISO_DES");
|
||||
_Log.DebugFormat("IV: {0}", HexConverter.ConvertToHexString(_IV));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change Same DES key as Authenticated
|
||||
/// Change AES key, the same as Authenticated
|
||||
/// </summary>
|
||||
/// <param name="key_id"></param>
|
||||
/// <param name="new_key"></param>
|
||||
/// <param name="key_version"></param>
|
||||
public void ChangeKey_DES(byte key_id, byte[] new_key, byte key_version)
|
||||
{
|
||||
_Log.Debug("Start ChangeKey_DES");
|
||||
|
||||
byte[] header = new byte[]
|
||||
{
|
||||
0xC4, key_id
|
||||
};
|
||||
_Log.DebugFormat("header: {0}", HexConverter.ConvertToHexString(header));
|
||||
|
||||
byte[] key_and_version = new_key;
|
||||
byte[] command = Concatenate(header, key_and_version);
|
||||
_Log.DebugFormat("command: {0}", HexConverter.ConvertToHexString(command));
|
||||
|
||||
CRC32 crc32 = new CRC32();
|
||||
byte[] crc = crc32.Calculate(command);
|
||||
_Log.DebugFormat("crc: {0}", HexConverter.ConvertToHexString(crc));
|
||||
|
||||
byte[] cryptogram = Concatenate(key_and_version, crc);
|
||||
_Log.DebugFormat("cryptogram: {0}", HexConverter.ConvertToHexString(cryptogram));
|
||||
|
||||
byte[] cryptogram_block = ExpandToBlockSize(cryptogram, 16);
|
||||
_Log.DebugFormat("cryptogram_block: {0}", HexConverter.ConvertToHexString(cryptogram_block));
|
||||
|
||||
TDES des = new TDES();
|
||||
byte[] cryptogram_enc = des.Encrypt(cryptogram_block, _SessionKey, _IV);
|
||||
_Log.DebugFormat("cryptogram_enc: {0}", HexConverter.ConvertToHexString(cryptogram_enc));
|
||||
|
||||
_IV = ExtractLastBlock(cryptogram_enc, 8);
|
||||
_Log.DebugFormat("_IV: {0}", HexConverter.ConvertToHexString(_IV));
|
||||
|
||||
byte[] data = Concatenate(new byte[] { key_id }, cryptogram_enc);
|
||||
_Log.DebugFormat("data: {0}", HexConverter.ConvertToHexString(data));
|
||||
|
||||
APDUCommand cmd_ChangeKey = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = 0xC4,
|
||||
Data = data
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_ChangeKey): {0}", HexConverter.ConvertToHexString(cmd_ChangeKey.ToArray()));
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_ChangeKey);
|
||||
_Log.DebugFormat("APDU_RES(cmd_ChangeKey): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
_Log.Debug("End ChangeKey_AES");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change Same AES key as Authenticated
|
||||
/// </summary>
|
||||
/// <param name="key_id"></param>
|
||||
/// <param name="new_key"></param>
|
||||
/// <param name="key_version"></param>
|
||||
/// <param name="key_id">0x01 - 0x0D</param>
|
||||
/// <param name="new_key">Array of 16 Bytes</param>
|
||||
/// <param name="key_version">Version of Key(min. 0x10)</param>
|
||||
public void ChangeKey_AES(byte key_id, byte[] new_key, byte key_version)
|
||||
{
|
||||
_Log.Debug("Start ChangeKey_AES");
|
||||
if (key_id >= 0x0E)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("KeyID is invalid");
|
||||
}
|
||||
|
||||
_Log.InfoFormat("Change AES Key No: 0x{0:x}", key_id);
|
||||
|
||||
byte[] header = new byte[]
|
||||
{
|
||||
@ -789,25 +744,28 @@ namespace NFC.Mifare_DESFire
|
||||
INS = 0xC4,
|
||||
Data = data
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_ChangeKey): {0}", HexConverter.ConvertToHexString(cmd_ChangeKey.ToArray()));
|
||||
_Log.Debug(cmd_ChangeKey.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_ChangeKey);
|
||||
_Log.DebugFormat("APDU_RES(cmd_ChangeKey): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
_Log.Debug("End ChangeKey_AES");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Change other AES key as Authenticated
|
||||
/// Change AES key, other than Authenticated
|
||||
/// </summary>
|
||||
/// <param name="key_id"></param>
|
||||
/// <param name="new_key"></param>
|
||||
/// <param name="key_version"></param>
|
||||
/// <param name="key_id">0x01 - 0x0D</param>
|
||||
/// <param name="new_key">Array of 16 Bytes</param>
|
||||
/// <param name="key_version">Version of Key(min. 0x10)</param>
|
||||
public void ChangeOtherKey_AES(byte key_id, byte[] new_key, byte[] old_key, byte key_version)
|
||||
{
|
||||
_Log.Debug("Start ChangeOtherKey_AES");
|
||||
if (key_id >= 0x0E)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("KeyID is invalid");
|
||||
}
|
||||
|
||||
_Log.InfoFormat("Change AES Key No: 0x{0:x}", key_id);
|
||||
|
||||
byte[] header = new byte[]
|
||||
{
|
||||
@ -851,28 +809,28 @@ namespace NFC.Mifare_DESFire
|
||||
INS = 0xC4,
|
||||
Data = data
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_ChangeKey): {0}", HexConverter.ConvertToHexString(cmd_ChangeKey.ToArray()));
|
||||
_Log.Debug(cmd_ChangeKey.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_ChangeKey);
|
||||
_Log.DebugFormat("APDU_RES(cmd_ChangeKey): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
_Log.Debug("End ChangeOtherKey_AES");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create Standard Data File
|
||||
/// </summary>
|
||||
/// <param name="file_id">ID of File (0x01 - 0x10)</param>
|
||||
/// <param name="file_id">ID of File (0x00 - 0x20)</param>
|
||||
/// <param name="communication">Type of File Communicaton</param>
|
||||
/// <param name="accessRights">Access Rights for File</param>
|
||||
/// <param name="size">Size of File in Bytes</param>
|
||||
public void CreateFile_Standard(byte file_id, FileCommunication communication, UInt16 accessRights, UInt32 size)
|
||||
{
|
||||
_Log.Debug("Start CreateFile_Standard");
|
||||
|
||||
_Log.DebugFormat("FID: {0}", file_id);
|
||||
if (file_id >= 0x20)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("FileID is to large");
|
||||
}
|
||||
_Log.DebugFormat("Create STD File: {0}", file_id);
|
||||
|
||||
byte[] accessRights_byte = BitConverter.GetBytes(accessRights);
|
||||
byte[] size_byte_tolong = BitConverter.GetBytes(size);
|
||||
@ -896,97 +854,27 @@ namespace NFC.Mifare_DESFire
|
||||
INS = (byte)APDUInstructions.CREATE_STDDATAFILE,
|
||||
Data = Concatenate(data, accessRights_byte, size_byte)
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_CreateFile_Standard): {0}", HexConverter.ConvertToHexString(cmd_CreateFile_Standard.ToArray()));
|
||||
_Log.Debug(cmd_CreateFile_Standard.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_CreateFile_Standard);
|
||||
_Log.DebugFormat("APDU_RES(cmd_CreateFile_Standard): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.DebugFormat(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
_Log.Debug("End CreateFile_Standard");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read Data from File
|
||||
/// </summary>
|
||||
/// <param name="file_id">ID of File (0x01 - 0x10)</param>
|
||||
/// <param name="offset">Offset for File</param>
|
||||
/// <param name="length">Lenght of Data</param>
|
||||
public byte[] ReadData(byte file_id, UInt32 offset, UInt32 length)
|
||||
{
|
||||
_Log.Debug("Start ReadData");
|
||||
|
||||
int max_read_bytes_pre_transaction = 47;
|
||||
long bytes_readed = 0;
|
||||
|
||||
List<byte> read_data = new List<byte>();
|
||||
|
||||
while (bytes_readed != length)
|
||||
{
|
||||
byte[] data = new byte[]
|
||||
{
|
||||
file_id
|
||||
};
|
||||
|
||||
byte[] offset_byte_tolong = BitConverter.GetBytes(offset + bytes_readed);
|
||||
// Use only 3 Bytes
|
||||
byte[] offset_byte = new byte[]
|
||||
{
|
||||
offset_byte_tolong[0],
|
||||
offset_byte_tolong[1],
|
||||
offset_byte_tolong[2],
|
||||
};
|
||||
|
||||
long bytes_toread = 0;
|
||||
|
||||
if (length - bytes_readed < max_read_bytes_pre_transaction)
|
||||
{
|
||||
bytes_toread = length - bytes_readed;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytes_toread = max_read_bytes_pre_transaction;
|
||||
}
|
||||
|
||||
byte[] length_byte_tolong = BitConverter.GetBytes(bytes_toread);
|
||||
bytes_readed += bytes_toread;
|
||||
|
||||
// Use only 3 Bytes
|
||||
byte[] length_byte = new byte[]
|
||||
{
|
||||
length_byte_tolong[0],
|
||||
length_byte_tolong[1],
|
||||
length_byte_tolong[2],
|
||||
};
|
||||
|
||||
APDUCommand cmd_ReadData = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = (byte)APDUInstructions.READ_DATA,
|
||||
Data = Concatenate(data, offset_byte, length_byte)
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_ReadData): {0}", HexConverter.ConvertToHexString(cmd_ReadData.ToArray()));
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_ReadData);
|
||||
_Log.DebugFormat("APDU_RES(cmd_ReadData): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
read_data.AddRange(response.Body);
|
||||
}
|
||||
_Log.Debug("End ReadData");
|
||||
|
||||
return read_data.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write Data to File
|
||||
/// </summary>
|
||||
/// <param name="file_id">ID of File (0x01 - 0x10)</param>
|
||||
/// <param name="file_id">ID of File (0x00 - 0x20)</param>
|
||||
/// <param name="offset">Offset for File</param>
|
||||
/// <param name="data">Data to write</param>
|
||||
public void WriteData(byte file_id, UInt32 offset, byte[] data)
|
||||
{
|
||||
_Log.Debug("Start WriteData");
|
||||
if (file_id >= 0x20)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("FileID is to large");
|
||||
}
|
||||
_Log.DebugFormat("Write Data to File: {0}", file_id);
|
||||
|
||||
int max_write_bytes_pre_transaction = 47;
|
||||
byte[] write_buffer;
|
||||
@ -1040,15 +928,91 @@ namespace NFC.Mifare_DESFire
|
||||
INS = (byte)APDUInstructions.WRITE_DATA,
|
||||
Data = Concatenate(file_id_array, offset_byte, length_byte, write_buffer)
|
||||
};
|
||||
_Log.DebugFormat("APDU_CMD(cmd_WriteData): {0}", HexConverter.ConvertToHexString(cmd_WriteData.ToArray()));
|
||||
_Log.Debug(cmd_WriteData.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_WriteData);
|
||||
_Log.DebugFormat("APDU_RES(cmd_WriteData): {0}", HexConverter.ConvertToHexString(response.ToArray()));
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
}
|
||||
}
|
||||
|
||||
_Log.Debug("End WriteData");
|
||||
/// <summary>
|
||||
/// Read Data from File
|
||||
/// </summary>
|
||||
/// <param name="file_id">ID of File (0x00 - 0x20)</param>
|
||||
/// <param name="offset">Offset for File</param>
|
||||
/// <param name="length">Lenght of Data</param>
|
||||
public byte[] ReadData(byte file_id, UInt32 offset, UInt32 length)
|
||||
{
|
||||
if (file_id >= 0x20)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("FileID is to large");
|
||||
}
|
||||
_Log.DebugFormat("Read Data from File: {0}", file_id);
|
||||
|
||||
int max_read_bytes_pre_transaction = 47;
|
||||
long bytes_readed = 0;
|
||||
byte[] readbuffer = new byte[47];
|
||||
|
||||
List<byte> read_data = new List<byte>();
|
||||
|
||||
while (bytes_readed != length)
|
||||
{
|
||||
byte[] data = new byte[]
|
||||
{
|
||||
file_id
|
||||
};
|
||||
|
||||
byte[] offset_byte_tolong = BitConverter.GetBytes(offset + bytes_readed);
|
||||
// Use only 3 Bytes
|
||||
byte[] offset_byte = new byte[]
|
||||
{
|
||||
offset_byte_tolong[0],
|
||||
offset_byte_tolong[1],
|
||||
offset_byte_tolong[2],
|
||||
};
|
||||
|
||||
long bytes_toread = 0;
|
||||
|
||||
if (length - bytes_readed < max_read_bytes_pre_transaction)
|
||||
{
|
||||
bytes_toread = length - bytes_readed;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytes_toread = max_read_bytes_pre_transaction;
|
||||
}
|
||||
|
||||
byte[] length_byte_tolong = BitConverter.GetBytes(bytes_toread);
|
||||
bytes_readed += bytes_toread;
|
||||
|
||||
// Use only 3 Bytes
|
||||
byte[] length_byte = new byte[]
|
||||
{
|
||||
length_byte_tolong[0],
|
||||
length_byte_tolong[1],
|
||||
length_byte_tolong[2],
|
||||
};
|
||||
|
||||
APDUCommand cmd_ReadData = new APDUCommand(IsoCase.Case4Short)
|
||||
{
|
||||
CLA = 0x90,
|
||||
INS = (byte)APDUInstructions.READ_DATA,
|
||||
Data = Concatenate(data, offset_byte, length_byte)
|
||||
};
|
||||
_Log.Debug(cmd_ReadData.ToString());
|
||||
|
||||
APDUResponse response = _Card.Transmit(cmd_ReadData);
|
||||
_Log.Debug(response.ToString());
|
||||
|
||||
CheckAPDUResponse(response);
|
||||
|
||||
// Remove CMAC from Body
|
||||
read_data.AddRange(GetSubArray(response.Body, 0, bytes_toread));
|
||||
}
|
||||
|
||||
return read_data.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user