Android Question Error in Read Write Mifare Clasic Sector 2 and biger

hello
i can read block 0 and 1 and 2 and 3 of sector 0 from mifare clasic 1k s50

1- but when i want read sector 2 or more. it have Error
2- i cannot write on mifare
my code is:
B4X:
Sub Activity_Resume 'ok
    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))
               
                    ListView1.AddSingleLine(Id(0)&" "&Id(1)&" "&Id(2)&" "&Id(3))
                Else
                    ListView1.AddSingleLine("error in Id")
                End If
            Else
                ToastMessageShow("Tag does not support Ndef.", True)
            End If
    End If
    Catch
        Log(LastException)
        ListView1.AddSingleLine("error:"&LastException)
'        Dim in As Intent
'        in.Initialize("android.settings.NFC_SETTINGS", "")
'        StartActivity(in)
   
    End Try
End Sub

part of read is:
NFC - MIFARE CLASSIC 1K S50:
Private Sub TagTech_Connected (Success As Boolean)
    Log($"Connected: ${Success}"$)
    sector=txtSector.Text
   
    If kA0.Checked Then
        key(0)=0xA0
        key(1)=0xA1
        key(2)=0xA2
        key(3)=0xA3
        key(4)=0xA4
        key(5)=0xA5
       
    else if kFF.Checked Then
        key(0)=0xFF
        key(1)=0xFF
        key(2)=0xFF
        key(3)=0xFF
        key(4)=0xFF
        key(5)=0xFF
       
    End If
   
    If Success = False Then
        ToastMessageShow("Error connecting to tag", True)
        Log(LastException)
    Else
        Try
            If ka.Checked Then
                TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyA", Array(sector,  key), 0)
            Else
                TagTech.RunAsync("Authenticate", "authenticateSectorWithKeyB", Array(sector,  key), 0)
            End If
'            Authenticate
        Catch
            ToastMessageShow("Ath1:"&LastException,False)
        End Try
 
    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=txtBLK.Text
'        TagTech.RunAsync("blockToSector","blockToSector",Array(sector),0)
        Try
            Dim message  As String
            message="pedramghaneipou"
           
            If rdbWrite.Checked Then
                TagTech.RunAsync("writeBlock", "writeBlock", Array(blockIndex,message), 0)
            Else
                TagTech.RunAsync("readBlock","readBlock", Array(blockIndex), 0)
            End If
        Catch
            ToastMessageShow("Read2:"&LastException,False)
        End Try
    Else
        ListView1.AddSingleLine("Read error flag :"& Flag)
    End If
End Sub

Private Sub writeBlock_RunAsync (Flag As Int, Success As Boolean, Result As Object)
    Log($"Writing completed. Success=${Success}, Flag=${Flag}"$)
    If Success Then
        ListView1.AddSingleLine("Write ok:"&Result)
    Else  
        ListView1.AddSingleLine("Write error:"&Result)
       
    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(txtSector.Text & ">"&txtBLK.Text&":  "& _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(0))),2)&" "&  _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(1))),2)&" "&  _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(2))),2)&" "&  _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(3))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(4))),2)&" "&  _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(5))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(6))),2)&" "&  _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(7))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(8))),2)&" "&  _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(9))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(10))),2)&" "&  _
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(11))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(12))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(13))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(14))),2)&" "&  _            
                Right("0" & Bit.ToHexString(ToUnsigned(myBlock(15))),2)&" "  _
                )
            TagTech.Close
        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

variables;
B4X:
Sub Process_Globals
    Private prevIntent As Intent
    '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 rdbWrite As RadioButton
    Private ListView1 As ListView
    Private flTxt1 As FloatLabeledEditText
    Private flTxt2 As FloatLabeledEditText
    Private txtSector As EditText
    Private txtBLK As EditText
    Private ka As RadioButton
    Private kb As RadioButton
    Private kFF As RadioButton
    Private kA0 As RadioButton
End Sub

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

manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true" />
    <uses-permission android:name="android.permission.NFC" />
    )
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
library i use :
core 9.90
javaobject 2.05
nfc 2.01
stringutils 1.12
viewex 1.30
 

Attachments

  • PedramNFCAdvanced.zip
    496.1 KB · Views: 334
  • WhatsApp Image 2020-11-21 at 12.27.11.jpeg
    32 KB · Views: 277

Mihai Rainer Jr.

Member
Licensed User
You need to take care of the combination between sector and block.
For example, if you need to read block 0 of sector 2, then you need to authenticate on sector 2 and readBlock(8).
I'm not sure what values are you using for testing, since it seems you are using EditText or smth for input values.
But, if you are trying to authenticate to sector 2 and then try to readBlock(0), that's a fail, since you are authenticated to sector 2 and trying to read block 0 which belongs to sector 0. I was struggling today half a day until I have realized that I made this mistake.
writeBlock(x) is also working for me following the same logic. First you need to use blockToSector to check in which sector is your desired block and authenticate to that sector. Then, readBlock on exact that sector number you have offered as a parameter to blockToSector method. Block number for MIFARE Classic 1K is from 0 to 63.
 
Upvote 0

nicomx

New Member
Licensed User
Longtime User
Good evening, I've been wandering around for two hours and I can't manage to engrave a sector and a block at will. Could you give a small example of data recording?
Thank you
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…