Android Question Read \ Write NCF Midfare Clasic

Situ LLC

Active Member
Licensed User
Greetings clever friends
My question is this
From the examples of writing and reading with the NFC library that are in the forum, I have seen many questions and problems with use.
I have an example that is partly functional. You can read sector 0 and block 0,1,2,3, until then perfect.
But if I try to read Sector 1 blocks 4,5,6,7 it is not successful, even though the key authentication is correct in both transactions.

Look . Try to read Sector =0 Block = 0

Techs: [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
c7 63 d8 33
UUID ok
ID c7 63 d8 33
Connect: true

Block Read 0 success true Flag 0
Sector 0 Block 0 Result= c763d8334f0804006263646566676869
Success, even on block 01,2,3

\----------------------------------------------------------------------------------------/


Now i will try to read Sector 1, blocks 4

Techs: [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
c7 63 d8 33
UUID ok
ID c7 63 d8 33
Connected: true
authentication del sector. Success=true, Flag=0

Sector =1 Block = 4 MSG

Block Read 4 Estado false Flag 0

cannot read

\------------------------------------------------------------------------------------------/

As you can see, the authentication is fine according to what can be seen, because it leaves only sector 0, the others
are not, the authentication is fine, I use a new 1K Midface card

In writing despite having successful sector authentication and the correct block number
Sector 0, block 1


The result


Techs: [android.nfc.tech.MifareClassic, android.nfc.tech.NfcA, android.nfc.tech.Ndef]
c7 63 d8 33
UUID ok
ID c7 63 d8 33
Connected: true
authentication del sector. Success=true, Flag=0
Sector =0 Block = 1
writing was = false, Flag=0
Write Error: null

I would like to know if anyone has already managed to overcome these problems and share the solution.
I am fully available to pay for help. Thank you



Write \ Read Midafare clasic:
#Region  Project Attributes
    #ApplicationLabel: NFC_SITULLC
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region
Sub Process_Globals
    Private prevIntent As Intent
    Public sector As Int=0
    Public Block As Int = 0
    Private message As String =""
    Private evento As Boolean
  
End Sub

Sub Globals
    Private nfc As NFC
    Private TagTech As TagTechnology
    Private LeerBlock As Button
    Private Escribir_block As Button
End Sub
 

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    evento= True ' try read\write
End Sub

Sub Activity_Resume
    Try
        nfc.EnableForegroundDispatch
        Dim si As Intent = Activity.GetStartingIntent
        If si.IsInitialized = False Or si = prevIntent Then Return
        prevIntent = si
        Dim tag As JavaObject
        If si.Action.EndsWith("TECH_DISCOVERED") Or si.Action.EndsWith("NDEF_DISCOVERED") Or si.Action.EndsWith("TAG_DISCOVERED") Then
            Dim techs As List = nfc.GetTechList(si)
            Log($"Techs: ${techs}"$)
      
            Dim Id0() As  Byte
            Dim Id(8) As  String
            Dim jTagTech As JavaObject
            'NfcA
            If techs.IndexOf("android.nfc.tech.NfcA")> -1 Then
                TagTech.Initialize("TagTech", "android.nfc.tech.MifareClassic", si)
                TagTech.Connect
                jTagTech  = TagTech
              
                tag   = jTagTech.RunMethod("getTag", Null)
                Id0   = tag.RunMethod("getId", Null)
              
                If Id0(0)>0 Or Id0(1)>0 Or Id0(2)>0 Or Id0(3)>0  Then
                  
                    Id(0)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(0))),2)
                    Id(1)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(1))),2)
                    Id(2)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(2))),2)
                    Id(3)=    Right("0" & Bit.ToHexString(ToUnsigned(Id0(3))),2)
              
                    Log(Id(0)&" "&Id(1)&" "&Id(2)&" "&Id(3))
                    Log("UUID ok")
                    LogColor("Numero de tarjeta "& Id(0)&" "&Id(1)&" "&Id(2)&" "&Id(3),0xFF0F26FC)
                Else
                    LogColor("Error en Serial de Tarjeta ",0xFFFF0000)
                End If
            Else
                ToastMessageShow("Tag does not support Ndef.", True)
            End If
        End If
    Catch
        Log(LastException)
        LogColor("Erro 1 "&LastException,0xFFFF0000)
    End Try
End Sub


Private Sub TagTech_Connected (Success As Boolean)
    LogColor($" Conectado : ${Success}"$,0xFF001FFF)
 
    Dim key() As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5)
  
    sector =0
    Block  =1
  
    If Success = False Then
        ToastMessageShow("Error connecting to tag", True)
        Log(LastException)
    Else
      
        Try 
            TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyA", Array(sector,  key), 0)
             Catch
            ToastMessageShow("Ath1:"&LastException,False)
        End Try
 
    End If
End Sub

Private Sub Escribir_block_Click
End Sub
 
Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
  
    Log($"Autenticacion del sector . Success=${Success}, Flag=${Flag}"$)
  
    If Success Then
      
        Log( "Sector  ="& sector& "    Block  = "& Block & " MSG "&message)
      
        Try
          
            If evento  Then
              
                TagTech.RunAsync("readBlock0","readBlock", Array(Block), 0)
              
                Else
                  
                TagTech.RunAsync("writeBlock", "writeBlock", Array(Block,message), 0)
            End If
 
        Catch
            ToastMessageShow("Read2:"&LastException,False)
            LogColor("Read2:"&LastException,0xFFFF0000)
        End Try
    Else
        LogColor("Read error flag :"& Flag,0xFFFF0000)
    End If
End Sub


Private Sub Authenticate1_RunAsync (Flag As Int, Success As Boolean, Result As Object)
  
    Log($"Autenticacion del sector . Success=${Success}, Flag=${Flag}"$)
    Block=2
    If Success Then
        Log( "Sector  ="& sector& "    Block  = "& Block )
        Try
          
            TagTech.RunAsync("readBlock0","readBlock", Array(Block), 0)
          
        Catch
          
            ToastMessageShow("Read2:"&LastException,False)
            LogColor("Read2:"&LastException,0xFFFF0000)
          
        End Try
    Else
        LogColor("Read error flag :"& Flag,0xFFFF0000)
    End If
End Sub

Private Sub Authenticate2_RunAsync (Flag As Int, Success As Boolean, Result As Object)
  
    Log($"Autenticacion del sector . Success=${Success}, Flag=${Flag}"$)
    Block=3
    If Success Then
        Log( "Sector  ="& sector& "    Block  = "& Block )
        Try
            TagTech.RunAsync("readBlock0","readBlock", Array(Block), 0)
          
        Catch
            ToastMessageShow("Read2:"&LastException,False)
            LogColor("Read2:"&LastException,0xFFFF0000)
        End Try
    Else
        LogColor("Read error flag :"& Flag,0xFFFF0000)
    End If
End Sub





Private Sub writeBlock_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    LogColor($"Escritura de block fue =${Success}, Flag=${Flag}"$,0xFF0037FF)
  
    If Success Then
        LogColor("Escritura  ok : "&Result ,0xFF0037FF)
    Else
        LogColor("Escritura  Error : "&Result ,0xFFFF0000)
      
      
    End If
End Sub





Private Sub readBlock0_RunAsync(Flag As Int, Success As Boolean, Result As Object)
  
    LogColor("Block Read "&Block & "  Estado "&Success &" Flag "&Flag ,0xFF0037FF)
  
  
'    Log(LastException)
  
    If Success Then
        If Result<>Null Then
            Dim myBlock() As Byte = Result
            Dim sb As StringBuilder
            sb.Initialize
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(0))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(1))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(2))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(3))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(4))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(5))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(6))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(7))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(8))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(9))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(10))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(11))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(12))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(13))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(14))),2))
            sb.Append(Right("0" & Bit.ToHexString(ToUnsigned(myBlock(15))),2))
                  
            Log("Contenido Sector "&sector &" Block  "& Block &"  "  &sb)
            
            TagTech.Close ' important'
          
        End If
    Else
        Log("cannot read")
    End If
End Sub




Sub Activity_Pause (UserClosed As Boolean)
    nfc.DisableForegroundDispatch
End Sub

Sub ToUnsigned(b As Byte) As Int
    Dim x As String
    x= Bit.And(0xFF, b)
    Return x
End Sub

Sub Right(Text As String, Length As Int) As String
    If Length>Text.Length Then Length=Text.Length
    Return Text.SubString(Text.Length-Length)
End Sub
 
\------------------------------------------------------------------------------------------



[B]I would like to know if anyone has already managed to overcome these problems and share the solution.
I am fully available to pay for help.
Thank you[/B]

1700760661441.png
 

Attachments

  • 1700760568315.png
    1700760568315.png
    13.9 KB · Views: 94
Last edited:

drgottjr

Expert
Licensed User
Longtime User
i had a little difficulty followng your description, but it looks like you're trying to write to sector 0. if that's what you're doing, you can't. it's protected.
 
Upvote 0

Situ LLC

Active Member
Licensed User
Thanks for responding.

I am really clear about the restrictions of each of the blocks.
What I want to explain is that sector 0 is possible to read blocks 0,1,2,3

The problem is when changing Sector 1 and reading blocks 4,5,6,7 already gives an error.

Validation and authentication are correct in any sector,
I know the restrictions on Midfare cards, it's just that these examples don't work correctly.

The Midfare card es new.
Thanks
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
at no point in the code that you posted do you refer to sector as anything but 0. hence my inference that you were trying to write to sector 0. but it sounds like you know all there is to know, so i'm sure you'll resolve things.
 
Upvote 0

Situ LLC

Active Member
Licensed User
I think you misunderstand.

I said I know the restrictions on midfare cards
and if you see I said I can read sector 0 block 0,1,2,3, but if I change sectors and appears that it is authorized and authenticated .
it does not read the content of sector 1 in terms of showing the content of blocks 4, 5,6,7

About r writing, I try to write sector 0, block 1, which has no restriction.

FOR REASON, I ASK, could it be a problem with the library, or with the way they are using the method calls?

Thanks for your appreciation
 
Upvote 0
Top