树莓派使用PN532读IC卡写入M1卡
1. PN532设置
1.1 连接模式切换
因为我这里使用I2C
模式,就需要把PN532
板子的左上角模式开关拨动为1和0,
即黑色开关内的两个白色拨头(下方文字为1和2),左边拨到上面,右边拨到下面。
1.2 接线到树莓派GPIO
GND -> 6 地线
VCC -> 4 5V供电
SDA -> 3 SDA.1
SCL -> 5 SCL.1
接线后PN532
板子中间的红色LED灯亮起
这里我犯了一个错误,SCL
线没有插好,但是PN532
板子的灯依旧亮了,导致后面nfc-list
找不到设备。
2. 树莓派设定
2.1 开启I2C
执行
sudo raspi-config
然后选择 Interfacing Options
,进入后找到I2C
,进去选择启用。
启用后重启树莓派
reboot
2.2 树莓派安装操作所需模块
2.2.1安装nfc操作模块
libnfc
目前在仓库内的名字是libnfc6
,版本为1.8.0
sudo apt install libnfc6
2.2.2 安装mfoc(破解key,读出数据到文件)
sudo apt install mfoc
2.2.3 安装mfcuk(破解全加密数据)
sudo apt install mfcuk
2.2.4 修改libnfc配置文件
sudo nano /etc/nfc/libnfc.conf
修改和添加内容如下
- 去掉这三行的前面的注释 #
allow_autoscan = true
allow_intrusive_scan = false
log_level = 1
- 末尾添加两行,第一行给
PN532
起个名字,第二行指定PN532位置
device.name = "PN532_I2C"
device.connstring = "pn532_i2c:/dev/i2c-1"
修改后的文件内容如下,Ctrl + X关闭nano
并覆盖文件保存。
# Allow device auto-detection (default: true)
# Note: if this auto-detection is disabled, user has to manually set a device
# configuration using file or environment variable
allow_autoscan = true
# Allow intrusive auto-detection (default: false)
# Warning: intrusive auto-detection can seriously disturb other devices
# This option is not recommended, so user should prefer to add manually his/her device.
allow_intrusive_scan = false
# Set log level (default: error)
# Valid log levels are (in order of verbosity): 0 (none), 1 (error), 2 (info), 3 (debug)
# Note: if you compiled with --enable-debug option, the default log level is "debug"
log_level = 1
# Manually set default device (no default)
# To set a default device, users must set both name and connstring for their device
# Note: if autoscan is enabled, default device will be the first device available in device list.
device.name = "PN532overI2C"
device.connstring = "pn532_i2c:/dev/i2c-1"
3. 检查PN532模块工作状态是否正常
i2cdetect -y 1
如果出现的不全是横杠,而是类似如下情况,则表示成功
$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- 24 -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
我因为没接好SCL
线,出现了返回全部横杠
使用nfc-list
查看是返回了如下错误
nfc-list: ERROR: Unable to open NFC device: pn532_i2c:/dev/i2c-1
调整好插线后再次查看,提示正常
$ nfc-list
nfc-list uses libnfc 1.8.0
NFC device: PN532overI2C opened
这时候在PN532上面放一张IC卡,再次输入命令nfc-list
$ nfc-list
nfc-list uses libnfc 1.7.1
NFC device: pn532_i2c:/dev/i2c-1 opened
1 ISO14443A passive target(s) found:
ISO/IEC 14443A (106 kbps) target:
ATQA (SENS_RES): 00 04
UID (NFCID1): 29 0f 82 73
SAK (SEL_RES): 08
可以读到数据了!
4. 读取旧IC卡数据并写入到新的M1白卡
4.1 读取旧IC卡数据并保存到output.mfd
文件
mfoc -O output.mfd // 读出卡中的数据保存为文件output.mfd
mfoc
是读取数据,如果有加密就自动破解,如果全加密,就没法读取,可用mfcuk
命令破解。我是的这张卡mfoc
就已经可以正常读取保存了,没有使用mfcuk
命令。
4.2 写入输出的output.mfd到新的M1卡
nfc-mfclassic w a output.mfd output.mfd // 写入数据,w小写,如果大写是强写0扇区
返回的命令形如
$ nfc-mfclassic w a output.mfd output.mfd
NFC reader: PN532overI2C opened
Found MIFARE Classic card:
ISO/IEC 14443A (106 kbps) target:
ATQA (SENS_RES): 00 04
UID (NFCID1): d3 a0 f0 06
SAK (SEL_RES): 08
RATS support: no
Guessing size: seems to be a 1024-byte card
Writing 64 blocks |............................................................|
Done, 60 of 64 blocks written.
完成!
参考文章:树莓派B 使用PN532 V3 - 简书