Android Question MifareClassic

Pantelis Loizou

Member
Licensed User
Longtime User
Hi Erel

Thanks for your reply. I've already read that thread but i am facing some problems reading block sectors. i will appreciated for any help. below is my code. Reading success is always false

B4X:
Sub Activity_Resume
    nfc.EnableForegroundDispatch
    Dim si As Intent = Activity.GetStartingIntent
    If si.IsInitialized = False Or si = prevIntent Then Return
    prevIntent = si
    If si.Action.EndsWith("TECH_DISCOVERED") Or si.Action.EndsWith("TAG_DISCOVERED") Then
        Dim techs As List = nfc.GetTechList(si)
        Log($"Techs: ${techs}"$)
        If techs.IndexOf("android.nfc.tech.MifareUltralight") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.MifareUltralight" , si)
            TagTech.Connect
        Else If techs.IndexOf("android.nfc.tech.MifareClassic") > -1 Then
            TagTech.Initialize("TagTech", "android.nfc.tech.MifareClassic" , si)
            TagTech.Connect
        Else
            ToastMessageShow("Tag does not support Ndef.", True)
        End If
    End If
End Sub


Private Sub TagTech_Connected (Success As Boolean)
    Dim key(6) As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5)  ' Fake key for the forum
    Dim sector As Int=0
    TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyA", Array( sector, key ), 0)
    
'    Log($"Connected: ${Success}"$)
'    If Success = False Then
'        ToastMessageShow("Error connecting to tag", True)
'        Log(LastException)
'    Else
'        ReadNdef
'    End If
End Sub

Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to auth. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        Dim blockIndex As Int=0
        TagTech.RunAsync("readBlock","", Array(blockIndex), 0)
    End If
End Sub

Private Sub readBlock_RunAsync(Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to read. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        If Result<>Null Then
            Dim myBlock() As Byte = Result
        End If
    End If
End Sub
 
Upvote 0

Pantelis Loizou

Member
Licensed User
Longtime User
Logs below
B4X:
Finished trying to auth. Success=true, Flag=0
B4X:
Finished trying to read. Success=false, Flag=0
B4X:
Techs: [android.nfc.tech.NfcA, android.nfc.tech.MifareClassic, android.nfc.tech.NdefFormatable]
 
Upvote 0

Pantelis Loizou

Member
Licensed User
Longtime User
I TRY IT BUT NO RESULT
B4X:
Private Sub TagTech_Connected (Success As Boolean)
    Dim key() As Byte = Array As Byte(0xA1,0xA2,0xA3,0xA4,0xA5,0xA6)
    Dim sector As Int=0
    TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyA", Array(sector, key), 0)
End Sub

Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to auth. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        Dim blockIndex As Int=0
        TagTech.RunAsync("ReadBlock","readBlock",Array(blockIndex),0)
    End If
End Sub

Private Sub ReadBlock_RunAsync(Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to read. Success=${Success}, Flag=${Flag}"$)
    Log(LastException)
    If Success Then
        If Result<>Null Then
            Dim myBlock() As Byte = Result
        End If
    End If
End Sub
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Perhaps the used key has no read permissions (just a guess)

So, even if you can authenticate a sector with a key, it does not grant that you can read it.

Have you checked that the card is readable using that key with an external Mifare app?
 
Upvote 0

Pantelis Loizou

Member
Licensed User
Longtime User
hi again

i am using mifare classic tool to read a card and attached you may find two different screenshots with results. i am trying also to read sector 5 which includes data but unfortunately i cannot. only sector 0 is readable. i spend more than a month to find a solution but nothing yet. what i am doing wrong? for sector 0 i use 0xA0A1A2A3A4A5 and for sector 5 i use 0xFFFFFFFFFF. Could somebody help me. i will appreciate it very much
 

Attachments

  • Screenshot_20190829-130626_MIFARE_Classic_Tool[1].jpg
    Screenshot_20190829-130626_MIFARE_Classic_Tool[1].jpg
    339.2 KB · Views: 320
  • Screenshot_20190829-114437_MIFARE_Classic_Tool[1].jpg
    Screenshot_20190829-114437_MIFARE_Classic_Tool[1].jpg
    305.7 KB · Views: 408
Upvote 0
B4X:
#Region  Project Attributes
    #ApplicationLabel: Advanced NFC
    #VersionCode: 2
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region


'        5=5h
'        -21=ebh
'        35=23h
'        -71=b9h
'
'        05
'        eb
'        23
'        b9

Sub Process_Globals
    Private prevIntent As Intent
    Dim jTagTech As JavaObject   
    Dim tag As JavaObject
    Dim Id0() As  Byte
    Dim Id(8) As  String
    'Dim key(6) As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5)  ' Fake key for the forum
    Dim key() As Byte = Array As Byte(0xA0,0xA1,0xA2,0xA3,0xA4,0xA5)
    Dim key() As Byte = Array As Byte(0xFF,0xFF,0xFF,0xFF,0xFF,0xFF)
    Dim sector As Int=0
            
End Sub

Sub Globals
    Private nfc As NFC
    Private TagTech As TagTechnology
    
    Private rdbRead As RadioButton
    Private ListView1 As ListView
    Private rdbWrite As RadioButton
    Private flTxt1 As FloatLabeledEditText
    Private flTxt2 As FloatLabeledEditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Activity.LoadLayout("1")
    ListView1.SingleLineLayout.Label.TextSize = 15
    ListView1.SetColorAnimated(Colors.Red,Colors.Blue,Colors.Black)
    flTxt2.EditText.ForceDoneButton = True
End Sub

Sub Activity_Resume 'ok
    Try
        
    'forces all nfc intents to be sent to this activity
    nfc.EnableForegroundDispatch
    Dim si As Intent = Activity.GetStartingIntent
    'check that the intent is a new intent
    If si.IsInitialized = False Or si = prevIntent Then Return
    prevIntent = si
    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}"$)
        
 
            If techs.IndexOf("android.nfc.tech.MifareClassic")> -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))
                
                ListView1.AddSingleLine(Id(0)&" "&Id(1)&" "&Id(2)&" "&Id(3))
            End If         
            
            Else
                ToastMessageShow("Tag does not support Ndef.", True)
            End If
    End If
    Catch
        Log(LastException)
'        Dim in As Intent
'        in.Initialize("android.settings.NFC_SETTINGS", "")
'        StartActivity(in)
    
    End Try
End Sub

Private Sub TagTech_Connected (Success As Boolean)
    Log($"Connected: ${Success}"$)
    If Success = False Then
        
        ToastMessageShow("Error connecting to tag", True)
        Log(LastException)
    Else
        TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyB", Array( sector, key ), 0)
  
    End If
End Sub
 
Private Sub Authenticate_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to auth. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        Dim blockIndex As Int=1
        TagTech.RunAsync("readBlock","readBlock", Array(blockIndex), 0)
    End If
End Sub

Private Sub readBlock_RunAsync(Flag As Int, Success As Boolean, Result As Object)
    Log($"Finished trying to read. Success=${Success}, Flag=${Flag}"$)
    Log(LastException)
    If Success Then
        If Result<>Null Then
            Dim myBlock() As Byte = Result
            ListView1.AddSingleLine(">:"&Result)
        End If
    Else
        ListView1.AddSingleLine("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
'check:

but error
1605938239622.png
 
Upvote 0
Top