如何将第八位设置为零(从智能卡读取数据)

how to set the eighth bit to zero (reading data from smarcard)

如何将第八位设置为零?有时我得到不正确的文件。 p1,p2有什么问题?

CommandAPDU(cla, ins, p1, p2, ne);
                CommandAPDU readBinaryAPDU = new CommandAPDU(0x00, 0xB0, offset >> 8, offset & 0xFF, 0xff);


    public byte[] readCard(ResponseAPDU responseAPDU, CardChannel channel, byte[] fid, FidAdjusted fidEnum) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int offset = 0;
        try {
            System.out.println("Reading: " + fidEnum.name());
            if ((fid[0] != 0x00 && fid[1] != 0x05) && (fid[0] != 0x00 && fid[1] != 0x02)) {
                //select MF - Solo para posicionarse
                //AID: ‘FF 54 41 43 48 4F’ dla aplikacji tacho 1. gen
                //AID: ‘FF 53 4D 52 44 54’ dla aplikacji tacho 2. gen
                CommandAPDU SELECT_MF = new CommandAPDU(0x00, 0xA4, 0x04, 0x0C, OperationHelper.hexToByteAr("ff,54,41,43,48,4f"), 0x00, 0x06);
                responseAPDU = channel.transmit(SELECT_MF);
                System.out.println("Command select MF response: " + Integer.toHexString(responseAPDU.getSW1()));
            }
            // select EF
            CommandAPDU SELECT_EF = new CommandAPDU(0x00, APDUCommand.SELECT_FILE.getCommand(), 0x02, 0x0C, fid, 0x00, 0x02);
            responseAPDU = channel.transmit(SELECT_EF);
            System.out.println("Command select EF response: " + Integer.toHexString(responseAPDU.getSW1()));
            System.out.println("Begin reading... for " + fidEnum.name());
            do {
                // read binary
                CommandAPDU readBinaryAPDU = new CommandAPDU(0x00, 0xB0, offset >> 8, offset & 0xFF, 0xff);
                responseAPDU = channel.transmit(readBinaryAPDU);
                int sw1 = responseAPDU.getSW1();
                baos.write(responseAPDU.getData());
                switch (sw1) {
                    case 0x90: {
                        System.out.println("(Good)" + Integer.toHexString(sw1));
                        offset += responseAPDU.getData().length;
                        break;
                    }
                    case 0x67: {
                        System.out.println("(incorrect Lc or Le) (Wrong) " + Integer.toHexString(sw1));
                        break;
                    }
                }
            } while (responseAPDU.getData().length == 0xff);
            System.out.println("Reading end: " + fidEnum.name());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return baos.toByteArray();
    }





控制台输出:

Begin reading... for EF_ICC
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_ICC
5
Reading: EF_IC
Command select EF response: 90
Begin reading... for EF_IC
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_IC
12032
Reading: EF_DIR
Command select MF response: 90
Command select EF response: 6a
Begin reading... for EF_DIR
Reading end: EF_DIR
1280
1281
Reading: EF_APPLICATION_IDENTIFICATION
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_APPLICATION_IDENTIFICATION
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_APPLICATION_IDENTIFICATION
49408
Reading: EF_CARD_CERTIFICATE
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_CARD_CERTIFICATE
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_CARD_CERTIFICATE
49416
Reading: EF_CA_CERTIFICATE
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_CA_CERTIFICATE
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_CA_CERTIFICATE
1312
Reading: EF_IDENTIFICATION
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_IDENTIFICATION
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_IDENTIFICATION
1294
Reading: EF_CARD_DOWNLOAD
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_CARD_DOWNLOAD
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_CARD_DOWNLOAD
1313
Reading: EF_DRIVING_LICENSE_INFO
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_DRIVING_LICENSE_INFO
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_DRIVING_LICENSE_INFO
1282
Reading: EF_EVENTS_DATA
Command select EF response: 90
Begin reading... for EF_EVENTS_DATA
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_EVENTS_DATA
1283
Reading: EF_FAULTS_DATA
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_FAULTS_DATA
(Good)90
(Good)90
(Good)90
(Good)90
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_FAULTS_DATA
1284
Reading: EF_DRIVER_ACTIVITY_DATA
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_DRIVER_ACTIVITY_DATA
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_DRIVER_ACTIVITY_DATA
1285
Reading: EF_VEHICLES_USED
Command select EF response: 90
Begin reading... for EF_VEHICLES_USED
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(Good)90
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_VEHICLES_USED
1286
Reading: EF_PLACES
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_PLACES
(Good)90
(Good)90
(Good)90
(Good)90
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_PLACES
1287
Reading: EF_CURRENT_USAGE
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_CURRENT_USAGE
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_CURRENT_USAGE
1288
Reading: EF_CONTROL_ACTIVITY_DATA
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_CONTROL_ACTIVITY_DATA
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_CONTROL_ACTIVITY_DATA
1314
Reading: EF_SPECIFIC_CONDITIONS
Command select MF response: 90
Command select EF response: 90
Begin reading... for EF_SPECIFIC_CONDITIONS
(Good)90
(incorrect Lc lub Le) (Wrong) 67
Reading end: EF_SPECIFIC_CONDITIONS

Process finished with exit code 0


要将第八位设置为零,您的位掩码必须是二进制的 0111 1111,即十六进制的 0x7F。

offset & 0x7F

我不知道你的代码做了什么,但如果 CommandAPDU 的第 3 个和第 4 个参数应该只设置最后 7 位,应用掩码。

new CommandAPDU(0x00, 0xB0, (offset >> 8) & 0x7F, offset & 0x7F, 0xff);

对于READ BINARY P1和P2表示起始偏移量,P1是最高有效字节。从文件中的那个偏移量中检索数据。

如果您的偏移量 >= 0x8000,这是指令代码 0xB0 的错误(因为 P1 将被解释为短 EF 标识符)。 (因此 32 kByte 是一个重要的大小限制。)

P2 的最高位没有特殊意义,因此屏蔽它除了获得与预期不同的数据外没有任何好处。 (我认为这是错误。)

有关详细信息,一如既往,请参阅 ISO 7816,第 4 部分。