【发布时间】:2019-04-15 14:16:58
【问题描述】:
我使用 nfc-pcsc 通过 ACR122U 读卡器(USB 连接)读取 MIFARE Classic 1K 卡。但是我经常遇到0x6800错误:
- 有时当我读取第一个扇区(4-5-6 块)时
- 当我读取第 2 个扇区及更多扇区时(块 8-9-10、12-13-14 等)
但是像 NFC-GUI 这样的原生软件可以毫无困难地读取/写入所有数据。
A/B 键是默认键(000000.... 和 FFFFFF....)。访问位的默认值为空卡。
为什么会出现这个错误?怎样才能正确读卡?
请注意,我可以毫无困难地读取 MIFARE Ultralight 卡。
NodeJS 代码(与 NFC-PCSC 示例相同):
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);
});
【问题讨论】:
标签: node.js express authentication nfc mifare