private void WriteNdefToMifareStandard(PcscSdk.MifareStandard.AccessHandler mifare, byte[] ndefData) { InitializeMifareStandard(mifare); const int capacity = 15 * 3 * 16; byte[] wrappedData = GenerateTLVData(ndefData); if (wrappedData.Length > capacity) { throw new NfcHandlerException("Data size of " + wrappedData.Length + " bytes exceeds capacity of " + capacity + " bytes!"); } int data_length = wrappedData.Length; Array.Resize(ref wrappedData, (data_length / 16) * 16 + 16); byte writeBlock = 3; for (byte pos = 0; pos < wrappedData.Length; pos += 16) { if (++writeBlock % 4 == 3) { writeBlock++; } mifare.Write(writeBlock, wrappedData.Skip(pos).Take(16).ToArray(), GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA, 1); } StatusMessage?.Invoke("Written " + data_length + " bytes of data. Ndef message length is " + ndefData.Length + " bytes."); }
private void InitializeMifareStandard(PcscSdk.MifareStandard.AccessHandler mifare) { byte[] mad1 = mifare.Read(1, PcscSdk.MifareStandard.GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA, 0); byte[] mad2 = mifare.Read(2, PcscSdk.MifareStandard.GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA, 0); // One of my readers only has two key slots, another one does not allow overwriting them, but has at least 3. So here we go... // Also, the Microsoft IFD emulation(?) reports 0x0000 in response, but it still seems to have worked. byte factoryKeySlot = 0; try { mifare.LoadKey(PcscSdk.MifareStandard.DefaultKeys.FactoryDefault, 0); StatusMessage?.Invoke("Loaded factory default key in slot 0."); } catch (ApduFailedException e) { if (e.Response.SW == 0x0000 && e.Response.ResponseData.Length == 0 && mifare.CardReader.Name.Contains("Microsoft IFD")) { StatusMessage?.Invoke("Loaded factory default key in slot 0. (MS Quirk)"); } else { StatusMessage?.Invoke("Could not re-load to slot 0, trying slot 2: " + e.Message); mifare.LoadKey(PcscSdk.MifareStandard.DefaultKeys.FactoryDefault, 2); StatusMessage?.Invoke("Loaded factory default key in slot 2."); factoryKeySlot = 2; } } if (!mad1.SequenceEqual(ndefMadData[1]) || !mad2.SequenceEqual(ndefMadData[2])) { StatusMessage?.Invoke("Writing NDEF MAD block."); for (byte block = 1; block < 4; block++) { StatusMessage?.Invoke("Writing MAD block " + block); try { mifare.Write(block, ndefMadData[block], GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA, factoryKeySlot); } catch (Exception) { mifare.Write(block, ndefMadData[block], GeneralAuthenticate.GeneralAuthenticateKeyType.PicoTagPassKeyB, factoryKeySlot); } } } for (int sector = 1; sector < 16; sector++) { byte block = (byte)(sector * 4 + 3); try { for (int i = 1; i < 4; i++) { var authRes = mifare.CardReader.Transceive(new PcscSdk.MifareStandard.GeneralAuthenticate((byte)(block - i), 1, GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA)); if (!authRes.Succeeded) { StatusMessage?.Invoke("NDEF read authentication failed, trying trailer rewrite."); throw new Exception(); } } byte[] sectorIdent = mifare.Read(block, GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA, 1); if (!sectorIdent.SequenceEqual(ndefComparator)) { throw new Exception(); } } catch (Exception) { StatusMessage?.Invoke("Writing NDEF trailer into sector " + sector); try { mifare.Write(block, ndefSectorIdent, GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA, factoryKeySlot); } catch (Exception) { mifare.Write(block, ndefSectorIdent, GeneralAuthenticate.GeneralAuthenticateKeyType.PicoTagPassKeyB, factoryKeySlot); } } } StatusMessage?.Invoke("Tag is NDEF formated"); }