You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Proxmark3GUI/module/mifare.cpp

1044 lines
31 KiB
C++

5 years ago
#include "mifare.h"
5 years ago
Mifare::Mifare(Ui::MainWindow *ui, Util *addr, QWidget *parent): QObject(parent)
5 years ago
{
this->parent = parent;
util = addr;
this->ui = ui;
cardType = card_1k;
keyAList = new QStringList();
keyBList = new QStringList();
dataList = new QStringList();
data_clearKey(); // fill with blank Qstring
data_clearData(); // fill with blank Qstring
dataPattern = new QRegExp("([0-9a-fA-F]{2} ){15}[0-9a-fA-F]{2}");
chkKeyPattern = new QRegExp("\\|\\d{3}\\|.+\\|.+\\|");
nestedKeyPattern = new QRegExp("\\|\\d{3}\\|.+\\|.+\\|.+\\|.+\\|");
5 years ago
}
QString Mifare::info(bool isRequiringOutput)
5 years ago
{
5 years ago
if(isRequiringOutput)
{
QString result = util->execCMDWithOutput("hf 14a info", 500);
5 years ago
qDebug() << result << result.indexOf(QRegExp(ui->MF_RW_dataEdit->text()), 0);
result.replace("UID :", "|");
result.replace("ATQA :", "|");
result.replace("SAK :", "|");
result.replace("TYPE :", "|");
QStringList lis = result.split("|");
5 years ago
if(lis.length() > 4)
{
qDebug() << lis[1] + lis[2] + lis[3];
return lis[1] + lis[2] + lis[3];
}
else
return "";
}
else
{
util->execCMD("hf 14a info");
ui->funcTab->setCurrentIndex(1);
return "";
}
}
5 years ago
void Mifare::chk()
{
QString result = util->execCMDWithOutput(
5 years ago
"hf mf chk *"
+ QString::number(cardType.type)
+ " ?",
1000 + cardType.type * 1000);
qDebug() << result;
int offset = 0;
QString tmp, tmp2;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
offset = chkKeyPattern->indexIn(result, offset);
// offset = result.indexOf(*chkKeyPattern, offset);
tmp = result.mid(offset, 39).toUpper();
offset += 39;
qDebug() << tmp << offset;
tmp2 = tmp.mid(7, 12).trimmed();
5 years ago
if(tmp2 != "?")
keyAList->replace(i, tmp2);
tmp2 = tmp.mid(24, 12).trimmed();
5 years ago
if(tmp2 != "?")
keyBList->replace(i, tmp2);
5 years ago
}
data_syncWithKeyWidget();
5 years ago
}
void Mifare::nested()
5 years ago
{
QString result = util->execCMDWithOutput(
5 years ago
"hf mf nested "
+ QString::number(cardType.type)
+ " *");
int offset = 0;
QString tmp;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
5 years ago
{
offset = nestedKeyPattern->indexIn(result, offset);
// offset = result.indexOf(*nestedKeyPattern, offset);
tmp = result.mid(offset, 47).toUpper();
offset += 47;
5 years ago
if(tmp.at(23) == '1')
keyAList->replace(i, tmp.mid(7, 12).trimmed());
5 years ago
if(tmp.at(44) == '1')
keyBList->replace(i, tmp.mid(28, 12).trimmed());
5 years ago
}
data_syncWithKeyWidget();
}
void Mifare::hardnested()
{
4 years ago
MF_Attack_hardnestedDialog dialog(cardType.block_size);
connect(&dialog, &MF_Attack_hardnestedDialog::sendCMD, util, &Util::execCMD);
5 years ago
if(dialog.exec() == QDialog::Accepted)
ui->funcTab->setCurrentIndex(1);
}
void Mifare::sniff()
{
util->execCMD("hf mf sniff");
ui->funcTab->setCurrentIndex(1);
}
void Mifare::snoop()
{
util->execCMD("hf 14a snoop");
ui->funcTab->setCurrentIndex(1);
}
void Mifare::list()
{
util->execCMD("hf list mf");
ui->funcTab->setCurrentIndex(1);
}
QString Mifare::_readblk(int blockId, KeyType keyType, const QString& key, int waitTime)
{
QString data;
QString result;
bool isKeyBlock = (blockId < 128 && ((blockId + 1) % 4 == 0)) || ((blockId + 1) % 8 == 0);
if(!data_isKeyValid(key))
{
return "";
}
if(util->getClientType() == Util::OFFICIAL)
{
// use the given key type to read the target block
result = util->execCMDWithOutput(
"hf mf rdbl "
+ QString::number(blockId)
+ " "
+ (char)keyType
+ " "
+ key,
waitTime);
if(result.indexOf("isOk:01") != -1)
{
result = result.mid(dataPattern->indexIn(result), 47).toUpper();
// when the target block is a key block and the given key type is keyA, try to check whether the keyB is valid
// if the given key type is keyB, it will never get the keyA from the key block
if(!isKeyBlock)
{
data = result;
}
else if(isKeyBlock && keyType == KEY_A)
{
5 years ago
for(int i = 0; i < 6; i++)
{
result = result.replace(i * 3, 2, key.mid(i * 2, 2));
}
data = result;
QString tmpKey = result.right(18).replace(" ", "");
result = util->execCMDWithOutput(
5 years ago
"hf mf rdbl "
+ QString::number(blockId)
5 years ago
+ " B "
+ tmpKey,
waitTime);
5 years ago
if(result.indexOf("isOk:01") == -1)
{
result = data;
result = result.replace(30, 17, "?? ?? ?? ?? ?? ??");
data = result;
}
}
else if(isKeyBlock && keyType == KEY_B)
{
for(int i = 0; i < 6; i++) // use the given keyB to replace revelant part of block data
{
5 years ago
result = result.replace(
30 + i * 3,
2,
key.mid(i * 2, 2));
}
result = result.replace(0, 18, "?? ?? ?? ?? ?? ?? "); // fill the keyA part with ?
data = result;
}
}
else
{
data = "";
}
}
return data.remove(" ");
}
QStringList Mifare::_readsec(int sectorId, KeyType keyType, const QString& key, int waitTime)
{
QStringList data;
QString result, tmp;
int offset = 0;
if(!data_isKeyValid(key))
{
return data;
}
if(util->getClientType() == Util::OFFICIAL)
{
result = util->execCMDWithOutput(
"hf mf rdsc "
+ QString::number(sectorId)
+ " "
+ (char)keyType
+ " "
+ key,
waitTime);
offset = result.indexOf("isOk:01");
if(offset != -1)
{
for(int i = 0; i < cardType.blk[sectorId]; i++)
{
offset = dataPattern->indexIn(result, offset);
tmp = result.mid(offset, 47).toUpper();
offset += 47;
qDebug() << tmp;
tmp.remove(" ");
data.append(tmp);
}
}
5 years ago
}
return data;
}
void Mifare::read()
{
int blockId = ui->MF_RW_blockBox->currentText().toInt();
Mifare::KeyType keyType = (Mifare::KeyType)(ui->MF_RW_keyTypeBox->currentData().toInt());
4 years ago
QString result = _readblk(blockId, keyType, ui->MF_RW_keyEdit->text().toUpper());
if(result != "")
{
ui->MF_RW_dataEdit->setText(result);
}
else
{
4 years ago
ui->MF_RW_dataEdit->setText(tr("Failed!"));
}
}
4 years ago
void Mifare::readAll() // note:cannot handle some situations(special trailer block)
{
4 years ago
QStringList data;
QString result;
bool isKeyAValid;
bool isKeyBValid;
const int waitTime = 150;
QString tmp;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
5 years ago
{
result = "";
isKeyAValid = false;
isKeyBValid = false;
// check keys and read the first block of each sector
4 years ago
data = _readsec(i, Mifare::KEY_A, keyAList->at(i));
if(data.size() > 0)
{
4 years ago
isKeyAValid = true;
for(int j = 0; j < cardType.blk[i]; j++)
{
4 years ago
dataList->replace(cardType.blks[i] + j, data[j]);
data_syncWithDataWidget(false, cardType.blks[i] + j);
}
}
4 years ago
data.clear();
data = _readsec(i, Mifare::KEY_B, keyBList->at(i));
if(data.size() > 0)
{
4 years ago
isKeyBValid = true;
for(int j = 0; j < cardType.blk[i]; j++)
{
4 years ago
dataList->replace(cardType.blks[i] + j, data[j]);
data_syncWithDataWidget(false, cardType.blks[i] + j);
}
}
4 years ago
// check keys and read the first block of each sector
5 years ago
if(isKeyAValid || isKeyBValid)
{
// fill the MF_dataWidget with the known valid key
//
// check whether the MF_dataWidget contains the valid key,
// and fill MF_keyWidget(when you only have KeyA but the ReadBlock output
// contains the KeyB)
//
// the structure is not symmetric, since you cannot get KeyA from output
// this program will only process the provided KeyA(in MF_keyWidget)
result = dataList->at(cardType.blks[i] + cardType.blk[i] - 1);
5 years ago
if(isKeyAValid)
{
result.replace(0, 12, keyAList->at(i));
}
else
{
result = result.replace(0, 12, "????????????");
}
dataList->replace(cardType.blks[i] + cardType.blk[i] - 1, result);
5 years ago
if(isKeyBValid)
{
result.replace(20, 12, keyBList->at(i));
dataList->replace(cardType.blks[i] + cardType.blk[i] - 1, result);
data_syncWithDataWidget(false, cardType.blks[i] + cardType.blk[i] - 1);
}
else // now isKeyAValid == true, the output might contains the KeyB
{
QString tmpKey =
dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12);
4 years ago
result = _readblk(cardType.blks[i] + cardType.blk[i] - 1, Mifare::KEY_B, tmpKey);
if(result != "")
{
keyBList->replace(i, tmpKey);
data_syncWithKeyWidget(false, i, KEY_B);
}
else
{
result = dataList->at(cardType.blks[i] + cardType.blk[i] - 1);
result = result.replace(20, 12, "????????????");
dataList->replace(cardType.blks[i] + cardType.blk[i] - 1, result);
}
}
data_syncWithDataWidget(false, cardType.blks[i] + cardType.blk[i] - 1);
}
5 years ago
}
}
void Mifare::write()
5 years ago
{
int waitTime = 300;
QString result = util->execCMDWithOutput(
"hf mf wrbl " + ui->MF_RW_blockBox->currentText() + " " +
ui->MF_RW_keyTypeBox->currentText() + " " +
ui->MF_RW_keyEdit->text() + " " +
ui->MF_RW_dataEdit->text().replace(" ", ""),
waitTime);
5 years ago
if(result.indexOf("isOk:01") != -1)
{
QMessageBox::information(parent, tr("Info"), tr("Success!"));
}
else
{
QMessageBox::information(parent, tr("Info"), tr("Failed!"));
}
5 years ago
}
void Mifare::writeAll()
{
const int waitTime = 300;
QString result;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
5 years ago
for(int j = 0; j < cardType.blk[i]; j++)
{
result = ""; // if the KeyA is valid and the result is not empty, the KeyB will not be tested.
5 years ago
if(data_isDataValid(dataList->at(cardType.blks[i] + j)) != DATA_NOSPACE || dataList->at(cardType.blks[i] + j).contains('?'))
continue;
5 years ago
if(data_isKeyValid(keyAList->at(i)))
{
result = util->execCMDWithOutput(
5 years ago
"hf mf wrbl " +
QString::number(cardType.blks[i] + j)
+ " A "
+ keyAList->at(i)
+ " "
+ dataList->at(cardType.blks[i] + j),
waitTime);
}
5 years ago
qDebug() << i << j << result.indexOf("isOk:01") << data_isKeyValid(keyBList->at(i));
if(result.indexOf("isOk:01") == -1 && data_isKeyValid(keyBList->at(i)))
{
result = util->execCMDWithOutput(
5 years ago
"hf mf wrbl "
+ QString::number(cardType.blks[i] + j)
+ " B "
+ keyBList->at(i)
+ " "
+ dataList->at(cardType.blks[i] + j),
waitTime);
}
if(result.indexOf("isOk:01") == -1 && keyAList->at(i) != "FFFFFFFFFFFF") // Try default key. It's useful when writing to a blank card
{
result = util->execCMDWithOutput(
"hf mf wrbl "
+ QString::number(cardType.blks[i] + j)
+ " A FFFFFFFFFFFF "
+ dataList->at(cardType.blks[i] + j),
waitTime);
}
}
}
}
void Mifare::readC()
{
int waitTime = 300;
int currblk = ui->MF_RW_blockBox->currentText().toInt();
QString result = util->execCMDWithOutput(
5 years ago
"hf mf cgetblk "
+ QString::number(currblk),
waitTime);
if(result.indexOf("No chinese") == -1)
{
result = result.mid(dataPattern->indexIn(result), 47).toUpper();
ui->MF_RW_dataEdit->setText(result);
}
}
void Mifare::readAllC()
{
QString result;
const int waitTime = 150;
QString tmp;
int offset = 0;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
5 years ago
result = util->execCMDWithOutput(
"hf mf cgetsc "
+ QString::number(i),
waitTime);
qDebug() << result;
5 years ago
if(result.indexOf("No chinese") == -1)
{
offset = 0;
5 years ago
for(int j = 0; j < cardType.blk[i]; j++)
{
offset = dataPattern->indexIn(result, offset);
// offset = result.indexOf(*dataPattern, offset);
tmp = result.mid(offset, 47).toUpper();
offset += 47;
qDebug() << tmp;
tmp.replace(" ", "");
dataList->replace(cardType.blks[i] + j, tmp);
data_syncWithDataWidget(false, cardType.blks[i] + j);
}
5 years ago
keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
data_syncWithKeyWidget(false, i, KEY_A);
data_syncWithKeyWidget(false, i, KEY_B);
}
}
}
void Mifare::writeC()
{
int waitTime = 150;
QString result = util->execCMDWithOutput(
5 years ago
"hf mf csetblk "
+ ui->MF_RW_blockBox->currentText()
+ " "
+ ui->MF_RW_dataEdit->text().replace(" ", ""),
waitTime);
5 years ago
if(result.indexOf("No chinese") == -1)
{
QMessageBox::information(parent, tr("Info"), tr("Success!"));
}
else
{
QMessageBox::information(parent, tr("Info"), tr("Failed!"));
}
}
void Mifare::writeAllC()
{
const int waitTime = 150;
QString result;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
5 years ago
for(int j = 0; j < cardType.blk[i]; j++)
{
result = "";
5 years ago
if(data_isDataValid(dataList->at(cardType.blks[i] + j)) != DATA_NOSPACE || dataList->at(cardType.blks[i] + j).contains('?'))
continue;
result = util->execCMDWithOutput(
5 years ago
"hf mf csetblk "
+ QString::number(cardType.blks[i] + j)
+ " "
+ dataList->at(cardType.blks[i] + j),
waitTime);
}
}
}
void Mifare::dump()
{
util->execCMD("hf mf dump");
ui->funcTab->setCurrentIndex(1);
}
void Mifare::restore()
{
util->execCMD("hf mf restore");
ui->funcTab->setCurrentIndex(1);
}
void Mifare::wipeC()
{
5 years ago
util->execCMD(
"hf mf cwipe "
+ QString::number(cardType.type)
+ " f");
ui->funcTab->setCurrentIndex(1);
}
void Mifare::setParameterC()
{
QString result = info(true);
5 years ago
if(result == "")
QMessageBox::information(parent, tr("Info"), tr("Failed to read card."));
else
{
QStringList lis = result.split("\r\n");
lis[0].replace(" ", "");
lis[1].replace(" ", "");
lis[2].replace(" ", "");
5 years ago
MF_UID_parameterDialog dialog(lis[0].toUpper(), lis[1].toUpper(), lis[2].mid(0, 2).toUpper());
connect(&dialog, &MF_UID_parameterDialog::sendCMD, util, &Util::execCMD);
5 years ago
if(dialog.exec() == QDialog::Accepted)
ui->funcTab->setCurrentIndex(1);
}
}
void Mifare::lockC()
{
util->execCMD("hf 14a raw -pa -b7 40");
util->execCMD("hf 14a raw -pa 43");
util->execCMD("hf 14a raw -pa E0 00 39 F7");
util->execCMD("hf 14a raw -pa E1 00 E1 EE");
5 years ago
util->execCMD("hf 14a raw -pa 85 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08 18 47");
util->execCMD("hf 14a raw 52");
}
void Mifare::writeAllE()
{
const int waitTime = 200;
QString result;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
5 years ago
for(int j = 0; j < cardType.blk[i]; j++)
{
result = "";
5 years ago
if(data_isDataValid(dataList->at(cardType.blks[i] + j)) != DATA_NOSPACE || dataList->at(cardType.blks[i] + j).contains('?'))
continue;
result = util->execCMDWithOutput(
5 years ago
"hf mf eset "
+ QString::number(cardType.blks[i] + j)
+ " "
+ dataList->at(cardType.blks[i] + j),
waitTime);
}
}
util->execCMDWithOutput("hf mf eget", waitTime); // to refresh output buffer;
}
void Mifare::readAllE()
{
QString result;
const int waitTime = 200;
QString tmp;
int offset = 0;
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
offset = 0;
5 years ago
for(int j = 0; j < cardType.blk[i]; j++)
{
qDebug() << "**********" ;
5 years ago
result = util->execCMDWithOutput(
"hf mf eget "
+ QString::number(cardType.blks[i] + j),
waitTime);
qDebug() << result ;
offset = dataPattern->indexIn(result);
// offset = result.indexOf(*dataPattern, offset); // When I find the data position in this way, the Regex might fail to match.
tmp = result.mid(offset, 47).toUpper();
qDebug() << tmp << offset;
qDebug() << "**********" ;
if(offset == -1)
continue;
tmp.replace(" ", "");
dataList->replace(cardType.blks[i] + j, tmp);
data_syncWithDataWidget(false, cardType.blks[i] + j);
}
keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
data_syncWithKeyWidget(false, i, KEY_A);
data_syncWithKeyWidget(false, i, KEY_B);
}
}
void Mifare::wipeE()
{
util->execCMD("hf mf eclr");
}
void Mifare::simulate()
{
MF_Sim_simDialog dialog(cardType.type);
connect(&dialog, &MF_Sim_simDialog::sendCMD, util, &Util::execCMD);
if(dialog.exec() == QDialog::Accepted)
ui->funcTab->setCurrentIndex(1);
}
void Mifare::loadSniff(const QString& file)
{
util->execCMD("hf list mf -l " + file);
ui->funcTab->setCurrentIndex(1);
}
void Mifare::saveSniff(const QString& file)
{
util->execCMD("hf list mf -s " + file);
ui->funcTab->setCurrentIndex(1);
}
void Mifare::data_syncWithDataWidget(bool syncAll, int block)
{
QString tmp;
5 years ago
if(syncAll)
{
4 years ago
for(int i = 0; i < cardType.block_size; i++)
{
tmp = "";
5 years ago
if(dataList->at(i) != "")
{
tmp += dataList->at(i).mid(0, 2);
5 years ago
for(int j = 1; j < 16; j++)
{
tmp += " ";
tmp += dataList->at(i).mid(j * 2, 2);
}
}
ui->MF_dataWidget->item(i, 2)->setText(tmp);
}
}
else
{
tmp = "";
5 years ago
if(dataList->at(block) != "")
{
tmp += dataList->at(block).mid(0, 2);
5 years ago
for(int j = 1; j < 16; j++)
{
tmp += " ";
tmp += dataList->at(block).mid(j * 2, 2);
}
}
ui->MF_dataWidget->item(block, 2)->setText(tmp);
}
}
void Mifare::data_syncWithKeyWidget(bool syncAll, int sector, KeyType keyType)
{
5 years ago
if(syncAll)
{
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
ui->MF_keyWidget->item(i, 1)->setText(keyAList->at(i));
ui->MF_keyWidget->item(i, 2)->setText(keyBList->at(i));
}
}
else
{
if(keyType == KEY_A)
ui->MF_keyWidget->item(sector, 1)->setText(keyAList->at(sector));
else
ui->MF_keyWidget->item(sector, 2)->setText(keyBList->at(sector));
}
}
void Mifare::data_clearData()
{
dataList->clear();
4 years ago
for(int i = 0; i < cardType.block_size; i++)
dataList->append("");
}
void Mifare::data_clearKey()
{
keyAList->clear();
keyBList->clear();
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
keyAList->append("");
keyBList->append("");
}
}
bool Mifare::data_isKeyValid(const QString &key)
{
5 years ago
if(key.length() != 12)
return false;
5 years ago
for(int i = 0; i < 12; i++)
{
5 years ago
if(!((key[i] >= '0' && key[i] <= '9') || (key[i] >= 'A' && key[i] <= 'F')))
return false;
}
return true;
}
Mifare::DataType
Mifare::data_isDataValid(QString data) // "?" will not been processd there
{
5 years ago
if(data.length() == 47)
{
5 years ago
for(int i = 0; i < 47; i++)
{
5 years ago
if(i % 3 != 0)
{
5 years ago
if(!((data[i] >= '0' && data[i] <= '9') || (data[i] >= 'A' && data[i] <= 'F') || data[i] == '?'))
return DATA_INVALID;
}
else
{
5 years ago
if(data[i] != ' ')
return DATA_INVALID;
}
}
return DATA_WITHSPACE;
}
5 years ago
else if(data.length() == 32)
{
5 years ago
for(int i = 0; i < 32; i++)
{
5 years ago
if(!((data[i] >= '0' && data[i] <= '9') || (data[i] >= 'A' && data[i] <= 'F') || data[i] == '?'))
return DATA_INVALID;
}
return DATA_NOSPACE;
}
else
return DATA_INVALID;
}
Mifare::CardType Mifare::getCardType()
{
return cardType;
}
void Mifare::setCardType(int type)
{
5 years ago
if(type == 0 || type == 1 || type == 2 || type == 4)
{
5 years ago
if(type == 0)
cardType = card_mini;
5 years ago
else if(type == 1)
cardType = card_1k;
5 years ago
else if(type == 2)
cardType = card_2k;
5 years ago
else if(type == 4)
cardType = card_4k;
data_clearKey();
data_clearData();
}
}
bool Mifare::data_loadDataFile(const QString &filename)
{
QFile file(filename, this);
5 years ago
if(file.open(QIODevice::ReadOnly))
{
QByteArray buff;
buff = file.read(10000);
bool isBin = false;
4 years ago
for(int i = 0; i < cardType.block_size * 16; i++) // Detect the file type
{
// qDebug() << (unsigned char)buff[i];
5 years ago
if(!((buff[i] >= 'A' && buff[i] <= 'F') || (buff[i] >= 'a' && buff[i] <= 'f') || (buff[i] >= '0' && buff[i] <= '9') || buff[i] == '\n' || buff[i] == '\r'))
{
isBin = true;
break;
}
}
5 years ago
if(isBin)
{
4 years ago
if(file.size() < cardType.block_size * 16)
return false;
4 years ago
for(int i = 0; i < cardType.block_size; i++)
{
QString tmp = bin2text(buff, i, 16);
dataList->replace(i, tmp.toUpper());
}
}
else
{
4 years ago
QString tmp = buff.left(cardType.block_size * 34);
QStringList tmpList = tmp.split("\r\n");
4 years ago
for(int i = 0; i < cardType.block_size; i++)
{
dataList->replace(i, tmpList[i].toUpper());
qDebug() << tmpList[i];
}
}
file.close();
data_syncWithDataWidget();
return true;
}
else
{
return false;
}
}
bool Mifare::data_loadKeyFile(const QString &filename)
{
QFile file(filename, this);
5 years ago
if(file.open(QIODevice::ReadOnly))
{
QByteArray buff;
buff = file.read(10000);
4 years ago
bool isKey = file.size() <= cardType.sector_size * 14;
5 years ago
if(isKey)
{
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
QString tmp = bin2text(buff, i, 12);
keyAList->replace(i, tmp.left(12).toUpper());
keyBList->replace(i, tmp.right(12).toUpper());
}
}
else
{
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
int blk = cardType.blks[i] + cardType.blk[i] - 1;
QString tmp = bin2text(buff, blk, 16);
keyAList->replace(i, tmp.left(12).toUpper());
keyBList->replace(i, tmp.right(12).toUpper());
}
}
file.close();
data_syncWithKeyWidget();
return true;
}
else
{
return false;
}
}
QString Mifare::bin2text(const QByteArray &buff, int i, int length)
{
QString ret = "";
char LByte, RByte;
char map[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
5 years ago
for(int j = 0; j < length; j++)
{
LByte = map[(unsigned char)buff[i * length + j] >> 4];
RByte = map[(unsigned char)buff[i * length + j] & 0xF];
ret += LByte;
ret += RByte;
}
qDebug() << ret;
return ret;
}
bool Mifare::data_saveDataFile(const QString &filename, bool isBin)
{
QFile file(filename, this);
5 years ago
if(file.open(QIODevice::WriteOnly))
{
QByteArray buff;
QChar tmp;
5 years ago
if(isBin)
{
4 years ago
for(int i = 0; i < cardType.block_size; i++)
{
5 years ago
for(int j = 0; j < 16; j++)
{
4 years ago
unsigned char Byt[2] = {0x0, 0x0};
5 years ago
for(int k = 0; k < 2; k++)
{
tmp = dataList->at(i).at(j * 2 + k).toUpper();
5 years ago
if(tmp >= '0' && tmp <= '9')
Byt[k] = tmp.toLatin1() - '0';
5 years ago
else if(tmp >= 'A' && tmp <= 'F')
Byt[k] = tmp.toLatin1() - 'A' + 10;
}
buff += (Byt[0] << 4) | Byt[1];
}
}
}
else
{
4 years ago
for(int i = 0; i < cardType.block_size; i++)
{
buff += dataList->at(i);
buff += "\r\n";
}
}
bool ret = file.write(buff) != -1;
file.close();
return ret;
}
else
{
return false;
}
}
bool Mifare::data_saveKeyFile(const QString &filename, bool isBin)
{
QFile file(filename, this);
5 years ago
if(file.open(QIODevice::WriteOnly))
{
QByteArray buff;
QChar tmp;
5 years ago
if(isBin)
{
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
5 years ago
for(int j = 0; j < 6; j++)
{
4 years ago
unsigned char Byt[2] = {0x0, 0x0};
5 years ago
for(int k = 0; k < 2; k++)
{
tmp = keyAList->at(i).at(j * 2 + k).toUpper();
5 years ago
if(tmp >= '0' && tmp <= '9')
Byt[k] = tmp.toLatin1() - '0';
5 years ago
else if(tmp >= 'A' && tmp <= 'F')
Byt[k] = tmp.toLatin1() - 'A' + 10;
}
buff += (Byt[0] << 4) | Byt[1];
}
5 years ago
for(int j = 0; j < 6; j++)
{
4 years ago
unsigned char Byt[2] = {0x0, 0x0};
5 years ago
for(int k = 0; k < 2; k++)
{
tmp = keyBList->at(i).at(j * 2 + k).toUpper();
5 years ago
if(tmp >= '0' && tmp <= '9')
Byt[k] = tmp.toLatin1() - '0';
5 years ago
else if(tmp >= 'A' && tmp <= 'F')
Byt[k] = tmp.toLatin1() - 'A' + 10;
}
buff += (Byt[0] << 4) | Byt[1];
}
}
}
else
{
}
bool ret = file.write(buff) != -1;
file.close();
return ret;
}
else
{
return false;
}
}
void Mifare::data_key2Data()
{
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
QString tmp = "";
5 years ago
if(data_isKeyValid(keyAList->at(i)))
tmp += keyAList->at(i);
else
tmp += "????????????";
5 years ago
if(dataList->at(cardType.blks[i] + cardType.blk[i] - 1) == "")
tmp += "FF078069"; // default control bytes
else
tmp += dataList->at(cardType.blks[i] + cardType.blk[i] - 1).mid(12, 8);
5 years ago
if(data_isKeyValid(keyBList->at(i)))
tmp += keyBList->at(i);
else
tmp += "????????????";
dataList->replace(cardType.blks[i] + cardType.blk[i] - 1, tmp);
data_syncWithDataWidget();
}
}
void Mifare::data_data2Key()
{
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
{
5 years ago
if(dataList->at(cardType.blks[i] + cardType.blk[i] - 1) == "")
{
keyAList->replace(i, "????????????");
keyBList->replace(i, "????????????");
}
else
{
5 years ago
keyAList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).left(12));
keyBList->replace(i, dataList->at(cardType.blks[i] + cardType.blk[i] - 1).right(12));
}
data_syncWithKeyWidget();
}
}
void Mifare::data_setData(int block, const QString &data)
{
dataList->replace(block, data);
}
void Mifare::data_setKey(int sector, KeyType keyType, const QString &key)
{
if(keyType == KEY_A)
keyAList->replace(sector, key);
else
keyBList->replace(sector, key);
}
4 years ago
void Mifare::data_fillKeys()
{
4 years ago
for(int i = 0; i < cardType.sector_size; i++)
4 years ago
{
if(!data_isKeyValid(keyAList->at(i)))
{
keyAList->replace(i, "FFFFFFFFFFFF");
}
if(!data_isKeyValid(keyBList->at(i)))
{
keyBList->replace(i, "FFFFFFFFFFFF");
}
}
data_syncWithKeyWidget();
}