From c74cdee924138497fad121eeb9bb1a4f373073e1 Mon Sep 17 00:00:00 2001 From: wh201906 Date: Mon, 27 Apr 2020 23:57:21 +0800 Subject: [PATCH] Support save data file and key file --- module/mifare.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++++- module/mifare.h | 3 ++ ui/mainwindow.cpp | 36 +++++++++++++++-- 3 files changed, 133 insertions(+), 6 deletions(-) diff --git a/module/mifare.cpp b/module/mifare.cpp index 9feab89..7fe5214 100644 --- a/module/mifare.cpp +++ b/module/mifare.cpp @@ -509,8 +509,8 @@ Mifare::DataType Mifare::data_isDataValid(QString data) // "?" will not been pro if(data[i] != ' ') return DATA_INVALID; } - return DATA_WITHSPACE; } + return DATA_WITHSPACE; } else if(data.length() == 32) { @@ -643,12 +643,108 @@ bool Mifare::data_loadKeyFile(const QString& filename) } } +bool Mifare::data_saveDataFile(const QString& filename, bool isBin) +{ + QFile file(filename, this); + if(file.open(QIODevice::WriteOnly)) + { + QByteArray buff; + QChar tmp; + if(isBin) + { + for(int i = 0; i < cardType.blocks; i++) + { + for(int j = 0; j < 16; j++) + { + unsigned char Byt[2]; + for(int k = 0; k < 2; k++) + { + tmp = dataList->at(i).at(j*2+k).toUpper(); + if(tmp >= '0' && tmp <= '9') + Byt[k] = tmp.toLatin1() - '0'; + else if(tmp >= 'A' && tmp <= 'F') + Byt[k] = tmp.toLatin1() - 'A' + 10; + } + buff += (Byt[0] << 4)|Byt[1]; + } + } + } + else + { + for(int i = 0; i < cardType.blocks; 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); + if(file.open(QIODevice::WriteOnly)) + { + QByteArray buff; + QChar tmp; + if(isBin) + { + for(int i = 0; i < cardType.sectors; i++) + { + for(int j = 0; j < 6; j++) + { + unsigned char Byt[2]; + for(int k = 0; k < 2; k++) + { + tmp = keyAList->at(i).at(j*2+k).toUpper(); + if(tmp >= '0' && tmp <= '9') + Byt[k] = tmp.toLatin1() - '0'; + else if(tmp >= 'A' && tmp <= 'F') + Byt[k] = tmp.toLatin1() - 'A' + 10; + } + buff += (Byt[0] << 4)|Byt[1]; + } + for(int j = 0; j < 6; j++) + { + unsigned char Byt[2]; + for(int k = 0; k < 2; k++) + { + tmp = keyBList->at(i).at(j*2+k).toUpper(); + if(tmp >= '0' && tmp <= '9') + Byt[k] = tmp.toLatin1() - '0'; + 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() { for(int i = 0; i < cardType.sectors; i++) { QString tmp = ""; - dataList->replace(cardType.blks[i] + cardType.blk[i] - 1, "????????????FF078069????????????"); if(data_isKeyValid(keyAList->at(i))) tmp += keyAList->at(i); else diff --git a/module/mifare.h b/module/mifare.h index dc1dde7..fdc1392 100644 --- a/module/mifare.h +++ b/module/mifare.h @@ -96,8 +96,11 @@ public: bool data_loadDataFile(const QString &filename); bool data_loadKeyFile(const QString &filename); + bool data_saveDataFile(const QString& filename, bool isBin); + bool data_saveKeyFile(const QString &filename, bool isBin); void data_key2Data(); void data_data2Key(); + public slots: signals: diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 356300c..a126b42 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -198,7 +198,7 @@ void MainWindow::on_MF_File_loadButton_clicked() QString filename = ""; if(ui->MF_File_dataBox->isChecked()) { - title = tr("Plz choose the data file:"); + title = tr("Plz select the data file:"); filename = QFileDialog::getOpenFileName(this, title, "./", tr("Binary Data Files(*.bin *.dump);;Text Data Files(*.txt *.eml);;All Files(*.*)")); qDebug() << filename; if(filename != "") @@ -211,7 +211,7 @@ void MainWindow::on_MF_File_loadButton_clicked() } else if(ui->MF_File_keyBox->isChecked()) { - title = tr("Plz choose the key file:"); + title = tr("Plz select the key file:"); filename = QFileDialog::getOpenFileName(this, title, "./", tr("Binary Key Files(*.bin *.dump);;All Files(*.*)")); qDebug() << filename; if(filename != "") @@ -227,9 +227,37 @@ void MainWindow::on_MF_File_loadButton_clicked() void MainWindow::on_MF_File_saveButton_clicked() { - QString title = tr("Save data to"); + + QString title = ""; + QString filename = ""; QString selectedType = ""; - QString filename = QFileDialog::getSaveFileName(this, title, "./", tr("Bin Files(*.bin *.dump);;Text Files(*.txt *.eml)"), &selectedType); + + if(ui->MF_File_dataBox->isChecked()) + { + title = tr("Plz select the location to save data file:"); + filename = QFileDialog::getSaveFileName(this, title, "./", tr("Binary Data Files(*.bin *.dump);;Text Data Files(*.txt *.eml)"), &selectedType); + qDebug() << filename; + if(filename != "") + { + if(!mifare->data_saveDataFile(filename, selectedType == "Binary Data Files(*.bin *.dump)")) + { + QMessageBox::information(this, tr("Info"), tr("Failed to save to") + "\n" + filename); + } + } + } + else if(ui->MF_File_keyBox->isChecked()) + { + title = tr("Plz select the location to save key file:"); + filename = QFileDialog::getSaveFileName(this, title, "./", tr("Binary Key Files(*.bin *.dump)"), &selectedType); + qDebug() << filename; + if(filename != "") + { + if(!mifare->data_saveKeyFile(filename, selectedType == "Binary Key Files(*.bin *.dump)")) + { + QMessageBox::information(this, tr("Info"), tr("Failed to save to") + "\n" + filename); + } + } + } qDebug() << filename << selectedType; }