IC卡特點(diǎn):
1IC卡的內(nèi)部存儲結(jié)構(gòu)分為16個扇區(qū),從第0區(qū)到第15區(qū),每個扇區(qū)有獨(dú)立的密碼和權(quán)限控制字,可作為卡的密碼和權(quán)限控制字專用獨(dú)立的電子錢包,每個扇區(qū)有4個塊,序號為第0塊、第1塊、第2塊、第3塊,每塊16個字節(jié),第3塊是塊,禁止在此存放數(shù)據(jù)。第0塊、第1塊、第2塊可隨意存放數(shù)據(jù)。但第0區(qū)的第0塊已被固化了IC卡出廠信息,此塊只能讀出信息,不可更改。
調(diào)用DLL動態(tài)庫代碼: unsigned char result; QLibrary mylib("OUR_MIFARE.dll"); if (!mylib.load()) //判斷是否正確加載 { QMessageBox::information(NULL, "提示", "動態(tài)庫沒裝載!原因是動態(tài)庫OUR_MIFARE.dll沒放在生成exe文件相同目錄下!"); return; } pcdbeep mypcdbeep = (pcdbeep)mylib.resolve("pcdbeep"); if (mypcdbeep == NULL) //是否成功連接上 idr_beep()函數(shù) { mylib.unload(); QMessageBox::information(NULL, "提示", "請檢查函數(shù)名是否正確!"); return; } result = mypcdbeep(30); //這里函數(shù)指針調(diào)用dll中的idr_beep() 函數(shù) mylib.unload(); if(result > 0) { if(result == 23) { QMessageBox::information(NULL, "提示", "機(jī)器沒連上或者驅(qū)動沒下載"); } else { QMessageBox::information(NULL, "提示", "錯誤代碼:" + QString::number(result)); }; }
讀出IC卡數(shù)據(jù)的代碼:
void MainWindow::on_pushButton_3_clicked()//輕松讀卡 { unsigned char ctrlword;//控制字 unsigned char serial[4];//可寫數(shù)組 unsigned char area;//讀出的區(qū)號 unsigned char keyA1B0;//AB密碼認(rèn)證 unsigned char picckey[6];//存放卡密碼的數(shù)組 unsigned char piccdata0_2[48];//下標(biāo)0~15存放作為讀出的塊0的數(shù)據(jù),下標(biāo)16~31存放作為讀出的塊1的數(shù)據(jù),下標(biāo)32~47存放作為讀出的塊2的數(shù)據(jù) unsigned char status;//返回 unsigned char status1; unsigned char result; unsigned char piccdata[16]; //卡數(shù)據(jù)緩沖 //以下控制字含義:讀塊0、塊1、塊2,僅讀指定序列號的卡,需要每次指定密碼 if (ui->radioButton->isChecked()) { ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY; } else {//內(nèi)部密碼 ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN; } area = ui->comboBox->currentText().toUShort(); keyA1B0 = 1;//用A密碼 QString Key_str = ui->lineEdit->text().trimmed(); if(Key_str.length() < 12) { QMessageBox::information(NULL, "提示", "密碼長度不夠12位!"); ui-?>lineEdit->setFocus(); return; } char c; for (int var = 0; var < 12; ++var) { c = Key_str.at(var).toLatin1(); if(!((('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) || (('a' <= c) && (c <= 'f')))) { QMessageBox::information(NULL, "提示", "密碼輸入字符不正確!"); ui-?>lineEdit->setSelection(var,1); ui->lineEdit->setFocus(); return; } } bool ok; for(int i = 0; i < 6; i++) { picckey[i]=QString(Key_str.mid(i*2,2)).toInt(&ok,16); } QLibrary mylib("OUR_MIFARE.dll"); if (!mylib.load()) //判斷是否正確加載 { QMessageBox::information(NULL, "提示", "動態(tài)庫沒裝載!原因是動態(tài)庫OUR_MIFARE.dll沒放在生成exe文件相同目錄下!"); return; } piccreadex mypiccreadex = (piccreadex)mylib.resolve("piccreadex"); status = mypiccreadex(ctrlword,serial,area,keyA1B0,picckey,piccdata0_2); if(status == 0) { ui-?>lineEdit_2->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X ",piccdata0_2[0],piccdata0_2[1],piccdata0_2[2],piccdata0_2[3],piccdata0_2[4],piccdata0_2[5],piccdata0_2[6],piccdata0_2[7],piccdata0_2[8],piccdata0_2[9],piccdata0_2[10],piccdata0_2[11],piccdata0_2[12],piccdata0_2[13],piccdata0_2[14],piccdata0_2[15])); ui->lineEdit_3->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X ",piccdata0_2[16],piccdata0_2[17],piccdata0_2[18],piccdata0_2[19],piccdata0_2[20],piccdata0_2[21],piccdata0_2[22],piccdata0_2[23],piccdata0_2[24],piccdata0_2[25],piccdata0_2[26],piccdata0_2[27],piccdata0_2[28],piccdata0_2[29],piccdata0_2[30],piccdata0_2[31])); ui->lineEdit_4->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X ",piccdata0_2[32],piccdata0_2[33],piccdata0_2[34],piccdata0_2[35],piccdata0_2[36],piccdata0_2[37],piccdata0_2[38],piccdata0_2[39],piccdata0_2[40],piccdata0_2[41],piccdata0_2[42],piccdata0_2[43],piccdata0_2[44],piccdata0_2[45],piccdata0_2[46],piccdata0_2[47])); piccread mypiccread = (piccread)mylib.resolve("piccread"); status1 = mypiccread(area * 4 + 3,piccdata); if(status1 == 0) { ui->lineEdit_5->setText(QString::asprintf("%02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X ",piccdata[0],piccdata[1],piccdata[2],piccdata[3],piccdata[4],piccdata[5],piccdata0_2[6],piccdata[7],piccdata[8],piccdata[9],piccdata[10],piccdata[11],piccdata[12],piccdata[13],piccdata[14],piccdata[15])); } pcdbeep mypcdbeep = (pcdbeep)mylib.resolve("pcdbeep"); if (mypcdbeep == NULL) //是否成功連接上 idr_beep()函數(shù) { mylib.unload(); QMessageBox::information(NULL, "提示", "請檢查函數(shù)名是否正確!"); return; } result = mypcdbeep(30); //這里函數(shù)指針調(diào)用dll中的idr_beep() 函數(shù) mylib.unload(); } else if(status == 8) { ui->lineEdit_2->clear(); ui->lineEdit_3->clear(); ui->lineEdit_4->clear(); QMessageBox::information(NULL, "提示", "尋卡錯誤,根本就沒有卡在感應(yīng)區(qū)"); } else if(status == 12) { ui->lineEdit_2->clear(); ui->lineEdit_3->clear(); ui->lineEdit_4->clear(); QMessageBox::information(NULL, "提示", "密碼認(rèn)證失敗"); } }
寫入數(shù)據(jù)到IC卡的代碼:
void MainWindow::on_pushButton_12_clicked()//輕松寫卡 { unsigned char serial[4];//可寫數(shù)組 unsigned char area;//讀出的區(qū)號 unsigned char keyA1B0;//AB密碼認(rèn)證 unsigned char picckey[6];//存放卡密碼的數(shù)組 unsigned char piccdata0_2[48];//下標(biāo)0~15存放作為讀出的塊0的數(shù)據(jù),下標(biāo)16~31存放作為讀出的塊1的數(shù)據(jù),下標(biāo)32~47存放作為讀出的塊2的數(shù)據(jù) unsigned char status;//返回 unsigned char result; char s; //以下控制字含義:讀塊0、塊1、塊2,僅讀指定序列號的卡,需要每次指定密碼 if (ui->radioButton->isChecked()) { ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY + NEEDHALT; } else {//內(nèi)部密碼 ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN; } area = ui->comboBox->currentText().toUShort(); keyA1B0 = 1;//用A密碼 QString Key_str = ui->lineEdit->text().trimmed(); if(Key_str.length() < 12) { QMessageBox::information(NULL, "提示", "密碼長度不夠12位!"); ui-?>lineEdit->setFocus(); return; } char c; for (int var = 0; var < 12; ++var) { c = Key_str.simplified().at(var).toLatin1(); if(!((('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) || (('a' <= c) && (c <= 'f')))) { QMessageBox::information(NULL, "提示", "密碼輸入字符不正確!"); //ui-?>lineEdit->setSelection(var,1); ui->lineEdit->setSelection(var,1); ui->lineEdit->setFocus(); return; } } bool ok; for(int i = 0; i < 6; i++) { picckey[i]=QString(Key_str.mid(i*2,2)).toInt(&ok,16); } //第0塊數(shù)據(jù) QChar ch; QString writinf = ui-?>lineEdit_2->text().trimmed(); QString writinf1 = ""; int m = 0; int n = 0; ch = writinf.at(0); s = writinf.at(0).toLatin1(); for(int i=0; i=16) { break; } } } else if(s == ' ') { } else { QMessageBox::information(NULL, "提示", "寫卡數(shù)據(jù)錯誤,請輸入正確的16進(jìn)制數(shù)據(jù)!"); ui->lineEdit_2->setSelection(i,1); ui->lineEdit_2->setFocus(); return; } } if(m16) { QMessageBox::information(NULL, "提示", "第0塊輸入數(shù)據(jù)不足!"); ui-?>lineEdit_2->setFocus(); return; } //第1塊數(shù)據(jù) writinf = ui->lineEdit_3->text().trimmed(); QString writinf2 = ""; m = 16; for(int i=0; i=32) { break; } } } else if(s == ' ') { } else { QMessageBox::information(NULL, "提示", "寫卡數(shù)據(jù)錯誤,請輸入正確的16進(jìn)制數(shù)據(jù)!"); qDebug() < i; ui-?>lineEdit_3->setSelection(i,1); ui->lineEdit_3->setFocus(); return; } } if(m16) { QMessageBox::information(NULL, "提示", "第1塊輸入數(shù)據(jù)不足!"); ui-?>lineEdit_3->setFocus(); return; } //第2塊數(shù)據(jù) writinf = ui->lineEdit_4->text().trimmed(); QString writinf3 = ""; m = 32; for(int i=0; i=48) { break; } } } else if(s == ' ') { } else { QMessageBox::information(NULL, "提示", "寫卡數(shù)據(jù)錯誤,請輸入正確的16進(jìn)制數(shù)據(jù)!"); ui->lineEdit_4->setSelection(i,1); ui->lineEdit_4->setFocus(); return; } } if(m48) { QMessageBox::information(NULL, "提示", "第2塊輸入數(shù)據(jù)不足!"); ui-?>lineEdit_4->setFocus(); return; } QLibrary mylib("OUR_MIFARE.dll"); if (!mylib.load()) //判斷是否正確加載 { QMessageBox::information(NULL, "提示", "動態(tài)庫沒裝載!原因是動態(tài)庫OUR_MIFARE.dll沒放在生成exe文件相同目錄下!"); return; } piccwriteex mypiccwriteex = (piccwriteex)mylib.resolve("piccwriteex"); status = mypiccwriteex(ctrlword,serial,area,keyA1B0,picckey,piccdata0_2); if(status == 0) { pcdbeep mypcdbeep = (pcdbeep)mylib.resolve("pcdbeep"); if (mypcdbeep == NULL) //是否成功連接上 idr_beep()函數(shù) { mylib.unload(); QMessageBox::information(NULL, "提示", "請檢查函數(shù)名是否正確!"); return; } result = mypcdbeep(30); //這里函數(shù)指針調(diào)用dll中的idr_beep() 函數(shù) mylib.unload(); QMessageBox::information(NULL, "提示", "寫卡成功!"); } else if(status == 8) { ui->lineEdit_2->clear(); ui->lineEdit_3->clear(); ui->lineEdit_4->clear(); QMessageBox::information(NULL, "提示", "尋卡錯誤,根本就沒有卡在感應(yīng)區(qū)"); } else if(status == 12) { ui->lineEdit_2->clear(); ui->lineEdit_3->clear(); ui->lineEdit_4->clear(); QMessageBox::information(NULL, "提示", "密碼認(rèn)證失敗"); } }
修改A,B區(qū)密碼:
void MainWindow::on_pushButton_14_clicked() { unsigned char ctrlword;//控制字 unsigned char serial[4];//可寫數(shù)組 unsigned char area;//讀出的區(qū)號 unsigned char keyA1B0;//AB密碼認(rèn)證 unsigned char piccoldkey[6];//存放卡原始密碼的數(shù)組 unsigned char piccdata[17];//存放卡新A密碼(也就是準(zhǔn)備改成的密碼)、密碼權(quán)限訪問字、新B密碼的數(shù)組、指定更改項目的標(biāo)志 unsigned char status;//返回 unsigned char result; if (ui->radioButton->isChecked()) { ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN + EXTERNKEY + NEEDHALT; } else {//內(nèi)部密碼 ctrlword = BLOCK0_EN + BLOCK1_EN + BLOCK2_EN; } area = ui->comboBox->currentText().toUShort(); keyA1B0 = 1;//用A密碼 QString Key_str = ui->lineEdit->text().trimmed(); if(Key_str.length() < 12) { QMessageBox::information(NULL, "提示", "密碼長度不夠12位!"); ui-?>lineEdit->setFocus(); return; } char c; for (int var = 0; var < 12; ++var) { c = Key_str.simplified().at(var).toLatin1(); if(!((('0' <= c) && (c <= '9')) || (('A' <= c) && (c <= 'F')) || (('a' <= c) && (c <= 'f')))) { QMessageBox::information(NULL, "提示", "密碼輸入字符不正確!"); ui-?>lineEdit->setSelection(var,1); ui->lineEdit->setFocus(); return; } } bool ok; for(int i = 0; i < 6; i++) { piccoldkey[i]=QString(Key_str.mid(i*2,2)).toInt(&ok,16); } QChar ch; QString writinf = ui-?>lineEdit_5->text().trimmed(); QString writinf4 = ""; int m = 0; int n = 0; char s; for(int i=0; i=16) { break; } } } else if(s == ' ') { } else { QMessageBox::information(NULL, "提示", "寫卡數(shù)據(jù)錯誤,請輸入正確的16進(jìn)制數(shù)據(jù)!"); ui->lineEdit_5->setSelection(i,1); ui->lineEdit_5->setFocus(); return; } } if(m16) { QMessageBox::information(NULL, "提示", "第3塊輸入數(shù)據(jù)不足!"); ui-?>lineEdit_5->setFocus(); return; } QLibrary mylib("OUR_MIFARE.dll"); if (!mylib.load()) //判斷是否正確加載 { QMessageBox::information(NULL, "提示", "動態(tài)庫沒裝載!原因是動態(tài)庫OUR_MIFARE.dll沒放在生成exe文件相同目錄下!"); return; } piccchangesinglekeyex mypiccchangesinglekeyex = (piccchangesinglekeyex)mylib.resolve("piccchangesinglekeyex"); status = mypiccchangesinglekeyex(ctrlword,serial,area,keyA1B0,piccoldkey,piccdata); QMessageBox::warning(this, "警告", "你確定修改密碼控制塊的數(shù)據(jù)嗎 ?此數(shù)據(jù)塊如果改寫錯誤,數(shù)據(jù)卡將會被限制", QMessageBox::Ok, QMessageBox::Cancel); if(status == 0) { QMessageBox::information(NULL, "提示", "操作成功,寫卡數(shù)據(jù)有效!"); pcdbeep mypcdbeep = (pcdbeep)mylib.resolve("pcdbeep"); if (mypcdbeep == NULL) //是否成功連接上 idr_beep()函數(shù) { mylib.unload(); QMessageBox::information(NULL, "提示", "請檢查函數(shù)名是否正確!"); return; } result = mypcdbeep(30); //這里函數(shù)指針調(diào)用dll中的idr_beep() 函數(shù) mylib.unload(); } else if(status == 8) { QMessageBox::information(NULL, "提示", "尋卡錯誤,根本就沒有卡在感應(yīng)區(qū)"); } else if(status == 12) { QMessageBox::information(NULL, "提示", "密碼認(rèn)證失敗"); } } 審核編輯 黃宇
-
IC卡
+關(guān)注
關(guān)注
2文章
168瀏覽量
34146 -
Qt
+關(guān)注
關(guān)注
1文章
306瀏覽量
37951
發(fā)布評論請先 登錄
相關(guān)推薦
評論