/// <summary> /// Ask user for keys /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void BtnUnlockClick(object sender, EventArgs e) { string message = String.Format("Please enter the key to read sector {0}:", SectorNumber); KeyForm keyForm = new KeyForm(message, KeyForm.KeyType.reading); DialogResult r = keyForm.ShowDialog(); if (r == DialogResult.Cancel) { return; } MemoryCardMifareClassic.Sector newSector = new MemoryCardMifareClassic.Sector(SectorNumber); if (!newSector.setReadingKeyFromString(keyForm.getUserKey())) { MessageBox.Show(this.Parent, "The application is unable to use the specified key.", "Internal error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MemoryCardMifareClassic.Sector readedSector = Card.ReadSectorWithKey(newSector); if (readedSector == null) { MessageBox.Show(this.Parent, "There has been an error while reading the sector.", "Reading failed", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } this.Sector = readedSector; PrintData(); }
/// <summary> /// Change sector's access conditions /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void BtChangeAccesConditionsClick(object sender, EventArgs e) { if (this.Sector == null) { return; } bool proposeToTypeKeys = (!Sector.isSetKeyA() && !Sector.isSetKeyB()) || !Sector.isSetKeyA() || !Sector.isSetKeyB(); if (proposeToTypeKeys) { DialogResult AccessResult = MessageBox.Show(this.Parent, "Do you know the keys to unlock this sector?", "Authentication required", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (AccessResult == DialogResult.Yes) { UnlockForm unlockForm = new UnlockForm(this.Sector); DialogResult unlockFormResult = unlockForm.ShowDialog(); if (unlockFormResult != DialogResult.Cancel) { this.Sector = unlockForm.getSectorControl(); // Keys are set Card.ReadSectorTrailer(ref this.Sector); } } } MifareSectorTrailerDefinitionForm accessConditions = new MifareSectorTrailerDefinitionForm(this.Sector); DialogResult r = accessConditions.ShowDialog(); if (r == DialogResult.Cancel) { return; } bool success = false; if (Card.WriteSectorTrailer(this.Sector)) { success = true; } else { DialogResult result = MessageBox.Show(this.Parent, "Failed to change the access conditions.\nDo you know the key to write this sector?", "Authentication required", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { string message = String.Format("Please enter the key to write sector {0}:", Sector.Number); KeyForm keyForm = new KeyForm(message, KeyForm.KeyType.writing); DialogResult resdial = keyForm.ShowDialog(); if (resdial != DialogResult.Cancel) { if (this.Sector.setWritingKeyFromString(keyForm.getUserKey())) { if (Card.WriteSectorTrailer(this.Sector)) { success = true; } else { MessageBox.Show(this.Parent, "There has been an error while writing the sector's trailer.", "Writing failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(this.Parent, "The application is unable to use the specified key.", "Internal error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } if (success) { MessageBox.Show(this.Parent, "The sectors's trailer has been successfully updated !", "Writing succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information); } return; }
/// <summary> /// Write changes to card /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void BtWriteToCardClick(object sender, EventArgs e) { // @TODO: Interdire la modification du block 0 byte[] b = getDataFromHexbox(); if (b == null) { return; } MemoryCardMifareClassic.Sector newSector = new MemoryCardMifareClassic.Sector(Sector.Number); /* Fill the sector data */ int firstBlockAddress = MemoryCardMifareClassic.SectorFirstBlockAddress(Sector.Number); int lastBlockAddress = MemoryCardMifareClassic.SectorTrailerAddress(Sector.Number) - 1; // Excluded trailer def. int startingAddress = 0; for (int address = 0; address < 3; address++) { byte[] data = new byte[16]; Array.Copy(b, startingAddress, data, 0, 16); startingAddress += 16; Logger.Trace("Content " + BinConvert.ToHex(data)); newSector.SetBlock(address, data); } bool success = false; /* Write data */ if (Card.WriteSector(newSector)) { success = true; } else { // Can't write, we are going to try with a key string message = String.Format("Please enter the key to write sector {0}:", SectorNumber); KeyForm keyForm = new KeyForm(message, KeyForm.KeyType.writing); if (keyForm.ShowDialog() != DialogResult.Cancel) { if (newSector.setWritingKeyFromString(keyForm.getUserKey())) { if (Card.WriteSector(newSector)) { success = true; } else { MessageBox.Show(this.Parent, "There has been an error while writing the sector.", "Writing failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(this.Parent, "The application is unable to use the specified key.", "Internal error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } if (success) { MessageBox.Show(this.Parent, "The sectors's content has been successfully updated !", "Writing succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information); } btWriteToCard.Enabled = false; }