*RadeBit瑞安全原创保护文章,未经允许禁止转载!
什么是IC卡?
IC卡 (Integrated Circuit Card,集成电路卡),也称智能卡(Smart card)、智慧卡(Intelligent card)、微电路卡(Microcircuit card)或微芯片卡等。它是将一个微电子芯片嵌入符合ISO 7816标准的卡基中,做成卡片形式。IC卡与读写器之间的通讯方式可以是接触式,也可以是非接触式。根据通讯接口把IC卡分成接触式IC卡、非接触式IC和双界面卡(同时具备接触式与非接触式通讯接口)。IC卡运用范围广泛,例如:身份认证、银行、电信、公共交通、车场管理等领域。
IC卡原理
IC卡工作的基本原理是:射频读写器向IC卡发一组固定频率的电磁波,卡片内有一个LC串联谐振电路,其频率与读写器发射的频率相同,这样在电磁波激励下,LC谐振电路产生共振,从而使电容内有了电荷;在这个电容的另一端,接有一个单向导通的电子泵,将电容内的电荷送到另一个电容内存储,当所积累的电荷达到2V时,此电容可作为电源为其它电路提供工作电压,将卡内数据发射出去或接受读写器的数据。
设备要求
1、Arduino UNO 开发板
3、面包板
4、跳线(若干)
5、M1卡
6、一双灵巧的手和聪明的脑子
具体操作
一、连线
注意:一定要连接3V电源,电源过载会烧坏RC522模块!(3V连接UNO的3V,GND连接UNO的GND)
二、源程序
#include <SPI.h>
#include <MFRC522.h>#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout aboveMFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
byte buffer[18];
byte block;
byte waarde[64][16];
MFRC522::StatusCode status;MFRC522::MIFARE_Key key;
// Number of known default keys (hard-coded)
// NOTE: Synchronize the NR_KNOWN_KEYS define with the defaultKeys[] array
#define NR_KNOWN_KEYS 8
// Known keys, see: https://code.google.com/p/mfcuk/wiki/MifareClassicDefaultKeys
byte knownKeys[NR_KNOWN_KEYS][MFRC522::MF_KEY_SIZE] = {
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, // FF FF FF FF FF FF = factory default
{0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5}, // A0 A1 A2 A3 A4 A5
{0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5}, // B0 B1 B2 B3 B4 B5
{0x4d, 0x3a, 0x99, 0xc3, 0x51, 0xdd}, // 4D 3A 99 C3 51 DD
{0x1a, 0x98, 0x2c, 0x7e, 0x45, 0x9a}, // 1A 98 2C 7E 45 9A
{0xd3, 0xf7, 0xd3, 0xf7, 0xd3, 0xf7}, // D3 F7 D3 F7 D3 F7
{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, // AA BB CC DD EE FF
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // 00 00 00 00 00 00
};char choice;
/*
* Initialize.
*/
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Try the most used default keys to print block 0 to 63 of a MIFARE PICC."));
Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
}//Via seriele monitor de bytes uitlezen in hexadecimaal
void dump_byte_array(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
//Via seriele monitor de bytes uitlezen in ASCIvoid dump_byte_array1(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.write(buffer[i]);
}
}/*
* Try using the PICC (the tag/card) with the given key to access block 0 to 63.
* On success, it will show the key details, and dump the block data on Serial.
*
* @return true when the given key worked, false otherwise.
*/boolean try_key(MFRC522::MIFARE_Key *key)
{
boolean result = false;for(byte block = 0; block < 64; block++){
// Serial.println(F("Authenticating using key A..."));
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return false;
}// Read block
byte byteCount = sizeof(buffer);
status = mfrc522.MIFARE_Read(block, buffer, &byteCount);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}
else {
// Successful read
result = true;
Serial.print(F("Success with key:"));
dump_byte_array((*key).keyByte, MFRC522::MF_KEY_SIZE);
Serial.println();// Dump block data
Serial.print(F("Block ")); Serial.print(block); Serial.print(F(":"));
dump_byte_array1(buffer, 16); //omzetten van hex naar ASCI
Serial.println();for (int p = 0; p < 16; p++) //De 16 bits uit de block uitlezen
{
waarde [block][p] = buffer[p];
Serial.print(waarde[block][p]);
Serial.print(" ");
}}
}
Serial.println();Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD
return result;start();
}/*
* Main loop.
*/
void loop() {
start();}
void start(){
choice = Serial.read();if(choice == '1')
{
Serial.println("Read the card");
keuze1();}
else if(choice == '2')
{
Serial.println("See what is in the variables");
keuze2();
}
else if(choice == '3')
{
Serial.println("Copying the data on to the new card");
keuze3();
}
}void keuze2(){ //Test waardes in blokken
for(block = 4; block <= 62; block++){
if(block == 7 || block == 11 || block == 15 || block == 19 || block == 23 || block == 27 || block == 31 || block == 35 || block == 39 || block == 43 || block == 47 || block == 51 || block == 55 || block == 59){
block ++;
}Serial.print(F("Writing data into block "));
Serial.print(block);
Serial.println("\n");for(int j = 0; j < 16; j++){
Serial.print(waarde[block][j]);
Serial.print(" ");
}
Serial.println("\n");}
Serial.println("1.Read card \n2.Write to card \n3.Copy the data.");
start();
}void keuze3(){ //Copy the data in the new card
Serial.println("Insert new card...");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));// Try the known default keys
/*MFRC522::MIFARE_Key key;
for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
// Copy the known key into the MIFARE_Key structure
for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
key.keyByte[i] = knownKeys[k][i];
}
}*/
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}for(int i = 4; i <= 62; i++){ //De blocken 4 tot 62 kopieren, behalve al deze onderstaande blocken (omdat deze de authenticatie blokken zijn)
if(i == 7 || i == 11 || i == 15 || i == 19 || i == 23 || i == 27 || i == 31 || i == 35 || i == 39 || i == 43 || i == 47 || i == 51 || i == 55 || i == 59){
i++;
}
block = i;// Authenticate using key A
Serial.println(F("Authenticating using key A..."));
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}// Authenticate using key B
Serial.println(F("Authenticating again using key B..."));
status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, block, &key, &(mfrc522.uid));
if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
return;
}// Write data to the block
Serial.print(F("Writing data into block "));
Serial.print(block);
Serial.println("\n");dump_byte_array(waarde[block], 16);
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(block, waarde[block], 16);
if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write() failed: "));
Serial.println(mfrc522.GetStatusCodeName(status));
}Serial.println("\n");
}
mfrc522.PICC_HaltA(); // Halt PICC
mfrc522.PCD_StopCrypto1(); // Stop encryption on PCDSerial.println("1.Read card \n2.Write to card \n3.Copy the data.");
start();
}void keuze1(){ //Read card
Serial.println("Insert card...");
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return;// Show some details of the PICC (that is: the tag/card)
Serial.print(F("Card UID:"));
dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println();
Serial.print(F("PICC type: "));
MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
Serial.println(mfrc522.PICC_GetTypeName(piccType));// Try the known default keys
MFRC522::MIFARE_Key key;
for (byte k = 0; k < NR_KNOWN_KEYS; k++) {
// Copy the known key into the MIFARE_Key structure
for (byte i = 0; i < MFRC522::MF_KEY_SIZE; i++) {
key.keyByte[i] = knownKeys[k][i];
}
// Try the key
if (try_key(&key)) {
// Found and reported on the key and block,
// no need to try other keys for this PICC
break;
}
}
}
三、arduino运行
首先我们要先加载库文件
项目/加载库/管理库...
搜索"RC522",选择"MFRC522"安装即可
安装完成后,记得查看是否存在RC522库,如果没有,请重新安装。
安装成功后,新建一个实例,清空里面的代码,将上述代码复制进入,烧录到UNO即可。
四、复制IC卡
当烧录成功后,打开串口监视器,将会看到下图:
将原IC卡放上去,发送“1”,读卡。读取完成后,发送“3”,复制数据,换空白IC卡,发送“2”写入复制的数据即可。
这样就完成了IC卡的复制。但是,这是建立在你事先知道IC卡扇区密码的前提下,目前IC卡都是带有扇区密码的,只要知道扇区密码才能完成复制!RC522功能还是比较有限,推荐有条件的同学还是购买PM3设备,相对于RC522功能可以说是非常强大的,不仅能破解扇区密码,也能复制和修改扇区信息。
原创文章,作者:RadeBit,如若转载,请注明出处:《如何用RC522复制IC卡》https://www.radebit.com/web/article/2045.html
最新评论