novotnyllc / MiFare

MiFare Classic support for UWP and Windows Desktop apps
MIT License
37 stars 29 forks source link

Sample for read and write Mifare 1k with KeyA and KeyB #1

Open ansega opened 8 years ago

ansega commented 8 years ago

I want to use this project to read and write Mifare 1k cards, I know the values for KeyA and KeyB but I can't find the proper combination to perform the read and write over the card.

There is a way for achieve this, I can't see a sample who uses such values.

I try to put together that but I'm stuck with the order of the actions.

After the card is detected and the card object is filled, I try for sector 9:

From the Card Added Event I have:

this.Card = args.SmartCard.CreateMiFareCard();

Outside the event on a button I want to get the sector to Authenticate with the keys

var sector = Card.GetSector(9);

But the authentication fails with Message:

System.AggregateException: One or more errors occurred. ---> MiFare.Classic.CardLoginException: Unable to login in sector 9 with key A or B
   at MiFare.Classic.Sector.<GetDataBlockInt>d__25.MoveNext() in C:\Users\Oren\Source\Git\MiFareRT\src\MiFare.Shared\Classic\Sector.cs:line 222
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at System.Threading.Tasks.Task`1.get_Result()
   at MiFare.Classic.Sector.set_KeyA(String value) in C:\Users\Oren\Source\Git\MiFareRT\src\MiFare.Shared\Classic\Sector.cs:line 67
   at SpeeronCheckIn.Hardware.CardReadersWriters.Iso14443_PcSc_Implementation.Write(LogMetaDataEntity callerData, String[] text) in C:\Users\Speeron\Documents\SpeeronTemp\TestProjects\EncodersTest\SpeeronCheckIn.Hardware.CardReadersWriters\Iso14443_PcSc_Implementation.cs:line 262
---> (Inner Exception #0) MiFare.Classic.CardLoginException: Unable to login in sector 9 with key A or B
   at MiFare.Classic.Sector.<GetDataBlockInt>d__25.MoveNext() in C:\Users\Oren\Source\Git\MiFareRT\src\MiFare.Shared\Classic\Sector.cs:line 222<---

Looks like I can write the cards and something is missing but I can't find the way, any sugestion to make it work in Mifare 1K?

kathmann commented 8 years ago

I have exactly the same issue, when trying to write I get the "Unable to login in sector X with key B" on a factory-default Mifare Classic 1K.

Any help?

dschinnerl commented 7 years ago

Not sure if this is really the problem in your cases but for anyone hitting the same issue... we tested w/ Mifare 1K and 4K cards on a Omnikey 5421 card reader. Our solution was to modify code a bit...

HTH

nicopretorius commented 5 years ago

Not sure if this is really the problem in your cases but for anyone hitting the same issue... we tested w/ Mifare 1K and 4K cards on a Omnikey 5421 card reader. Our solution was to modify code a bit...

  • LoadKeysTransmissionType.Volatile = 0x20 // needs to be added in Pcsc.cs
  • MifareStandardCommands.cs#LoadKey needs to modified so that LoadKeysTransmissionType. Volatile is used instead of .Plain

HTH

Thanks this worked in my case as well.

Burialarmach commented 2 years ago

Not sure if this is really the problem in your cases but for anyone hitting the same issue... we tested w/ Mifare 1K and 4K cards on a Omnikey 5421 card reader. Our solution was to modify code a bit...

  • LoadKeysTransmissionType.Volatile = 0x20 // needs to be added in Pcsc.cs
  • MifareStandardCommands.cs#LoadKey needs to modified so that LoadKeysTransmissionType. Volatile is used instead of .Plain

HTH im using a ACR122U and even with this fix i can't write or login in the sector, do the reader really matter? moreover with other library i could write on card but not on a wrist band what im getting wrong?