我使用nfc读取带有ACR122U阅读器的MIFARE经典1K卡(USB连接)。但是我经常收到0x6800错误:
但是像NFC这样的本地软件可以毫无困难地读写所有数据。
键A/B是默认键(000000.和FFFFFF.)访问位具有空卡的默认值。
为什么会有这个错误?我怎样才能不出错地读卡片呢?
请注意,我可以阅读MIFARE超光速卡,没有困难。
NodeJS代码(与NFC的示例相同):
const { NFC } = require('nfc-pcsc');
const TAG_ISO_14443_3 = 'TAG_ISO_14443_3'; // ISO/IEC 14443-3 tags
const TAG_ISO_14443_4 = 'TAG_ISO_14443_4'; // ISO/IEC 14443-4 tags
const KEY_TYPE_A = 0x60;
const KEY_TYPE_B = 0x61;
const nfc = new NFC(); // optionally you can pass logger
nfc.on('reader', reader => {
console.log(`${reader.reader.name} device attached`);
reader.on('card', card => {
const CLASSIC_1K = '000100000000';
const CLASSIC_4K = '000200000000';
const ULTRALIGHT = '000300000000';
console.log(`${reader.reader.name} card detected`, card);
let buf = card.atr;
let type = buf.slice(0,12).toString('hex').toUpperCase();
let version = null;
if (type == '3B8F8001804F0CA000000306')
{
version = card.atr.slice(13,19).toString('hex');
switch (version)
{
case '000100000000':
console.log('Mifare Classic 1k');
break;
case '000200000000':
console.log('Mifare Classic 4k');
break;
case '000300000000':
console.log('Mifare Ultralight');
break;
default:
console.log('Other card');
}
}
if (version == ULTRALIGHT)
{
.... (no difficulties)
}
else if (version == CLASSIC_1K)
{
const key_a = '000000000000';
const key_b = 'FFFFFFFFFFFF';
const keyTypeA = KEY_TYPE_A;
const keyTypeB = KEY_TYPE_B;
Promise.all([
// Sector 1
reader.authenticate(4, keyTypeB, key_b),
reader.authenticate(5, keyTypeB, key_b),
reader.authenticate(6, keyTypeB, key_b),
// Sector 2
reader.authenticate(8, keyTypeB, key_b),
reader.authenticate(9, keyTypeB, key_b),
reader.authenticate(10, keyTypeB, key_b),
]).
then(() => {
console.info(`blocks successfully authenticated`);
reader.read(4, 32, 16) // Often OK
.then(data => {
console.info(`data read`, data.toString());
return reader.read(8, 32, 16); // Always error
})
.then(data => {
console.info(`data read`, data.toString());
}
.catch(err => {
console.error(`error when reading data`);
})
})
.catch(() => {
console.info(`athentification error`);
});
}
});
reader.on('card.off', card => {
console.log(`${reader.reader.name} card removed`);
});
reader.on('error', err => {
console.log(`${reader.reader.name} an error occurred`, err);
});
reader.on('end', () => {
console.log(`${reader.reader.name} device removed`);
});
});
nfc.on('error', err => {
console.log('an error occurred', err);
});发布于 2019-04-15 14:53:09
您似乎首先对所有扇区进行身份验证,然后尝试从这些扇区读取一些数据。然而,这不是MIFARE经典身份验证的工作方式。相反,需要对扇区进行身份验证(例如,使用reader.authenticate(4, keyTypeB, key_b)对整个扇区1使用密钥B进行身份验证)。然后,您可以从该扇区的任何块读取数据(其中键B被授予读访问权)。在从该扇区读取完之后,您可以对下一个扇区进行身份验证(例如,reader.authenticate(8, keyTypeB, key_b)对整个扇区2使用密钥B进行身份验证)。然后,您可以从该扇区的任何块读取数据(其中键B被授予读访问权)。
还请注意,“空”MIFARE经典卡的默认配置是键A= FFFFFFFFFFFF,键B=不使用,只用键A进行读/写。由于包含键的区域是不可读的(除非没有使用键),因此从这些内存区域读取"000000000000“通常只是意味着无法读取数据,实际的键仍然可以是一些其他值。因此,如果您确信卡片为空且具有默认权限集,我建议使用"FFFFFFFFFFFF“作为键A进行身份验证:
const key_a = 'FFFFFFFFFFFF';
const keyTypeA = KEY_TYPE_A;
await reader.authenticate(4, keyTypeA, key_a);
console.info(`sector authenticated`);
const data1 = await reader.read(4, 48, 16);
console.info(`data read for sector 1`, data1.toString());
await reader.authenticate(8, keyTypeA, key_a);
const data2 = await reader.read(8, 48, 16);
console.info(`data read for sector 2`, data2.toString());注意:似乎混乱的根源是nfc附带的MIFARE经典示例中的误导性文档。这些文件已经更新。
https://stackoverflow.com/questions/55691191
复制相似问题