DLCardToolDlg.cpp
21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
// DLCardToolDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "DLCardTool.h"
#include "DLCardToolDlg.h"
#include "afxdialogex.h"
#include <vector>
#include "password_machine.h"
#include "win_http.h"
#include "DLDes.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
static const char* MF_FILE_DIR = "3F00";
static const char* DF01_DIR = "DF01";
static const char BASIC_INFO_FILE_ID = 0x5;
static const char SERVICE_INFO_FILE_ID = 0x15;
static const char* K_DCCK_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0";
static const char* K_F0015_DACK_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6";
static const char* K_F0015_DEAK_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7";
static const char* K_F0015_DAMK1_DV = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8";
static const char* K_F0005_DCMK1 = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF4";
// CDLCardToolDlg 对话框
CDLCardToolDlg::CDLCardToolDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CDLCardToolDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDLCardToolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_RICHEDIT_NOTICE, m_cNotice);
DDX_Control(pDX, IDC_STATIC_EXPLAIN, m_cExplain);
DDX_Control(pDX, IDC_BUTTON_CARDUPDATE, m_cUpdateButton);
//DDX_Control(pDX, IDC_BUTTON_CARDDECODE, m_cDecodeButton);
DDX_Control(pDX, IDC_STATIC_CARDNUM, m_cStaticCardNum);
DDX_Control(pDX, IDC_EDIT_CARDNUM, m_cEditCardNum);
DDX_Control(pDX, IDC_STATIC_STATE, m_cStaticState);
DDX_Control(pDX, IDC_EDIT_STATE, m_cEditState);
DDX_Control(pDX, IDC_STATIC_BOX, m_cStaticBox);
}
BEGIN_MESSAGE_MAP(CDLCardToolDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CARDUPDATE, &CDLCardToolDlg::OnBnClickedButtonCardupdate)
//ON_BN_CLICKED(IDC_BUTTON_CARDDECODE, &CDLCardToolDlg::OnBnClickedButtonCarddecode)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
// CDLCardToolDlg 消息处理程序
BOOL CDLCardToolDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
m_cNotice.SetWindowText(L"");
m_cNotice.SetReadOnly(true);
m_cConfig.ReadConfig();
m_cExplain.SetWindowText \
(L"使用说明:\r \
1.将园区卡放置在读卡器上,点击卡片升级按钮。\r \
2.若提示“升级成功!”,则园区卡升级完成。\r \
3.若提示“升级失败!”,请确认卡片状态或联系工作人员。\r ");
SetWindowText(L"卡片升级工具");
CRect rtDlg;
rtDlg.top = rtDlg.left = 0;
//保持黄金比例,完美!
rtDlg.bottom = 0.618 * 600;
rtDlg.right = 1 * 600;
MoveWindow(&rtDlg);
const int SPACE_LENGTH = 2;
CRect rtPanel;
GetClientRect(&rtPanel);
CRect rtExplain;
rtExplain.top = rtExplain.left = SPACE_LENGTH;
rtExplain.right = rtPanel.right - SPACE_LENGTH;
rtExplain.bottom = rtPanel.Height() * 2 / 10 - SPACE_LENGTH;
m_cExplain.MoveWindow(rtExplain);
const int kStaticHeight = 0.6 * 40;
const int kStaticWidth = 1.5 * 40;
CRect rtStaticCardNum;
rtStaticCardNum.top = rtExplain.bottom + SPACE_LENGTH * 13;
rtStaticCardNum.bottom = rtStaticCardNum.top + kStaticHeight;
rtStaticCardNum.left = (rtPanel.Width() - SPACE_LENGTH * 2 - kStaticWidth * 6) / 4;
rtStaticCardNum.right = rtStaticCardNum.left + kStaticWidth;
m_cStaticCardNum.MoveWindow(rtStaticCardNum);
rtStaticCardNum.top -= SPACE_LENGTH * 1;
rtStaticCardNum.left = rtStaticCardNum.right + SPACE_LENGTH;
rtStaticCardNum.right = rtStaticCardNum.left + kStaticWidth * 2;
m_cEditCardNum.MoveWindow(rtStaticCardNum);
rtStaticCardNum.top += SPACE_LENGTH * 1;
rtStaticCardNum.left = rtExplain.Width() - rtStaticCardNum.right;
rtStaticCardNum.right = rtStaticCardNum.left + kStaticWidth;
m_cStaticState.MoveWindow(rtStaticCardNum);
rtStaticCardNum.top -= SPACE_LENGTH * 1;
rtStaticCardNum.left = rtStaticCardNum.right + SPACE_LENGTH;
rtStaticCardNum.right = rtStaticCardNum.left + kStaticWidth * 2;
m_cEditState.MoveWindow(rtStaticCardNum);
CRect rtBox;
rtBox.top = rtStaticCardNum.top - SPACE_LENGTH * 10;
rtBox.bottom = rtStaticCardNum.bottom + SPACE_LENGTH * 2;
rtBox.left = SPACE_LENGTH;
rtBox.right = rtExplain.right - SPACE_LENGTH;
m_cStaticBox.MoveWindow(rtBox);
CRect rtNotice;
rtNotice.top = rtBox.bottom + SPACE_LENGTH;
rtNotice.left = SPACE_LENGTH;
rtNotice.right = rtPanel.right - SPACE_LENGTH;
rtNotice.bottom = rtPanel.Height() * 6 / 7 - SPACE_LENGTH;
m_cNotice.MoveWindow(&rtNotice);
CRect rtUpdateButton;
rtUpdateButton.top = rtPanel.Height() * 6 / 7 + SPACE_LENGTH;
rtUpdateButton.left = rtPanel.Width() * 2 / 5 + SPACE_LENGTH;
rtUpdateButton.bottom = rtPanel.Height() - SPACE_LENGTH;
rtUpdateButton.right = rtPanel.Width() * 3 / 5 - SPACE_LENGTH;
m_cUpdateButton.MoveWindow(&rtUpdateButton);
m_cStaticCardNum.SetWindowTextW(L"卡号");
m_cStaticState.SetWindowTextW(L"卡片状态");
m_cStaticBox.SetWindowTextW(L"卡片基本信息");
//m_cPWDMachine.ReadConfig();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CDLCardToolDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CDLCardToolDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
template <typename T>
std::string DecToHex(T d, bool show_base = false)
{
std::stringstream ss;
if (show_base)
ss << "0x";
ss << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex << static_cast<int>(d);
return ss.str();
}
void CDLCardToolDlg::OnBnClickedButtonCardupdate()
{
m_cNotice.SetWindowText(L"");
SetMsgLog(L"卡片升级。");
SetMsgLog(L"检查当前安全等级。");
if (m_cConfig.m_iSecurityLevel != 2)
{
SetMsgLog(L"当前安全等级下不需要升级。");
MessageBox(L"当前安全等级下不需要升级。");
return;
}
int iErrorCode = 0;
bool bSuccess = true;
do
{
SetMsgLog(L"连接读卡器。");
iErrorCode = ConnectDLDevice();
if (iErrorCode)
{
SetMsgLog(L"打开读卡器失败!", LOG_ERROR, iErrorCode);
MessageBox(L"请确认读卡器是否连接!", L"读卡器连接失败!");
bSuccess = false;
break;
}
SetMsgLog(L"寻找卡片。");
iErrorCode = FindDLCard();
if (iErrorCode)
{
SetMsgLog(L"寻卡失败!", LOG_ERROR, iErrorCode);
MessageBox(L"请正确放置卡片!", L"寻卡失败!");
bSuccess = false;
break;
}
SetMsgLog(L"读取基本信息。");
BasicInfo biCardInfo;
try
{
biCardInfo = ReadBasicInfo();
}
catch (std::exception &ex)
{
std::string sError = ex.what();
SetMsgLog(L"读取卡片基本信息失败!", LOG_ERROR, atoi(sError.c_str()));
MessageBox(L"读取卡片基本信息失败!", L"升级失败!");
bSuccess = false;
break;
}
SetMsgLog(CString("卡号:") + str_2_wstr(biCardInfo.sCardNum).c_str());
SetMsgLog(L"查询卡片状态。");
CardStateInfo csiCardStateInfo;
try
{
csiCardStateInfo = CheckCardState(biCardInfo);
}
catch (std::exception &ex)
{
std::string sError = ex.what();
CString swError(sError.c_str());
SetMsgLog(swError, LOG_ERROR);
MessageBox(L"升级失败!", L"升级失败!");
bSuccess = false;
break;
}
ShowBasicInfo(str_2_wstr(biCardInfo.sCardNum).c_str(), csiCardStateInfo.csCardState);
if (csiCardStateInfo.etEncryptType == ET_UNKNOWN)
{
SetMsgLog(L"卡片加密状态异常!", LOG_ERROR);
MessageBox(L"卡片加密状态异常,请联系工作人员!", L"升级失败!");
bSuccess = false;
break;
}
if (csiCardStateInfo.csCardState == CS_UNSTORAGE)
{
SetMsgLog(L"卡片未入库,请先将卡片入库!");
MessageBox(L"卡片未入库,请先将卡片入库!", L"升级失败!");
bSuccess = false;
break;
}
else if (csiCardStateInfo.etEncryptType == m_cConfig.m_iEncryptType)
{
SetMsgLog(L"卡片不需要升级!");
MessageBox(L"卡片不需要升级!", L"升级失败!");
bSuccess = false;
break;
}
SetMsgLog(L"获取主控秘钥。");
std::string sMainDCCK, sMainDACK;
try
{
GetMainKey(csiCardStateInfo.etEncryptType, biCardInfo.sCardOldSn, sMainDCCK, sMainDACK);
}
catch (std::exception &ex)
{
std::string sError = ex.what();
SetMsgLog(str_2_wstr(sError).c_str(), LOG_ERROR);
MessageBox(L"获取主控秘钥失败!", L"升级失败!");
bSuccess = false;
break;
}
SetMsgLog(L"计算秘钥。");
std::string sKeyDCCK, sKeyDACK, sKeyDEAK, sKeyDAMK05, sKeyDAMK15;
try
{
sKeyDCCK = CalculateKey(biCardInfo, KT_DCCK, m_cConfig.m_iEncryptType);
sKeyDACK = CalculateKey(biCardInfo, KT_DACK, m_cConfig.m_iEncryptType);
sKeyDEAK = CalculateKey(biCardInfo, KT_DEAK, m_cConfig.m_iEncryptType);
sKeyDAMK05 = CalculateKey(biCardInfo, KT_DAMK, m_cConfig.m_iEncryptType);
sKeyDAMK15 = CalculateKey(biCardInfo, KT_DAMK, m_cConfig.m_iEncryptType);
}
catch (...)
{
SetMsgLog(L"秘钥计算失败!", LOG_ERROR);
MessageBox(L"升级失败!", L"升级失败!");
bSuccess = false;
break;
}
//for test
Log(CString(L"新秘钥DCCK:") + str_2_wstr(sKeyDCCK).c_str());
Log(CString(L"新秘钥DACK:") + str_2_wstr(sKeyDACK).c_str());
Log(CString(L"新秘钥DEAK:") + str_2_wstr(sKeyDEAK).c_str());
Log(CString(L"新秘钥DAMK05:") + str_2_wstr(sKeyDAMK05).c_str());
Log(CString(L"新秘钥DAMK15:") + str_2_wstr(sKeyDAMK15).c_str());
//
SetMsgLog(L"替换秘钥。");
try
{
if (biCardInfo.sCardNum[4] == 1)
{
SetMsgLog(L"替换05DAMK。");
InstallKey(DF01_DIR, sKeyDAMK15, sMainDACK, KT_DAMK, 0);
SetMsgLog(L"替换15DAMK。");
InstallKey(MF_FILE_DIR, sKeyDAMK05, sMainDCCK, KT_DAMK, 0);
}
else
{
SetMsgLog(L"替换05DAMK。");
InstallKey(DF01_DIR, sKeyDAMK15, sMainDACK, KT_DAMK, 1);
SetMsgLog(L"替换15DAMK。");
InstallKey(MF_FILE_DIR, sKeyDAMK05, sMainDCCK, KT_DAMK, 1);
}
SetMsgLog(L"替换DEAK。");
InstallKey(DF01_DIR, sKeyDEAK, sMainDACK, KT_DEAK, 1);
SetMsgLog(L"替换DACK。");
InstallKey(DF01_DIR, sKeyDACK, sMainDACK, KT_DCCK, 0);
SetMsgLog(L"替换DCCK。");
InstallKey(MF_FILE_DIR, sKeyDCCK, sMainDCCK, KT_DCCK, 0);
}
catch (std::exception &ex)
{
std::string sError = ex.what();
SetMsgLog(L"替换秘钥失败!", LOG_ERROR, atoi(sError.c_str()));
MessageBox(L"替换秘钥失败!", L"升级失败!");
bSuccess = false;
break;
}
SetMsgLog(L"写入SN。");
unsigned char aucDAMK05[32] = { 0 };
try
{
WriteData(MF_FILE_DIR, BASIC_INFO_FILE_ID, 56, 32, sKeyDAMK05, biCardInfo.sCardNewSn);
}
catch (std::exception &ex)
{
std::string sError = ex.what();
SetMsgLog(L"写入sn失败!", LOG_ERROR, atoi(sError.c_str()));
MessageBox(L"写入sn失败!", L"升级失败!");
bSuccess = false;
break;
}
if (csiCardStateInfo.csCardState == CS_USE)
{
SetMsgLog(L"写入业务数据。");
static const uint16_t SERVICE_INFO_DATA_LEN_MAX = 0xFA; // 业务数据最大长度250bytes.
uint32_t u32DataLen = csiCardStateInfo.sServiceData.GetLength() / 2; // Why "/ 2"? refer to the comment at the front of this file.
if (u32DataLen > SERVICE_INFO_DATA_LEN_MAX)
{
SetMsgLog(L"业务信息过长,写入sn失败!", LOG_ERROR);
MessageBox(L"业务信息过长,写入sn失败!", L"升级失败!");
bSuccess = false;
break;
}
std::string sDataLen = DecToHex(_byteswap_ulong(u32DataLen)); // Convert to big endian first and then to HEX string.
csiCardStateInfo.sServiceData = str_2_wstr(sDataLen).c_str() + csiCardStateInfo.sServiceData;
try
{
WriteData(DF01_DIR, SERVICE_INFO_FILE_ID, 0, csiCardStateInfo.sServiceData.GetLength(), sKeyDAMK15, wstr_2_str(csiCardStateInfo.sServiceData));
}
catch (std::exception &ex)
{
std::string sError = ex.what();
SetMsgLog(L"写入业务信息失败!", LOG_ERROR, atoi(sError.c_str()));
MessageBox(L"写入业务信息失败!", L"升级失败!");
bSuccess = false;
break;
}
}
} while (0);
SetMsgLog(L"关闭读卡器。");
iErrorCode = DisconnectDLDevice();
if (iErrorCode)
{
SetMsgLog(L"断开读卡器失败!", LOG_ERROR, iErrorCode);
}
if (bSuccess)
{
MessageBox(L"升级成功!");
}
}
void CDLCardToolDlg::GetMainKey(ENCRYPT_TYPE etEncryptType, std::string sSN, std::string &sDCCK, std::string &sDACK)
{
if (etEncryptType == ET_PWM)
{
sDCCK = m_cPWDMachine.GetPassword("0000", sSN, 10000, m_cConfig.m_sPWMIP, m_cConfig.m_u32PWMPort);
sDACK = m_cPWDMachine.GetPassword("0000", sSN, 10000, m_cConfig.m_sPWMIP, m_cConfig.m_u32PWMPort);
}
else if (etEncryptType == ET_DES)
{
const char* pDCCKDL = "43434bff43434bff";
const char* pDACKDL = "41434bff41434bff";
unsigned char aucCardSNHex[16] = { 0 };
unsigned char aucDCCKDL[8] = { 0 }, aucDACKDL[8] = { 0 };
StringToHex((unsigned char*)pDCCKDL, 16, aucDCCKDL);
StringToHex((unsigned char*)pDACKDL, 16, aucDACKDL);
StringToHex((unsigned char*)sSN.c_str(), 32, aucCardSNHex);
unsigned char aucNewDCCKHex[16] = { 0 }, aucNewDACKHex[16] = { 0 }, aucNewDCCK[32] = { 0 }, aucNewDACK[32] = { 0 };
Encrypt(aucCardSNHex, 16, aucDCCKDL, aucNewDCCKHex);
Encrypt(aucCardSNHex, 16, aucDACKDL, aucNewDACKHex);
HexToChar(aucNewDCCKHex, 16, aucNewDCCK);
HexToChar(aucNewDACKHex, 16, aucNewDACK);
std::string sDCCKKey((char*)aucNewDCCK, 32);
std::string sDACKKey((char*)aucNewDACK, 32);
sDCCK = sDCCKKey;
sDACK = sDACKKey;
}
else if (etEncryptType == ET_NO)
{
sDCCK = K_DCCK_DV;
sDACK = K_F0015_DACK_DV;
}
}
void CDLCardToolDlg::SetMsgLog(CString sText, LOG_TYPE emLogType /*= LOG_TRACE*/, int iErrorCode /*= 0*/)
{
CString sErrorCode;
if (iErrorCode)
{
sErrorCode.Format(L",错误码: %d", iErrorCode);
}
CString sLogType = emLogType == LOG_TRACE ? L"<trace> " : L"<error> ";
Log(sLogType + sText + sErrorCode);
long lStart = 0, lEnd = 0;
m_cNotice.GetSel(lStart, lEnd);
m_cNotice.SetSel(lEnd, lEnd);
if (lEnd)
{
m_cNotice.ReplaceSel(L"\r\n");
m_cNotice.GetSel(lStart, lEnd);
m_cNotice.SetSel(lEnd, lEnd);
}
m_cNotice.ReplaceSel(sText);
if (emLogType == LOG_ERROR)
{
m_cNotice.GetSel(lStart, lEnd);
m_cNotice.SetSel(lEnd - sText.GetLength(), lEnd);
CHARFORMAT2 stuCF;
ZeroMemory(&stuCF, sizeof(CHARFORMAT2));
stuCF.cbSize = sizeof(CHARFORMAT2);
stuCF.dwMask = CFM_COLOR;
stuCF.dwEffects = NULL;
stuCF.crTextColor = RGB(255, 0, 0);
m_cNotice.SetSelectionCharFormat(stuCF);
}
}
HBRUSH CDLCardToolDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd == &m_cExplain)
{
pDC->SetTextColor(RGB(255, 0, 0));
}
return hbr;
}
CardStateInfo CDLCardToolDlg::CheckCardState(BasicInfo biCardInfo)
{
CardStateInfo stuServiceData;
WinHttp cWinHttp;
cWinHttp.ConnectHost(m_cConfig.m_sHost, m_cConfig.m_u16Port, false);
auto& request = cWinHttp.OpenRequest(WinHttp::Method::POST, "spi/card/updateDeviceId.temp");
CString sJson;
sJson.Format(L"{\"cardNo\":\"%s\",\"deviceId\":\"%s\"}", str_2_wstr(biCardInfo.sCardNum).c_str(), str_2_wstr(biCardInfo.sCardNewSn).c_str());
//for test
//Log(sJson);
//
request.SetPostData(wstr_2_str(sJson.GetBuffer()).c_str());
request.Send();
uint32_t u32StatusCode = request.GetResponseStatus();
const int kHTTPOK = 200;
if (kHTTPOK != u32StatusCode)
{
CString sError;
sError.Format(L"网络请求错误! 错误码: %d" + u32StatusCode);
throw std::exception(wstr_2_str(sError).c_str());
}
std::string sResponseBody = request.ReadResponseBody();
if (sResponseBody.empty())
{
CString sError = L"获取服务器响应数据失败,请确保网络连接正常!";
throw std::exception(wstr_2_str(sError).c_str());;
}
stuServiceData = ReadJson(str_2_wstr(sResponseBody).c_str());
////for test
//Log(stuServiceData.sServiceData);
////
if (stuServiceData.csCardState != CS_UNSTORAGE)
{
char* pDCCKOriginal = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0";
std::string sDCCKPMW = m_cPWDMachine.GetPassword("0000", biCardInfo.sCardOldSn, 10000, m_cConfig.m_sPWMIP, m_cConfig.m_u32PWMPort);
const char* pDCCKDL = "43434bff43434bff";
unsigned char aucCardSNHex[16] = { 0 };
unsigned char aucDCCKDL[8] = { 0 };
StringToHex((unsigned char*)pDCCKDL, 16, aucDCCKDL);
StringToHex((unsigned char*)biCardInfo.sCardNewSn.c_str(), 32, aucCardSNHex);
unsigned char aucNewKeyHex[16] = { 0 }, aucDCCKDES[32] = { 0 };
Encrypt(aucCardSNHex, 16, aucDCCKDL, aucNewKeyHex);
HexToChar(aucNewKeyHex, 16, aucDCCKDES);
if (!AuthenticationDLKey((unsigned char*)sDCCKPMW.c_str()))
{
stuServiceData.etEncryptType = ET_PWM;
}
else if (!AuthenticationDLKey((unsigned char*)pDCCKOriginal))
{
stuServiceData.etEncryptType = ET_NO;
}
else if (!AuthenticationDLKey(aucDCCKDES))
{
stuServiceData.etEncryptType = ET_DES;
}
}
return stuServiceData;
}
CardStateInfo CDLCardToolDlg::ReadJson(CString sResponseBody)
{
CardStateInfo stuServiceData;
static const std::string JSON_TAG_STATUS = "status";
static const std::string JSON_TAG_SECRETSTR = "secretStr";
static const std::string JSON_VAL_UNSTORAGE = "0";
static const std::string JSON_VAL_STORAGE = "1";
static const std::string JSON_VAL_USE = "2";
namespace PT = boost::property_tree;
bool login_res = false;
try //Parse the configuration file
{
PT::ptree ptree;
std::stringstream ss;
ss << wstr_2_str(sResponseBody);
PT::read_json(ss, ptree);
std::string sStatus = ptree.get<std::string>(JSON_TAG_STATUS);
if (sStatus.compare(JSON_VAL_UNSTORAGE) == 0)
{
stuServiceData.csCardState = CS_UNSTORAGE;
}
else if (sStatus.compare(JSON_VAL_STORAGE) == 0)
{
stuServiceData.csCardState = CS_STORAGE;
}
else if (sStatus.compare(JSON_VAL_USE) == 0)
{
stuServiceData.csCardState = CS_USE;
stuServiceData.sServiceData = ptree.get<std::string>(JSON_TAG_SECRETSTR).c_str();
}
}
catch (...)
{
throw std::exception("解析服务器返回的信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!", LOG_ERROR);
}
return stuServiceData;
}
BasicInfo CDLCardToolDlg::ReadBasicInfo()
{
BasicInfo biCardBasicInfo;
unsigned char aucDirMF[4] = { 0 };
unsigned char aucCardBasicInfo[88] = { 0 };
memcpy(aucDirMF, MF_FILE_DIR, 4);
int iErrorCode = ReadDLCardData(aucDirMF, BASIC_INFO_FILE_ID, 0, 88, aucCardBasicInfo); //实际上取出了所有基本信息
if (iErrorCode)
{
std::stringstream ssErrorCode;
ssErrorCode << iErrorCode;
throw std::exception(ssErrorCode.str().c_str());
}
std::string sCardnum((char *)aucCardBasicInfo, 0, 12);
std::string sOldSn((char*)aucCardBasicInfo, 56, 88);
std::string sNewSn = sCardnum + "44494c49" + sCardnum;
biCardBasicInfo.sCardNum = sCardnum;
biCardBasicInfo.sCardNewSn = sNewSn;
biCardBasicInfo.sCardOldSn = sOldSn;
return biCardBasicInfo;
}
std::string CDLCardToolDlg::CalculateKey(BasicInfo biCardBasicInfo, KEY_TYPE ktKeyType, int iEncryptType)
{
std::string sNewKey;
if (iEncryptType == 0)
{
std::string sKeyType;
switch (ktKeyType)
{
case KT_DACK:
case KT_DCCK:
{
sKeyType = "0000";
break;
}
case KT_DEAK:
{
sKeyType = "0001";
break;
}
case KT_DAMK:
{
sKeyType = "0002";
break;
}
}
sNewKey = m_cPWDMachine.GetPassword(sKeyType, biCardBasicInfo.sCardNewSn, 10000, m_cConfig.m_sPWMIP, m_cConfig.m_u32PWMPort);
}
else
{
const char* pDCCKDL = "43434bff43434bff";
const char* pDACKDL = "41434bff41434bff";
const char* pDEAKDL = "45414bff45414bff";
const char* pDAMKDL = "414d4b31414d4b31";
unsigned char aucCardSNHex[16] = { 0 };
unsigned char aucDCCKDL[8] = { 0 }, aucDACKDL[8] = { 0 }, aucDEAKDL[8] = { 0 }, aucDAMKDL[8] = { 0 };
StringToHex((unsigned char*)pDCCKDL, 16, aucDCCKDL);
StringToHex((unsigned char*)pDACKDL, 16, aucDACKDL);
StringToHex((unsigned char*)pDEAKDL, 16, aucDEAKDL);
StringToHex((unsigned char*)pDAMKDL, 16, aucDAMKDL);
StringToHex((unsigned char*)biCardBasicInfo.sCardNewSn.c_str(), 32, aucCardSNHex);
unsigned char aucNewKeyHex[16] = { 0 }, aucNewKey[32] = { 0 };
switch (ktKeyType)
{
case KT_DCCK:
{
Encrypt(aucCardSNHex, 16, aucDCCKDL, aucNewKeyHex);
break;
}
case KT_DACK:
{
Encrypt(aucCardSNHex, 16, aucDACKDL, aucNewKeyHex);
break;
}
case KT_DEAK:
{
Encrypt(aucCardSNHex, 16, aucDEAKDL, aucNewKeyHex);
break;
}
case KT_DAMK:
{
Encrypt(aucCardSNHex, 16, aucDAMKDL, aucNewKeyHex);
break;
}
}
HexToChar(aucNewKeyHex, 16, aucNewKey);
std::string sKey((char*)aucNewKey, 32);
sNewKey = sKey;
}
return sNewKey;
}
void CDLCardToolDlg::InstallKey(std::string sDir, std::string sNewKey, std::string sMainKey, KEY_TYPE ktKeyType, int iKeyID)
{
//unsigned char aucDir[2] = { 0 }/*, aucNewKey[16] = { 0 }, aucMainKey[16] = { 0 }*/;
//StringToHex((unsigned char*)sDir.c_str(), 4, aucDir);
//StringToHex((unsigned char*)sNewKey.c_str(), 32, aucNewKey);
//StringToHex((unsigned char*)sMainKey.c_str(), 32, aucMainKey);
int iErrorCode = 0;
iErrorCode = InstallDLKey((unsigned char*)sDir.c_str(), iKeyID, (unsigned char*)sNewKey.c_str(), ktKeyType, (unsigned char*)sMainKey.c_str());
if (iErrorCode)
{
std::stringstream ssErrorCode;
ssErrorCode << iErrorCode;
std::string sErrorCode = ssErrorCode.str();
throw std::exception(ssErrorCode.str().c_str());
}
}
void CDLCardToolDlg::WriteData(std::string sDir, int iFileID, int iOffest, int iLen, std::string sKey, std::string sData)
{
unsigned char aucDir[2] = { 0 }, aucKey[16] = { 0 };
//int iHexLen = iLen / 2;
//int iHexOffest = iOffest / 2;
//unsigned char* pData = new unsigned char(iHexLen);
//StringToHex((unsigned char*)sData.c_str(), iLen, pData);
//StringToHex((unsigned char*)sKey.c_str(), 32, aucKey);
int iError = WriteDLCardData((unsigned char*)sDir.c_str(), iFileID, iOffest, iLen, (unsigned char*)sData.c_str(), (unsigned char*)sKey.c_str());
if (iError)
{
std::stringstream ssErrorCode;
ssErrorCode << iError;
throw std::exception(ssErrorCode.str().c_str());
}
//delete pData;
}
void CDLCardToolDlg::ShowBasicInfo(CString sCardNum, CARD_STATE csCardState)
{
m_cEditCardNum.SetWindowTextW(sCardNum);
CString sCardState;
switch (csCardState)
{
case CS_UNSTORAGE:
{
sCardState = L"未入库";
break;
}
case CS_STORAGE:
{
sCardState = L"已入库,未开卡";
break;
}
case CS_USE:
{
sCardState = L"已开卡";
break;
}
}
m_cEditState.SetWindowTextW(sCardState);
}