protected void loginIntoSector(MfAccess mfAccess, CardChannel cardChannel, byte memoryKeyId) throws IOException {
try {
CommandAPDU loadKey = new CommandAPDU(Apdu.CLS_PTS, Apdu.INS_EXTERNAL_AUTHENTICATE,
Acs.P1_LOAD_KEY_INTO_VOLATILE_MEM, memoryKeyId, mfAccess.getKeyValue());
ResponseAPDU loadKeyResponse = cardChannel.transmit(loadKey);
if (!ApduUtils.isSuccess(loadKeyResponse)) {
throw new MfLoginException("Loading key failed. Sector: " + mfAccess.getSector() + ", Block: "
+ mfAccess.getBlock() + " Key: " + mfAccess.getKey().name() + ", Response: " + loadKeyResponse);
}
byte blockNumber = (byte)mfAccess.getCard().getBlockNumber(mfAccess.getSector(), mfAccess.getBlock());
byte keyTypeToUse = mfAccess.getKey() == Key.A ? Acs.KEY_A : Acs.KEY_B;
CommandAPDU auth = new CommandAPDU(Apdu.CLS_PTS, Apdu.INS_INTERNAL_AUTHENTICATE_ACS, 0, 0, new byte[] {
0x01, 0x00, blockNumber, keyTypeToUse, memoryKeyId });
ResponseAPDU authResponse = cardChannel.transmit(auth);
if (!ApduUtils.isSuccess(authResponse)) {
throw new MfLoginException("Login failed. Sector: " + mfAccess.getSector() + ", Block: "
+ mfAccess.getBlock() + " Key: " + mfAccess.getKey().name() + ", Response: " + authResponse);
}
}