ios NFC加密功能实现

前言:记录一下ios开发中NFC添加密码功能,卡类型为(NFCMiFareTag -->NFCMiFareUltralight),调用的iOS方法为:sendMiFareCommand。

一、JS代码实现,调用的ios原生NFC发送方法,针对卡类型为NTAG216的卡片。 NTAG212、NTAG213、NTAG215、NTAG216的配置位置不同,所以只需查到卡类型的配置起始位置即可。NTAG216的配置起始位置为E3。

二、写入密码的过程(0xA2为写入命令,0x30为读取命令)

 let pass = stringToBytes('1111'); // 密码  必须为4位  stringToBytes为字符串转byte方法;pass为数组。
 let pack = stringToBytes('11');  // 确认值 必须为2位

1、写入密码到 E5位置 ,即起始位置E3+2;

sendCommand([0xA2, E5, pass[0], pass[1], pass[2], pass[3]]);

2、将PACK(第 E6页,字节0-1)设置为所需的密码确认(默认值为0x0000).
sendCommand([0xA2, E6, pack[0], pack[1], 0x00, 0x00]);
3、将AUTHLIM(第 E4页,字节0,位2-0)设置为最大失败密码验证尝试次数(将此值设置为0将允许无限次数的PWD_AUTH尝试).将PROT(第 E4页,字节0,位7)设置为所需的值(0 =仅在写访问时需要PWD_AUTH,1 =读和写访问需要PWD_AUTH).
let response = sendCommand([0x30,E4]); //读取E4页数据
let response = readResponseE4.result;
let prot = false; // false 为只加密写 true为读写都加密
let authlim = 0; 
let writeResponse = sendCommand([0xA2, E4, ((response[0] & 0x078) | (prot ? 0x080 : 0x000) | (authlim & 0x007)), response[1], response[2], response[3]]);
4、将AUTH0(第 E3页,字节3)设置为应该要求密码验证的第一页.
let readResponseE3 = sendCommand([0x30, 0xE3]); //读取E3页数据
let writeResponseT = sendCommand([0xA2, index - 2, readResponseE3.result[0], readResponseE3.result[1], readResponseE3.result[2], (0 & 0x0ff)]); //0为密码要保护的开始位置。
三、验证密码(0x1B为验证命令)
sendCommand([0x1B, pass[0], pass[1], pass[2], pass[3]]); //验证通过会返回pack值,还需要与本地验证pack是否一致
四、删除密码
首先需要验证密码,然后只需将AUTH0(即配置起始页)设置为0x0ff即可。
let readResponse = sendCommand([0x30, E3]);
let deleteStatu = sendCommand([0xA2, E3, response[0], response[1], response[2], (0x0ff & 0x0ff)]);
五、ios原生方法
需要将传入的byte数组转成data类型

- (void)sendMiFareCommand:(NSData *)command completionHandler:(void(^)(NSData *response, NSError * _Nullable error))completionHandler API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(watchos, macos, tvos);

相关参考资料
 

猜你喜欢

转载自www.cnblogs.com/lxh123/p/11699700.html
今日推荐