1 Commits
V1.0 ... V1.1

Author SHA1 Message Date
79fee78d81 aggiunto il componente hexTextbox 2026-02-28 19:11:43 +01:00

View File

@@ -0,0 +1,248 @@
package letturascrittura_nfc;
import javax.smartcardio.*;
import java.util.HashMap;
import java.util.Map;
public class Tessera {
private final byte[] uid;
private final byte[][][] settori = new byte[16][4][16]; // [settore][blocco][byte]
private final Map<Integer, byte[]> trailer = new HashMap<>(); // trailer modificabili
private final CardChannel channel;
/**
* Costruttore: legge la scheda appoggiata sul lettore
*/
public Tessera(CardChannel channel) throws Exception {
this.channel = channel;
// Lettura blocco 0 per UID
byte[] blocco0 = leggiBlocco(0);
this.uid = estraiUID(blocco0);
// Lettura completa dei settori
for (int settore = 0; settore < 16; settore++) {
int primoBlocco = settore * 4;
// Autenticazione una volta per settore
autenticaBlocco(primoBlocco);
for (int blocco = 0; blocco < 4; blocco++) {
int numeroBlocco = primoBlocco + blocco;
byte[] dati = leggiBloccoRaw(numeroBlocco);
settori[settore][blocco] = dati;
// Salva i trailer nella map separata
if (blocco == 3) {
trailer.put(settore, dati.clone());
}
}
}
}
// =======================
// UID
// =======================
public byte[] getUID() {
return uid;
}
public String getUIDHex() {
if (uid == null) return null;
return bytesToHex(uid);
}
// =======================
// Blocchi dati
// =======================
public byte[] getBlocco(int settore, int blocco) {
checkRange(settore, blocco);
return settori[settore][blocco];
}
public void scriviBlocco(int settore, int blocco, byte[] dati) {
checkRange(settore, blocco);
if (blocco == 3) {
throw new IllegalArgumentException("Non puoi modificare il trailer con scriviBlocco");
}
if (dati.length != 16) {
throw new IllegalArgumentException("Un blocco deve essere di 16 byte");
}
settori[settore][blocco] = dati;
}
// =======================
// Trailer
// =======================
public byte[] getTrailer(int settore) {
checkRange(settore, 3);
return trailer.getOrDefault(settore, null);
}
public void setKeyA(int settore, byte[] keyA) {
if (keyA.length != 6) throw new IllegalArgumentException("KeyA deve essere lunga 6 byte");
byte[] t = trailer.getOrDefault(settore, new byte[16]);
System.arraycopy(keyA, 0, t, 0, 6);
trailer.put(settore, t);
}
public void setKeyB(int settore, byte[] keyB) {
if (keyB.length != 6) throw new IllegalArgumentException("KeyB deve essere lunga 6 byte");
byte[] t = trailer.getOrDefault(settore, new byte[16]);
System.arraycopy(keyB, 0, t, 10, 6);
trailer.put(settore, t);
}
public void setAccessBits(int settore, byte c1, byte c2, byte c3) {
byte[] t = trailer.getOrDefault(settore, new byte[16]);
t[6] = c1;
t[7] = c2;
t[8] = c3;
trailer.put(settore, t);
}
public void scriviTrailer(CardChannel channel, int settore) throws Exception {
byte[] t = trailer.get(settore);
if (t == null) throw new RuntimeException("Trailer settore " + settore + " non impostato");
int bloccoTrailer = settore * 4 + 3;
autenticaBlocco(bloccoTrailer);
byte[] cmd = new byte[21];
cmd[0] = (byte) 0xFF;
cmd[1] = (byte) 0xD6; // WRITE BLOCK
cmd[2] = 0x00;
cmd[3] = (byte) bloccoTrailer;
cmd[4] = 0x10;
System.arraycopy(t, 0, cmd, 5, 16);
ResponseAPDU resp = channel.transmit(new CommandAPDU(cmd));
if (resp.getSW() != 0x9000) {
throw new RuntimeException("Errore scrittura trailer settore " + settore);
}
// Aggiorna array settori
settori[settore][3] = t.clone();
}
// =======================
// Stampa
// =======================
public void stampaCompleta() {
for (int s = 0; s < 16; s++) {
System.out.println("=== Settore " + s + " ===");
for (int b = 0; b < 4; b++) {
String tipo = (b == 3) ? "Trailer" : "Blocco";
System.out.println(tipo + " " + b + ": " + bytesToHexAndText(settori[s][b]));
}
}
}
// =======================
// APDU NFC
// =======================
private byte[] leggiBlocco(int numeroBlocco) throws Exception {
autenticaBlocco(numeroBlocco);
byte[] cmd = new byte[]{(byte) 0xFF, (byte) 0xB0, 0x00, (byte) numeroBlocco, 0x10};
ResponseAPDU resp = channel.transmit(new CommandAPDU(cmd));
if (resp.getSW() != 0x9000)
throw new RuntimeException("Errore lettura blocco " + numeroBlocco);
return resp.getData();
}
private byte[] leggiBloccoRaw(int numeroBlocco) throws Exception {
return leggiBlocco(numeroBlocco).clone();
}
private void autenticaBlocco(int numeroBlocco) throws Exception {
byte[] auth = new byte[]{(byte) 0xFF, (byte) 0x86, 0x00, 0x00, 0x05, 0x01, 0x00, (byte) numeroBlocco, 0x60, 0x00};
ResponseAPDU resp = channel.transmit(new CommandAPDU(auth));
if (resp.getSW() != 0x9000) {
throw new RuntimeException("Autenticazione fallita blocco " + numeroBlocco);
}
}
// =======================
// UID extraction
// =======================
private byte[] estraiUID(byte[] blocco0) {
byte xor4 = 0;
for (int i = 0; i < 4; i++) xor4 ^= blocco0[i];
if (xor4 == blocco0[4]) return new byte[]{blocco0[0], blocco0[1], blocco0[2], blocco0[3]};
byte xor7 = 0;
for (int i = 0; i < 7; i++) xor7 ^= blocco0[i];
if (xor7 == blocco0[7]) return new byte[]{blocco0[0], blocco0[1], blocco0[2], blocco0[3], blocco0[4], blocco0[5], blocco0[6]};
return new byte[]{blocco0[0], blocco0[1], blocco0[2], blocco0[3]}; // default 4 byte
}
// =======================
// Utility
// =======================
private static void checkRange(int settore, int blocco) {
if (settore < 0 || settore >= 16) throw new IllegalArgumentException("Settore non valido");
if (blocco < 0 || blocco >= 4) throw new IllegalArgumentException("Blocco non valido");
}
public static byte[] textToBytes(String testo) {
byte[] dati = new byte[16];
byte[] testoBytes = testo.getBytes();
for (int i = 0; i < 16; i++) dati[i] = (i < testoBytes.length) ? testoBytes[i] : 0x00;
return dati;
}
public static String bytesToText(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) sb.append((b >= 32 && b <= 126) ? (char) b : '.');
return sb.toString();
}
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) sb.append(String.format("%02X ", b));
return sb.toString().trim();
}
public static String bytesToHexAndText(byte[] bytes) {
return bytesToHex(bytes) + " |" + bytesToText(bytes) + "|";
}
public static byte[] hexToBytes(String hex) {
if (hex == null) {
throw new IllegalArgumentException("Stringa nulla");
}
// Rimuove spazi
hex = hex.replaceAll("\\s+", "");
if (hex.length() == 0) {
return new byte[0];
}
// Deve essere pari (2 caratteri per byte)
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException("Numero di caratteri HEX non valido");
}
if (!hex.matches("[0-9A-Fa-f]+")) {
throw new IllegalArgumentException("Stringa contiene caratteri non HEX");
}
int len = hex.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
int value = Integer.parseInt(hex.substring(i, i + 2), 16);
data[i / 2] = (byte) value;
}
return data;
}
}