Bonjour à tous,

Je suis étudiant et dans le cadre de mon stage je cherche a faire une applet qui permet de lire un tag nfc a partir du lecteur acr122.
j'utilise le package javax.smartcardio.
Cependant je ne parvient pas à lire les data se trouvant sur une carte de type Mifare K1
Quand la carte est vide il n'y a pas de problème, mais un fois que j'écris dessus avec un autre programme, en l’occurrence GoToTags, impossible d'authentifier un block mémoire.
De plus je ne parviens pas non plus a écrire sur la carte



Voici le code de la méthode run de mon thread pour la lecture :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
while(!isStop())
                {
                        try 
                        {
 
                            if(MyReader.waitForCardPresent(0)) 
                            {
                                if(!Lu) 
                                    System.out.println("détectée");
                                card = MyReader.connect("*");
                                if(card != null)
                                {
                                    if(!Lu)
                                    {
                                        System.out.println("Carte connectée");
                                        System.out.println("ATR: " + arrayToHex(((ATR) card.getATR()).getBytes()));
                                    }
 
 
                                      ch = card.getBasicChannel();
 
                                      /*Get UID*/
                                      byte[] ApduArrayUID = {
                                        (byte) 0xff,
                                        (byte) 0xca,
                                        (byte) 0x00,
                                        (byte) 0x00,
                                        (byte) 0x00
                                        };
 
                                      /*Iso Card*/
                                      byte[] ApduArrayISO = {
                                        (byte) 0xff,
                                        (byte) 0xca,
                                        (byte) 0x01,
                                        (byte) 0x00,
                                        (byte) 0x00
                                        };
 
                                       /* load Authentification */
                                       byte[] ApduArrayLoadAuth= {
                                        (byte) 0xff, //Class
                                        (byte) 0x82, //INS
                                        (byte) 0x00,//emplacement volatile du lecteur
                                        (byte) 0x00, //emplacement sur le lecteur
                                        (byte) 0x06, //LC
                                        (byte) 0xff, // Valeur de la clé sur 6 bytes
                                        (byte) 0xff,
                                        (byte) 0xff,
                                        (byte) 0xff,
                                        (byte) 0xff,
                                        (byte) 0xff
 
                                        };
 
                                       /*Authentication du Block 00h*/
                                       byte[] ApduArrayAuth = {
                                        (byte) 0xff,// Class
                                        (byte) 0x86, //INS
                                        (byte) 0x00,//P1
                                        (byte) 0x00,//P2
                                        (byte) 0x05,//LC
                                        //Authentication Data bytes
                                        (byte) 0x01, //Version
                                        (byte) 0x00, // Byte 2
                                        (byte) 0x00,//Block number
                                        (byte) 0x60,// Clé de type A
                                        (byte) 0x00 //Emplacement de la clé
                                        };
 
                                       /*Read Block 1*/
                                       byte[] ApduArrayRead = {
                                        (byte) 0xff, //Class
                                        (byte) 0xb0,//INS
                                        (byte) 0x00,//P1
                                        (byte) 0x00,//P2 = Block number
                                        (byte) 0x10, // Le = Number of bytes to read
 
                                        };
 
 
                                     /*****  UID *****/
                                      CommandAPDU GetDataUID = new CommandAPDU(ApduArrayUID);
                                      ResponseAPDU CardApduResponseUID = ch.transmit(GetDataUID);
                                      //nfc.AfficheUID(byteToString(CardApduResponseUID.getBytes()));
 
                                      if(CardApduResponseUID.getSW() == 36864)
                                      {
                                          String Uid=byteToString(CardApduResponseUID.getBytes());
                                          if(! Uid.equals(UidCourant))// Si une nouvelle carte les blocks mémoire ne sont pas authentifié
                                          {
 
                                              Auth=false;
                                              Lu=false;
                                          }
                                          else
                                          {
                                              Auth=true; Lu=true;
                                          }
 
 
                                          UidCourant= byteToString(CardApduResponseUID.getBytes());
                                           uid.setText(UidCourant);
                                           if(!Lu)
                                                System.out.println("UID : "+UidCourant);
 
                                      }    
                                             /*****  Charger authentification *****/
                                            //System.out.println("UID response: " + byteToString(CardApduResponseUID.getBytes()));
                                      if(!Charge)
                                      {
                                          System.out.println("Chargement de l'authentification dans le lecteur ");
                                          CommandAPDU GetDataLoadAuth = new CommandAPDU(ApduArrayLoadAuth);
                                          ResponseAPDU CardApduResponse = ch.transmit(GetDataLoadAuth);
                                          if(CardApduResponse.getSW() == 36864) //  90 00h = success 
                                          {
                                              Charge=true;
                                              System.out.println("Chargement authentification réussie :" );
                                          }
                                      }
                                      if(Charge)
                                      {
 
                                          if(!Auth)
                                          {
                                              /******* authentification du block 00h*********/
                                                System.out.println("Authentification d'un block");
                                                CommandAPDU GetDataAuth = new CommandAPDU(ApduArrayAuth); 
                                                ResponseAPDU CardApduResponseAuth = ch.transmit(GetDataAuth);
                                                System.out.println("ResponseAPDU auth : "+ CardApduResponseAuth);
                                                if(CardApduResponseAuth.getSW() == 36864)
                                                {
                                                    Auth=true;
                                                    System.out.println("Authentification d'un block réussie ! ");
                                                }
                                                else
                                                    System.out.println("Impossible d'authentifier le block :'(");
                                          }
 
 
                                          if(Auth && !Lu) // si l'authentification est faite et qu'on a pas encore Lu, on lit
                                          {
                                               /*Lecture du block 0x00*/
                                              System.out.println("Lecture d'un block");
                                              CommandAPDU GetDataRead = new CommandAPDU(ApduArrayRead);
                                              ResponseAPDU CardApduResponseRead = ch.transmit(GetDataRead);
                                              System.out.println("ResponseAPDU auth : "+ CardApduResponseRead);
                                              if(CardApduResponseRead.getSW() == 36864)
                                              {
                                                    System.out.println("Read response: " + byteToString(CardApduResponseRead.getBytes()));
                                                    Lu=true;
                                              }
                                          }
 
 
 
                                      }
                                      card.disconnect(true);   
                                }    
                                else
                                    System.out.println("Carte non connectée");
 
                            }
 
                        } 
                        catch (CardException ex) {
                            System.out.println("problème :" + ex.getMessage());
                        }
                }
Voici le code de la méthode run de mon thread pour l'écriture :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
while(!isStop())
                {
                        try 
                        {
                            uid.setText("");
                            if(MyReader.waitForCardPresent(0)) 
                            {
                                if(!Lu) 
                                    System.out.println("détectée");
                                card = MyReader.connect("*");
                                if(card != null)
                                {
                                    if(!Lu)
                                    {
                                        System.out.println("Carte connectée");
                                        System.out.println("ATR: " + arrayToHex(((ATR) card.getATR()).getBytes()));
                                    }
 
 
                                      ch = card.getBasicChannel();
 
                                      /*Get UID*/
                                      byte[] ApduArrayUID = {
                                        (byte) 0xff,
                                        (byte) 0xca,
                                        (byte) 0x00,
                                        (byte) 0x00,
                                        (byte) 0x00
                                        };
 
                                      /*Iso Card*/
                                      byte[] ApduArrayISO = {
                                        (byte) 0xff,
                                        (byte) 0xca,
                                        (byte) 0x01,
                                        (byte) 0x00,
                                        (byte) 0x00
                                        };
 
                                       /* load Authentification */
                                       byte[] ApduArrayLoadAuth= {
                                        (byte) 0xff, //Class
                                        (byte) 0x82, //INS
                                        (byte) 0x00,//emplacement volatile du lecteur
                                        (byte) 0x00, //emplacement sur le lecteur
                                        (byte) 0x06, //LC
                                        (byte) 0xff, // Valeur de la clé sur 6 bytes
                                        (byte) 0xff,
                                        (byte) 0xff,
                                        (byte) 0xff,
                                        (byte) 0xff,
                                        (byte) 0xff
 
                                        };
 
                                       /*Authentication du Block 00h*/
                                       byte[] ApduArrayAuth = {
                                        (byte) 0xff,// Class
                                        (byte) 0x86, //INS
                                        (byte) 0x00,//P1
                                        (byte) 0x00,//P2
                                        (byte) 0x05,//LC
                                        //Authentication Data bytes
                                        (byte) 0x01, //Version
                                        (byte) 0x00, // Byte 2
                                        (byte) 0x00,//Block number
                                        (byte) 0x60,// Clé de type A
                                        (byte) 0x00 //Emplacement de la clé
                                        };
 
                                       /*Write Block 1*/
                                       byte[] ApduArrayWrite = {
                                        (byte) 0xff, //Class
                                        (byte) 0xd6,//INS
                                        (byte) 0x00,//P1
                                        (byte) 0x00,//P2 = Block number
                                        (byte) 0x02, // Lc = Number of bytes to update
                                        //Data to be written
                                        (byte)0x00,
                                        (byte)0x01
 
                                        };
 
 
                                     /*****  UID *****/
                                      CommandAPDU GetDataUID = new CommandAPDU(ApduArrayUID);
                                      ResponseAPDU CardApduResponseUID = ch.transmit(GetDataUID);
                                      //nfc.AfficheUID(byteToString(CardApduResponseUID.getBytes()));
 
                                      if(CardApduResponseUID.getSW() == 36864)
                                      {
                                          String Uid=byteToString(CardApduResponseUID.getBytes());
                                          if(! Uid.equals(UidCourant))// Si une nouvelle carte les blocks mémoire ne sont pas authentifié
                                          {
 
                                              Auth=false;
                                              Lu=false;
                                          }
                                          else
                                          {
                                              Auth=true; Lu=true;
                                          }
 
 
                                          UidCourant= byteToString(CardApduResponseUID.getBytes());
                                           uid.setText(UidCourant);
                                           if(!Lu)
                                                System.out.println("UID : "+UidCourant);
 
                                      }    
                                             /*****  Charger authentification *****/
                                            //System.out.println("UID response: " + byteToString(CardApduResponseUID.getBytes()));
                                      if(!Charge)
                                      {
                                          System.out.println("Chargement de l'authentification dans le lecteur ");
                                          CommandAPDU GetDataLoadAuth = new CommandAPDU(ApduArrayLoadAuth);
                                          ResponseAPDU CardApduResponse = ch.transmit(GetDataLoadAuth);
                                          if(CardApduResponse.getSW() == 36864) //  90 00h = success 
                                          {
                                              Charge=true;
                                              System.out.println("Chargement authentification réussie :" );
                                          }
                                      }
                                      if(Charge)
                                      {
                                          if(!Auth)
                                          {
                                              /******* authentification du block 00h*********/
                                                System.out.println("Authentification d'un block");
                                                CommandAPDU GetDataAuth = new CommandAPDU(ApduArrayAuth); 
                                                ResponseAPDU CardApduResponseAuth = ch.transmit(GetDataAuth);
                                                System.out.println("ResponseAPDU auth : "+ CardApduResponseAuth);
                                                if(CardApduResponseAuth.getSW() == 36864)
                                                {
                                                    Auth=true;
                                                    System.out.println("Authentification d'un block réussie ! ");
                                                }
                                                else
                                                    System.out.println("Impossible d'authentifier le block :'(");
                                          }
 
 
                                          if(Auth && !Lu) // si l'authentification est faite et qu'on a pas encore Lu, on lit
                                          {
                                               /*Ecriture du block 0x00*/
                                              System.out.println("Ecriture d'un block");
                                              CommandAPDU GetDataWrite = new CommandAPDU(ApduArrayWrite);
                                              ResponseAPDU CardApduResponseWrite = ch.transmit(GetDataWrite);
                                              System.out.println("ResponseAPDU Write : "+ CardApduResponseWrite);
                                              if(CardApduResponseWrite.getSW() == 36864)
                                              {
                                                    System.out.println("Ecriture réussite!");
                                                    System.out.println("Write response: " + byteToString(CardApduResponseWrite.getBytes()));
                                                    Lu=true;
                                              }
                                              else
                                                  System.out.println("Echec de l'écriture :/ (fuck)");
                                          }
 
 
 
                                      }
 
                                      card.disconnect(true);   
 
 
                                }    
                                else
                                    System.out.println("Carte non connectée");
 
                            }
 
                        } 
                        catch (CardException ex) {
                            System.out.println("problème :" + ex.getMessage());
                        }
                }

Merci d'avance pour vos réponses

Cordialement Benoit