diff --git a/RFiDGear/DataAccessLayer/Remote/FromIO/RFiDDevice.cs b/RFiDGear/DataAccessLayer/Remote/FromIO/RFiDDevice.cs index c0f5f8c..bdd5b5a 100644 --- a/RFiDGear/DataAccessLayer/Remote/FromIO/RFiDDevice.cs +++ b/RFiDGear/DataAccessLayer/Remote/FromIO/RFiDDevice.cs @@ -414,7 +414,7 @@ public ERROR ReadMiFareClassicSingleSector(int sectorNumber, string aKey, string } catch { - return ERROR.NoError; + return ERROR.AuthenticationError; } return ERROR.NoError; } @@ -705,7 +705,8 @@ public ERROR ReadMiFareClassicSingleBlock(int _blockNumber, string _aKey, string public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSector, string _aKeyToUse, string _bKeyToUse, string _aKeyToWrite, string _bKeyToWrite, - byte[] buffer, byte _madGPB, bool _useMADToAuth = false, bool _keyToWriteUseMAD = false) + string _madAKeyToUse, string _madBKeyToUse, string _madAKeyToWrite, string _madBKeyToWrite, + byte[] buffer, byte _madGPB, SectorAccessBits _sab, bool _useMADToAuth = false, bool _keyToWriteUseMAD = false) { var settings = new SettingsReaderWriter(); Sector = new MifareClassicSectorModel(); @@ -718,11 +719,11 @@ public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSecto var mAKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_aKeyToWrite) ? _aKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_aKeyToWrite) }; var mBKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_bKeyToWrite) ? _bKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_bKeyToWrite) }; - var madAKeyToUse = mAKeyToUse; - var madBKeyToUse = mBKeyToUse; + var madAKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madAKeyToUse) ? _madAKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madAKeyToUse) }; + var madBKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madBKeyToUse) ? _madBKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madBKeyToUse) }; - var madAKeyToWrite = mAKeyToWrite; - var madBKeyToWrite = mBKeyToWrite; + var madAKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madAKeyToWrite) ? _madAKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madAKeyToWrite) }; + var madBKeyToWrite = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madBKeyToWrite) ? _madBKeyToWrite : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madBKeyToWrite) }; try { @@ -757,13 +758,15 @@ public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSecto MifareAccessInfo aiToWrite = new MifareAccessInfoClass { - UseMAD = _keyToWriteUseMAD + UseMAD = _keyToWriteUseMAD, + }; - aiToWrite.MADKeyA.Value = _aKeyToUse == _aKeyToWrite ? madAKeyToUse.Value : madAKeyToWrite.Value; - aiToWrite.MADKeyB.Value = _bKeyToUse == _bKeyToWrite ? madBKeyToUse.Value : madBKeyToWrite.Value; + aiToWrite.MADKeyA.Value = _aKeyToUse == _madAKeyToWrite ? madAKeyToUse.Value : madAKeyToWrite.Value; + aiToWrite.MADKeyB.Value = _bKeyToUse == _madBKeyToWrite ? madBKeyToUse.Value : madBKeyToWrite.Value; aiToWrite.KeyA.Value = _aKeyToUse == _aKeyToWrite ? mAKeyToUse.Value : mAKeyToWrite.Value; aiToWrite.KeyB.Value = _bKeyToUse == _bKeyToWrite ? mBKeyToUse.Value : mBKeyToWrite.Value; aiToWrite.MADGPB = _madGPB; + aiToWrite.SAB = _sab; var aiToUse = new MifareAccessInfoClass { @@ -804,7 +807,7 @@ public ERROR WriteMiFareClassicWithMAD(int _madApplicationID, int _madStartSecto return ERROR.NoError; } - public ERROR ReadMiFareClassicWithMAD(int madApplicationID, string _aKeyToUse, string _bKeyToUse, int _length, byte _madGPB, bool _useMADToAuth = true, bool _aiToUseIsMAD = false) + public ERROR ReadMiFareClassicWithMAD(int madApplicationID, string _aKeyToUse, string _bKeyToUse, string _madAKeyToUse, string _madBKeyToUse, int _length, byte _madGPB, bool _useMADToAuth = true, bool _aiToUseIsMAD = false) { var settings = new SettingsReaderWriter(); Sector = new MifareClassicSectorModel(); @@ -814,8 +817,8 @@ public ERROR ReadMiFareClassicWithMAD(int madApplicationID, string _aKeyToUse, s var mAKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_aKeyToUse) ? _aKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_aKeyToUse) }; var mBKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_bKeyToUse) ? _bKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_bKeyToUse) }; - var madAKeyToUse = mAKeyToUse; - var madBKeyToUse = mBKeyToUse; + var madAKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madAKeyToUse) ? _madAKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madAKeyToUse) }; + var madBKeyToUse = new MifareKey() { Value = CustomConverter.KeyFormatQuickCheck(_madBKeyToUse) ? _madBKeyToUse : CustomConverter.FormatMifareClassicKeyWithSpacesEachByte(_madBKeyToUse) }; try { diff --git a/RFiDGear/Model/MifareClassic/MifareClassicSectorModel.cs b/RFiDGear/Model/MifareClassic/MifareClassicSectorModel.cs index 4189e39..b39532e 100644 --- a/RFiDGear/Model/MifareClassic/MifareClassicSectorModel.cs +++ b/RFiDGear/Model/MifareClassic/MifareClassicSectorModel.cs @@ -225,6 +225,9 @@ public string AccessBitsAsString public AccessCondition_MifareClassicSectorTrailer Read_KeyB { get => SectorAccessCondition.Read_KeyB; set => SectorAccessCondition.Read_KeyB = value; } public AccessCondition_MifareClassicSectorTrailer Write_KeyB { get => SectorAccessCondition.Read_KeyB; set => SectorAccessCondition.Read_KeyB = value; } + public LibLogicalAccess.SectorAccessBits SAB { get => sab; set => sab = value; } + private LibLogicalAccess.SectorAccessBits sab; + public bool IsAuthenticated { get; set; } public short Cx { get => SectorAccessCondition.Cx; set => SectorAccessCondition.Cx = value; } @@ -240,8 +243,6 @@ private bool decodeSectorTrailer(byte[] st, ref MifareClassicSectorModel _sector { uint C1x, C2x; - LibLogicalAccess.SectorAccessBits sab; - uint tmpAccessBitCx; if (CustomConverter.SectorTrailerHasWrongFormat(st)) @@ -468,9 +469,13 @@ private string encodeSectorTrailer(MifareClassicSectorModel _sector) // DataBlock 0 = C1/0; C2/0; C3/0 - st[1] |= (byte)((dataBlock0AccessBitsIndex & 0x01) << 4); // C1/0 - st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x02) >> 1); // C2/0 - st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x04) << 2); // C3/0 + st[1] |= (byte)((dataBlock0AccessBitsIndex & 0x0001) << 4); // C1/0 + st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x0002) >> 1); // C2/0 + st[2] |= (byte)((dataBlock0AccessBitsIndex & 0x0004) << 2); // C3/0 + + sab.d_data_block0_access_bits.c1 |= (short)(dataBlock0AccessBitsIndex & 0x0001); + sab.d_data_block0_access_bits.c2 |= (short)((dataBlock0AccessBitsIndex & 0x0002) >> 1); + sab.d_data_block0_access_bits.c3 |= (short)((dataBlock0AccessBitsIndex & 0x0004) >> 2); // DataBlock 1 = C1/1; C2/1; C3/1 @@ -478,18 +483,30 @@ private string encodeSectorTrailer(MifareClassicSectorModel _sector) st[2] |= (byte)(dataBlock1AccessBitsIndex & 0x02); // C2/1 st[2] |= (byte)((dataBlock1AccessBitsIndex & 0x04) << 3); // C3/1 + sab.d_data_block1_access_bits.c1 |= (short)(dataBlock1AccessBitsIndex & 0x01); + sab.d_data_block1_access_bits.c2 |= (short)((dataBlock1AccessBitsIndex & 0x02) >> 1); + sab.d_data_block1_access_bits.c3 |= (short)((dataBlock1AccessBitsIndex & 0x04) >> 2); + // DataBlock 2 = C1/2; C2/2; C3/2 st[1] |= (byte)((dataBlock2AccessBitsIndex & 0x01) << 6); // C1/2 st[2] |= (byte)((dataBlock2AccessBitsIndex & 0x02) << 1); // C2/2 st[2] |= (byte)((dataBlock2AccessBitsIndex & 0x04) << 4); // C3/2 + sab.d_data_block2_access_bits.c1 |= (short)(dataBlock2AccessBitsIndex & 0x01); + sab.d_data_block2_access_bits.c2 |= (short)((dataBlock2AccessBitsIndex & 0x02) >> 1); + sab.d_data_block2_access_bits.c3 |= (short)((dataBlock2AccessBitsIndex & 0x04) >> 2); + // SectorAccessBits = C1/3; C2/3; C3/3 st[1] |= (byte)((sectorAccessBitsIndex & 0x01) << 7); // C1/3 st[2] |= (byte)((sectorAccessBitsIndex & 0x02) << 2); // C2/3 st[2] |= (byte)((sectorAccessBitsIndex & 0x04) << 5); // C3/3 + sab.d_sector_trailer_access_bits.c1 |= (short)(sectorAccessBitsIndex & 0x01); + sab.d_sector_trailer_access_bits.c2 |= (short)((sectorAccessBitsIndex & 0x02) >> 1); + sab.d_sector_trailer_access_bits.c3 |= (short)((sectorAccessBitsIndex & 0x04) >> 2); + st = CustomConverter.buildSectorTrailerInvNibble(st); string[] stAsString; diff --git a/RFiDGear/View/TaskViews/RFIDTasks/MifareClassicTask/MifareClassicTaskTabPages/TabPageMifareClassicKeySetupView.xaml b/RFiDGear/View/TaskViews/RFIDTasks/MifareClassicTask/MifareClassicTaskTabPages/TabPageMifareClassicKeySetupView.xaml index 93be74d..d6f458c 100644 --- a/RFiDGear/View/TaskViews/RFIDTasks/MifareClassicTask/MifareClassicTaskTabPages/TabPageMifareClassicKeySetupView.xaml +++ b/RFiDGear/View/TaskViews/RFIDTasks/MifareClassicTask/MifareClassicTaskTabPages/TabPageMifareClassicKeySetupView.xaml @@ -42,99 +42,204 @@ IsEnabled="{Binding IsClassicAuthInfoEnabled}">