找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 11005|回复: 8
收起左侧

rc522读写,复制,修改密码,所有扇区内容修改

  [复制链接]
ID:173447 发表于 2018-8-10 13:43 | 显示全部楼层 |阅读模式
//读写扇区的例子://全部扇区数据块可单独读写,也可全部扇区同时读写,可以复制全部扇区的内容,也可以重新赋值全部扇区的内容
#include <SPI.h>
#include <RFID.h>

RFID rfid(53,49);    //D53--读卡器MOSI引脚、D49--读卡器RST引脚//2560板子
#define qh      43//功能切换读取
#define d      44//LED灯,灭代表读状态,亮代表写状态
char b=0;
//4字节卡序列号,第5字节为校验字节
unsigned char serNum[5];
//写卡数据,45为数据块数量,16为长度
//数据块是1-15扇区相加,每个扇区3个数据块故3*15=45;0号扇区固定不能改
unsigned char writeDate[45][16] ={
{'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1','1', '1', '1', '1'},
{'1', '2', '2','2', '2', '2','2', '2', '2','2', '2', '2','2', '2', '2', '2'},
{'1', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'2', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'2', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'3', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'3', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'4', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'4', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'5', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'5', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'6', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'6', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'7', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'7', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'8', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'8', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'9', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'9', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},

{'1', '2', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'1', '1', '3', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
{'0', '0', '0', '4', '-', '5', '6', '7', '8', '9', 'a', 'b', 'c','d', 'e', 0},
};
unsigned char fz[45][16];
//原扇区A密码,16个扇区,每个扇区密码6Byte
unsigned char sectorKeyA[16][16] = {
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//0~16扇区原密码
  //{0x11, 0x11, 0x11, 0x11, 0x11, 0x11},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
  {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},


        };
//新扇区A密码,16个扇区,每个扇区密码6Byte,一行代表一个扇区
//改密码只要改前面6个ff任意一个, 0xff,0x07,0x80,0x69, 千万不能动否则可能报废扇区
unsigned char sectorNewKeyA[16][16] = {
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},//1-16扇区新密码
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
        {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},



        };
//char aa[16];
void setup()
{
  Serial.begin(9600);
  SPI.begin();
  rfid.init();
pinMode(qh, INPUT);
digitalWrite(qh, 1);
pinMode(d, OUTPUT);
}

void loop()
{
  int j;
  char tmp;
  unsigned char status;
  unsigned char str[MAX_LEN];
  unsigned char blockAddr;        //选择操作的块地址0~63。但只能写4的倍数+3;比如7,11,15,19---
  if(digitalRead(qh)==0) //功能按键操作
  {
   b=b^1;
   digitalWrite(d, b);
   delay(30);
   while(!(digitalRead(qh)));
   delay(30);
  }


  //找卡
  rfid.isCard();

  //读取卡序列号
  if (rfid.readCardSerial())
  {
    Serial.print("The card's number is  : ");
    Serial.print(rfid.serNum[0],HEX);
    Serial.print(rfid.serNum[1],HEX);
    Serial.print(rfid.serNum[2],HEX);
    Serial.print(rfid.serNum[3],HEX);
    Serial.print(rfid.serNum[4],HEX);
    Serial.println(" ");
  }

  //选卡,返回卡容量(锁定卡片,防止多次读写)
rfid.selectTag(rfid.serNum);



  //复制数据卡
  if(b==1)
  {
    for(int ii= 7;ii<64;ii=ii+4)
  {
  for(int i=ii-3;i<ii;i++)//循环写入数据块012,不循环就单独写i值
    {
  blockAddr = ii;               
  if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[ blockAddr/4], rfid.serNum) == MI_OK)  //认证
  {
//    写密码
    status = rfid.write(blockAddr, sectorNewKeyA[blockAddr/4]);   
    //写数据

    blockAddr =i; //数据块i
    j=i-i/4-3;
   // status = rfid.write(blockAddr, writeDate[j]);//如果使用赋值功能则打开此行
   status = rfid.write(blockAddr, fz[j]);//如果使用复制功能则打开此行,与下面的
    if(status == MI_OK)
    {

      Serial.print("扇区:");
      Serial.print(i/4);
      Serial.print("数据块:");
      Serial.println(i);
      //Serial.println(j);
      Serial.println("Write card OK!");
    }
  }
    }

  }
  }

else
{
  //读卡
  for(int ii= 7;ii<64;ii=ii+4)
  {   
   for(int i=ii-3;i<ii;i++)
    {
  blockAddr = ii;


  status = rfid.auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], rfid.serNum);
  if (status == MI_OK)  //认证
  {
    //读数据
     blockAddr =i; //数据块i
    j=i-i/4-3;
  /*********如果使用复制卡里的数据则打开,否则关闭,与上面的status = rfid.write(blockAddr, fz[j])同时使用***************/
    if( rfid.read(blockAddr, str) == MI_OK)
    {
      for(int z=0;z<16;z++)//复制扇区数据块
      {
      fz[j][z]=str[z];   
      }
      /************************/
   //  Serial.println((char*)fz[i]);//打印复制的值,可省去
    Serial.print("扇区:");
    Serial.print(blockAddr/4);
    Serial.print("数据块:");
    Serial.print(i);
    Serial.print(":");
    Serial.print("the data is : ");
    Serial.println((char *)str);

    }
    }
  }
  }
}


  rfid.halt();//去掉本行连续读写

}

RFID.zip

9.02 KB, 下载次数: 150, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:112693 发表于 2018-9-5 13:27 | 显示全部楼层
谢谢分享
回复

使用道具 举报

ID:79006 发表于 2018-10-19 09:58 | 显示全部楼层
谢谢分享
回复

使用道具 举报

ID:79006 发表于 2018-12-21 09:33 | 显示全部楼层
谢谢分享
回复

使用道具 举报

ID:802567 发表于 2020-7-13 14:56 | 显示全部楼层
//数据块是1-15扇区相加,每个扇区3个数据块故3*15=45?
一个扇区不是四个数据块吗怎么变成三个了
回复

使用道具 举报

ID:820810 发表于 2020-9-19 01:13 | 显示全部楼层
加密占了四分之一吧?
回复

使用道具 举报

ID:964308 发表于 2021-10-27 08:50 来自手机 | 显示全部楼层
domifly 发表于 2020-7-13 14:56
//数据块是1-15扇区相加,每个扇区3个数据块故3*15=45?
一个扇区不是四个数据块吗怎么变成三个了

第三块单独写
回复

使用道具 举报

ID:964308 发表于 2021-12-1 15:53 来自手机 | 显示全部楼层
正好有做这个的打算
回复

使用道具 举报

ID:964308 发表于 2021-12-13 00:12 | 显示全部楼层
domifly 发表于 2020-7-13 14:56
//数据块是1-15扇区相加,每个扇区3个数据块故3*15=45?
一个扇区不是四个数据块吗怎么变成三个了

第三块特殊,要单独写入
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表