串口收发数据加密处理

这个项目是用于生产过程中创建独立序列号的上位机设计方案。

该方案是上位机通过接受外设备发送过来的8个字节数据后,通过加密处理生成16字节秘钥与标签码,并将这些信息通过串口发送回给外设备。

因为涉及到商业机密,这里只列出部分代码。

部分源码

  1 // Demo1Dlg.cpp : implementation file
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "Demo1.h"
  6 #include "Demo1Dlg.h"
  7 #include  <time.h> 
  8 
  9 typedef int (*PFUN)( const unsigned int        fobIdLen
 10                     ,const unsigned int        maxPepArrayLen
 11                     ,const unsigned int        maxRkeArrayLen
 12                     ,const unsigned char    *encryptedFobId
 13                     ,unsigned int            *actualPepArrayLen
 14                     ,unsigned int            *actualRkeArrayLen
 15                     ,unsigned char            *dtEncryptedPepArray
 16                     ,unsigned char            *dtEncryptedRkeArray);
 17 typedef void (*AESFUN)(unsigned char *pskey,unsigned char *blk);
 18 typedef int (*C2HFUN)(char*input,unsigned char *output,int length);
 19 
 20 //CRC-8 for ATM HEC (x8+x2+x+1)
 21 unsigned char cal_crc(unsigned char *vptr, unsigned char len)
 22 {
 23   const unsigned char *data = vptr;
 24   unsigned int crc = 0;
 25   int i, j;
 26   for (j = len; j; j--, data++) 
 27   {
 28     crc ^= (*data << 8);
 29     for (i = 8; i; i--) 
 30     {
 31       if (crc & 0x8000)
 32         crc ^= (unsigned int)(0x1070 << 3);
 33       crc <<= 1;
 34     }
 35   }
 36   return (unsigned char)(crc >> 8);
 37 }
 38 
 39 #ifdef _DEBUG
 40 #define new DEBUG_NEW
 41 #undef THIS_FILE
 42 static char THIS_FILE[] = __FILE__;
 43 #endif
 44 CFont font;
 45 
 46 static unsigned long LastBuf[3];
 47 
 48 /////////////////////////////////////////////////////////////////////////////
 49 // CAboutDlg dialog used for App About
 50 
 51 class CAboutDlg : public CDialog
 52 {
 53 public:
 54     CAboutDlg();
 55 
 56 // Dialog Data
 57     //{{AFX_DATA(CAboutDlg)
 58     enum { IDD = IDD_ABOUTBOX };
 59     //}}AFX_DATA
 60 
 61     // ClassWizard generated virtual function overrides
 62     //{{AFX_VIRTUAL(CAboutDlg)
 63     protected:
 64     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
 65     //}}AFX_VIRTUAL
 66 
 67 // Implementation
 68 protected:
 69     //{{AFX_MSG(CAboutDlg)
 70     //}}AFX_MSG
 71     DECLARE_MESSAGE_MAP()
 72 };
 73 
 74 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
 75 {
 76     //{{AFX_DATA_INIT(CAboutDlg)
 77     //}}AFX_DATA_INIT
 78 }
 79 
 80 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
 81 {
 82     CDialog::DoDataExchange(pDX);
 83     //{{AFX_DATA_MAP(CAboutDlg)
 84     //}}AFX_DATA_MAP
 85 }
 86 
 87 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
 88     //{{AFX_MSG_MAP(CAboutDlg)
 89         // No message handlers
 90     //}}AFX_MSG_MAP
 91 END_MESSAGE_MAP()
 92 
 93 /////////////////////////////////////////////////////////////////////////////
 94 // CDemo1Dlg dialog
 95 
 96 CDemo1Dlg::CDemo1Dlg(CWnd* pParent /*=NULL*/)
 97     : CDialog(CDemo1Dlg::IDD, pParent)
 98 {
 99     //{{AFX_DATA_INIT(CDemo1Dlg)
100     m_Edit2 = _T("");
101     m_Edit16 = _T("");
102     m_Edit12 = _T("");
103     m_Edit15 = _T("");
104     m_Edit1 = _T("");
105     m_Edit17 = 0;
106     m_Edit18 = _T("");
107     m_Edit3 = 0;
108     //}}AFX_DATA_INIT
109     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
110     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
111 }
112 
113 void CDemo1Dlg::DoDataExchange(CDataExchange* pDX)
114 {
115     CDialog::DoDataExchange(pDX);
116     //{{AFX_DATA_MAP(CDemo1Dlg)
117     DDX_Text(pDX, IDC_EDIT2, m_Edit2);
118     DDX_Text(pDX, IDC_EDIT16, m_Edit16);
119     DDX_Text(pDX, IDC_EDIT12, m_Edit12);
120     DDX_Text(pDX, IDC_EDIT15, m_Edit15);
121     DDX_Text(pDX, IDC_EDIT1, m_Edit1);
122     DDX_Text(pDX, IDC_EDIT17, m_Edit17);
123     DDX_Text(pDX, IDC_EDIT18, m_Edit18);
124     DDX_Text(pDX, IDC_EDIT3, m_Edit3);
125     //}}AFX_DATA_MAP
126 }
127 
128 BEGIN_MESSAGE_MAP(CDemo1Dlg, CDialog)
129     //{{AFX_MSG_MAP(CDemo1Dlg)
130     ON_WM_SYSCOMMAND()
131     ON_WM_PAINT()
132     ON_WM_QUERYDRAGICON()
133     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
134     ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
135     ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
136     ON_WM_ERASEBKGND()
137     ON_WM_CTLCOLOR()
138     ON_WM_SETCURSOR()
139     ON_WM_CLOSE()
140     //}}AFX_MSG_MAP
141 END_MESSAGE_MAP()
142 
143 /////////////////////////////////////////////////////////////////////////////
144 // CDemo1Dlg message handlers
145 
146 BOOL CDemo1Dlg::OnInitDialog()
147 {
148     CDialog::OnInitDialog();
149     
150     //SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE)|WS_EX_LAYERED);
151     //SetLayeredWindowAttributes(0,200,LWA_ALPHA);
152     // Add "About..." menu item to system menu.
153 
154     // IDM_ABOUTBOX must be in the system command range.
155     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
156     ASSERT(IDM_ABOUTBOX < 0xF000);
157 
158     CMenu* pSysMenu = GetSystemMenu(FALSE);
159     if (pSysMenu != NULL)
160     {
161         CString strAboutMenu;
162         strAboutMenu.LoadString(IDS_ABOUTBOX);
163         if (!strAboutMenu.IsEmpty())
164         {
165             pSysMenu->AppendMenu(MF_SEPARATOR);
166             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
167         }
168     }
169 
170     // Set the icon for this dialog.  The framework does this automatically
171     //  when the application's main window is not a dialog
172     SetIcon(m_hIcon, TRUE);            // Set big icon
173     SetIcon(m_hIcon, FALSE);        // Set small icon
174     
175     // TODO: Add extra initialization here
176     font.CreatePointFont(200,"??");
177     //CEdit *pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT6);
178     //pEdit->SetFont(&font);
179     //pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT12);
180     //pEdit->SetFont(&font);
181     
182     CEdit *pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT1);
183     pEdit->SetFont(&font);
184     pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT2);
185     pEdit->SetFont(&font);
186     pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT16);
187     pEdit->SetFont(&font);
188     //pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT15);
189     //pEdit->SetFont(&font);
190         
191     CComboBox *pc=(CComboBox *)this->GetDlgItem(IDC_COMBO1);//com serial com
192     CComboBox *pb=(CComboBox *)this->GetDlgItem(IDC_COMBO2);//com baudrate
193         
194     pc->SetCurSel(1);
195     pb->SetCurSel(2);
196     GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_HIDE);
197     //GetDlgItem(IDC_BUTTON6)->ShowWindow(SW_HIDE);
198     //ShowWindow(SW_MINIMIZE); 
199     //Invalidate(FALSE);
200     return TRUE;  // return TRUE  unless you set the focus to a control
201 }
202 
203 void CDemo1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
204 {
205     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
206     {
207         CAboutDlg dlgAbout;
208         dlgAbout.DoModal();
209     }
210     else
211     {
212         CDialog::OnSysCommand(nID, lParam);
213     }
214 }
215 
216 // If you add a minimize button to your dialog, you will need the code below
217 //  to draw the icon.  For MFC applications using the document/view model,
218 //  this is automatically done for you by the framework.
219 
220 void CDemo1Dlg::OnPaint() 
221 {
222     if (IsIconic())
223     {
224         
225         CPaintDC dc(this); // device context for painting
226 
227         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
228 
229         // Center icon in client rectangle
230         int cxIcon = GetSystemMetrics(SM_CXICON);
231         int cyIcon = GetSystemMetrics(SM_CYICON);
232         CRect rect;
233         GetClientRect(&rect);
234         int x = (rect.Width() - cxIcon + 1) / 2;
235         int y = (rect.Height() - cyIcon + 1) / 2;
236 
237         // Draw the icon
238         dc.DrawIcon(x, y, m_hIcon);
239     }
240     else
241     {
242         CDialog::OnPaint();
243         //Invalidate(FALSE);
244         //Invalidate(true);
245     }
246     //ShowWindow(SW_MINIMIZE); 
247 }
248 
249 // The system calls this to obtain the cursor to display while the user drags
250 //  the minimized window.
251 HCURSOR CDemo1Dlg::OnQueryDragIcon()
252 {
253     return (HCURSOR) m_hIcon;
254 }
255 
256 BEGIN_EVENTSINK_MAP(CDemo1Dlg, CDialog)
257     //{{AFX_EVENTSINK_MAP(CDemo1Dlg)
258     ON_EVENT(CDemo1Dlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
259     //}}AFX_EVENTSINK_MAP
260 END_EVENTSINK_MAP()
261 
262 void CDemo1Dlg::OnButton1() 
263 {
264     // TODO: Add your control notification handler code here
265     CMSComm *pcom = (CMSComm *)this->GetDlgItem(IDC_MSCOMM1);
266     CComboBox *pc=(CComboBox *)this->GetDlgItem(IDC_COMBO1);//com serial com
267     CComboBox *pb=(CComboBox *)this->GetDlgItem(IDC_COMBO2);//com baudrate
268     if(pcom->GetPortOpen())
269         pcom->SetPortOpen(FALSE);
270     pcom->SetCommPort(pc->GetCurSel());                //Port interface
271     pcom->SetInBufferSize(512);                            //Input buffer size
272     pcom->SetOutBufferSize(512);                            //Output buffer size
273     if(pb->GetCurSel()==0)
274     {
275         pcom->SetSettings("2400,n,8,1");                    //Interface configuration: 2400,n,8,1
276     }
277     else if(pb->GetCurSel()==1)
278     {
279         pcom->SetSettings("4800,n,8,1");                    //Interface configuration: 4800,n,8,1
280     }
281     else if(pb->GetCurSel()==3)
282     {
283         pcom->SetSettings("115200,n,8,1");                //Interface configuration: 115200,n,8,1
284     }
285     else
286     {
287         pcom->SetSettings("9600,n,8,1");                    //Interface configuration: 9600,n,8,1
288     }
289     pcom->SetInputMode(1);                                //1: Read data by binary checking
290     pcom->SetRThreshold(8);                                //onComm event trigger when N byte in input buffer
291     pcom->SetInputLen(0);                                    // set input length 0.
292     UpdateData(true);
293         if(!pcom->GetPortOpen())            //Open Port
294         {
295           pcom->SetPortOpen(TRUE);
296           
297           GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_HIDE);
298           GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_SHOW);
299         }
300         else
301         {
302           AfxMessageBox("Can not open serial port.");
303         }
304     pcom->GetInput();                    //Clear input buffer
305     
306     UpdateData(false);
307 }
308 /*
309 void CDemo1Dlg::OnButton2() 
310 {
311     // TODO: Add your control notification handler code here
312     //UpdateData(TRUE);    //Read data
313     //m_Comm1.SetOutput(COleVariant(m_EditTxData));//Transmitting
314 }
315 */
316 //Only for 3 long bytes, 0x1234567B-->"1234567B"
317 static int Lhex_to_hex(unsigned long* lb,unsigned char* by,int identify)
318 {
319     unsigned long tmp;
320     unsigned char k;
321     int index =0;
322     for(int i=0;i<3;i++)
323     {
324         tmp = lb[i];
325         
326         //1
327         k=(unsigned char)(tmp>>28) & 0x0F;
328         if(9>=k)
329         {
330             *by++=k+'0';
331         }
332         else if((k>=0x0a)&&(0x0f>=k))
333         {
334             *by++=k+0x37;
335         }
336         index++;
337         //2
338         k=(unsigned char)(tmp>>24) & 0x0F;
339         if(9>=k)
340         {
341             *by++=k+'0';
342         }
343         else if((k>=0x0a)&&(0x0f>=k))
344         {
345             *by++=k+0x37;
346         }
347         index++;
348         //3
349         k=(unsigned char)(tmp>>20) & 0x0F;
350         if(9>=k)
351         {
352             *by++=k+'0';
353         }
354         else if((k>=0x0a)&&(0x0f>=k))
355         {
356             *by++=k+0x37;
357         }
358         index++;
359         //4
360         k=(unsigned char)(tmp>>16) & 0x0F;
361         if(9>=k)
362         {
363             *by++=k+'0';
364         }
365         else if((k>=0x0a)&&(0x0f>=k))
366         {
367             *by++=k+0x37;
368         }
369         index++;
370         if((identify==1)||(identify==0))
371         {
372             *by++ = ' ';
373             index++;
374             if(identify==1)
375             {
376                 *by++=' ';
377                 index++;
378                 *by++=' ';
379                 index++;
380             }
381         }
382         //5
383         k=(unsigned char)(tmp>>12) & 0x0F;
384         if(9>=k)
385         {
386             *by++=k+'0';
387         }
388         else if((k>=0x0a)&&(0x0f>=k))
389         {
390             *by++=k+0x37;
391         }
392         index++;
393         //6
394         k=(unsigned char)(tmp>>8) & 0x0F;
395         if(9>=k)
396         {
397             *by++=k+'0';
398         }
399         else if((k>=0x0a)&&(0x0f>=k))
400         {
401             *by++=k+0x37;
402         }
403         index++;
404         //7
405         k=(unsigned char)(tmp>>4) & 0x0F;
406         if(9>=k)
407         {
408             *by++=k+'0';
409         }
410         else if((k>=0x0a)&&(0x0f>=k))
411         {
412             *by++=k+0x37;
413         }
414         index++;
415         //8
416         k=(unsigned char)(tmp) & 0x0F;
417         if(9>=k)
418         {
419             *by++=k+'0';
420         }
421         else if((k>=0x0a)&&(0x0f>=k))
422         {
423             *by++=k+0x37;
424         }
425         index++;
426         if((identify==1)||(identify==0))
427             {
428                 if(identify==1)
429                 {
430                     *by++ = '\r';
431                     *by++ ='\n';
432                     index++;
433                     index++;
434                 }
435                 else
436                 {
437                     if(i!=2)
438                     {
439                         *by++ = ' ';
440                         index++;
441                     }
442                 }
443             }
444     }
445     if((identify==0)||(identify==1))
446     {
447         *by++ = '\r';
448         *by++ = '\n';
449         index++;
450         index++;
451     }
452     /*if(identify==3)
453         {
454             *by++ ='-';
455             index++;
456         }*/
457     return index;
458 }
459 typedef union
460 {
461   unsigned long buf[4];
462   unsigned char bybuf[16];
463 }u32arr_structed;
464 
465 unsigned char dll_key_fob[16]={0x56,0x38,0x97,0x3f,0x56,0x70,0x22,0x88,0x3b,0x52,0xfe,0x5a,0x01,0x32,0x08,0x49};
466 unsigned char dll_key_PEP[16]={0x13,0xff,0x13,0x84,0x43,0x86,0x43,0x4a,0x23,0x53,0xe6,0x54,0xd3,0x02,0x00,0x34};
467 unsigned char dll_key_RKE[16]={0x15,0x42,0x71,0x73,0x6a,0x41,0xe3,0x45,0xde,0x40,0xba,0x34,0x42,0x82,0xaa,0xe2};
468 
469 unsigned char bcm_key_PEP[16]={0x18,0x92,0x14,0x81,0x89,0xf2,0x4f,0xa4,0x92,0x52,0xfe,0x5a,0x4e,0x32,0xcc,0x3d};
470 unsigned char bcm_key_RKE[16]={0x12,0x52,0x63,0x54,0x25,0x4b,0xc3,0x4b,0x23,0x00,0xdd,0xbb,0xc3,0x82,0xfd,0xf4};
471 
472 void CDemo1Dlg::OnOnCommMscomm1() 
473 {
474     // TODO: Add your control notification handler code here
475     u32arr_structed u32_buff;
476     u32arr_structed u32_xxtea_SK;
477     //unsigned char expansionkey[15*16];
478     VARIANT variant_inp;
479     COleSafeArray safearray_inp;
480     LONG len,k;
481     int j;
482     unsigned long tmp_l=0;
483     unsigned char Id_invalid,saveContext,u8_tmp;
484     
485     unsigned int peplen,rkelen;
486     unsigned char fob_buf[16];
487     unsigned char pep_buf[16];
488     unsigned char rke_buf[16];
489     unsigned char key_buf[48],index;
490     BYTE rxdata[100];
491     CString strtemp;
492     TCHAR chCurDir[MAX_PATH] = {0};
493         
494     CMSComm *pcom = (CMSComm *)this->GetDlgItem(IDC_MSCOMM1);
495     if(pcom->GetCommEvent()==2)            //CommEvent=2 Indicate data received
496     {
497         GetCurrentDirectory(MAX_PATH, chCurDir);
498         SetCurrentDirectory(("C:\\Windows"));
499         HMODULE hModule = ::LoadLibrary("AES_DLL.dll");
500         if(hModule==NULL)
501         {
502             SetCurrentDirectory(chCurDir);
503             ::FreeLibrary(hModule);
504             AfxMessageBox("Lost AES_DLL.dll.");
505             return;
506         }
507         
508         time_t t =time(NULL);            //timestanp
509         tm *tp = localtime(&t);
510         variant_inp = pcom->GetInput();    //get byte from input buffer
511         safearray_inp = variant_inp;        //VARIANTD to COleSafeArray type
512         len = safearray_inp.GetOneDimSize();    //length of rx data
513         
514             for(k=0;k<len;k++)            
515             {
516                 safearray_inp.GetElement(&k,rxdata+k);//read rx data and write into radata buffer
517             }
518             if(len!=8)
519             {
520                 SetCurrentDirectory(chCurDir);
521                 ::FreeLibrary(hModule);
522                 return;
523             }
524             for(k=0;k<8;k++)
525                 u32_buff.bybuf[k]=rxdata[k];
526             
527             saveContext = 0;
528             // serial number or timestamp is different means a new Fob sign up.
529             if((u32_buff.buf[0]!=LastBuf[0]) || (u32_buff.buf[1]!=LastBuf[1]))
530             {
531                 saveContext = 1;
532                 strtemp.Format("%d-%d-%d %d:%d:%d",tp->tm_year+1900,tp->tm_mon+1,tp->tm_mday,\
533                                 tp->tm_hour,tp->tm_min,tp->tm_sec);
534                 m_Edit18=strtemp;
535                 for(k=0;k<2;k++)
536                     LastBuf[k]=u32_buff.buf[k];
537             }
538             else
539             {
540                 for(k=0;k<2;k++)
541                     u32_buff.buf[k]=LastBuf[k];
542             }
543             m_Edit3++;
544             Id_invalid=0;
545             if((u32_buff.buf[0]&0x0000ffff)==0)
546             {
547                 Id_invalid=1;
548                 saveContext = 0;//the serial number is invalid.
549             }
550             UpdateData(FALSE);
551             for(j=0,k=0;k<4;k++)    //hex to char
552             {
553                 u8_tmp=u32_buff.bybuf[k];
554 
555                 if((u8_tmp>>4)>9)//hi
556                     rxdata[j++]=(u8_tmp>>4)+0x37;
557                 else rxdata[j++]=(u8_tmp>>4)+'0';
558                 if((u8_tmp&0x0f)>9)//lo
559                   rxdata[j++] = (u8_tmp&0x0f)+0x37;
560                 else rxdata[j++] = (u8_tmp&0x0f)+'0';
561             }
562             for(k=0;k<8;k++)                    //Display 'FD5D9BE0'
563             {
564                 BYTE bt =*(char*)(rxdata+k);    //string type
565                 strtemp.Format("%c",bt);        //write into strtemp
566                 m_Edit1+=strtemp;            //show on IDC_EDIT1
567             }
568             m_Edit1+="\r\n";
569             //=============== generate a new IDE code ====================
570             tmp_l =(unsigned long)u32_buff.bybuf[0]<<24;
571             tmp_l|=(unsigned long)u32_buff.bybuf[1]<<16;
572             tmp_l|=(unsigned long)u32_buff.bybuf[2]<<8;
573             tmp_l|=(unsigned long)u32_buff.bybuf[3];
574             
575             u32_buff.buf[2]=0;
576             u32_buff.buf[3]=0;
577             for(k=8;k<16;k++)
578                 u32_buff.bybuf[k]=u32_buff.bybuf[k-8]^u32_buff.bybuf[k-4];
579             u32_xxtea_SK.buf[0]=0x496D4C75;
580             u32_xxtea_SK.buf[1]=0x6D616F46;
581             u32_xxtea_SK.buf[2]=0x726F6D43;
582             u32_xxtea_SK.buf[3]=0x68696E61;
583             //----------------- encryption ---------------
584             AESFUN newAes = (AESFUN)::GetProcAddress(hModule,"AesEncrypt");
585             if(newAes==NULL)
586             {
587                 SetCurrentDirectory(chCurDir);
588                 ::FreeLibrary(hModule);
589                 AfxMessageBox("AES_DLL.dll is invalid.");
590                 return;
591             }
592             newAes(u32_xxtea_SK.bybuf,u32_buff.bybuf);    //create 12bytes fobid[0..11]
593             //--------------------------------------------
594             u32_buff.buf[0]=tmp_l;
595             for(k=0;k<4;k++)
596                 u32_buff.bybuf[15-k]=u32_buff.bybuf[4+k];
597             u32_buff.buf[1]=u32_buff.buf[3];
598             for(k=0;k<4;k++)
599                 u32_buff.bybuf[15-k]=u32_buff.bybuf[8+k];
600             u32_buff.buf[2]=u32_buff.buf[3];
601 
602             if(Id_invalid!=1)
603             {
604                 j=Lhex_to_hex(u32_buff.buf,rxdata,0);
605                 for(k=0;k<j;k++)
606                 {
607                     BYTE bt =*(char*)(rxdata+k);    //String type
608                     strtemp.Format("%c",bt);        //write into strtemp
609                     m_Edit2+=strtemp;                //show on IDC_EDIT2
610                 }
611                 j=Lhex_to_hex(u32_buff.buf,rxdata,1);
612                 for(k=0;k<j;k++)
613                 {
614                     BYTE bt =*(char*)(rxdata+k);    //String type
615                     strtemp.Format("%c",bt);    //write into strtemp
616                     m_Edit16+=strtemp;        //show on IDC_EDIT16
617                 }
618                 if(saveContext==1)
619                 {
620                                   m_Edit17++;
621                                         //year
622                                         strtemp.Format("%d",tp->tm_year+1900); 
623                                         m_Edit15+=strtemp;
624                                         //month
625                                         if((tp->tm_mon+1)<10)strtemp.Format("0%d",tp->tm_mon+1);
626                                         else strtemp.Format("%d",tp->tm_mon+1);
627                                         m_Edit15+=strtemp;
628                                         //day
629                                         if(tp->tm_mday<10)strtemp.Format("0%d-",tp->tm_mday);
630                                         else strtemp.Format("%d-",tp->tm_mday);
631                                         m_Edit15+=strtemp;
632                                         //hour
633                                         if(tp->tm_hour<10)strtemp.Format("0%d",tp->tm_hour);
634                                         else strtemp.Format("%d",tp->tm_hour);
635                                         m_Edit15+=strtemp;
636                                         //minute
637                                         if(tp->tm_min<10)strtemp.Format(":0%d:",tp->tm_min);
638                                         else strtemp.Format(":%d:",tp->tm_min);
639                                         m_Edit15+=strtemp;
640                                         //second
641                                         if(tp->tm_sec<10)strtemp.Format("0%d-Fobid:",tp->tm_sec);
642                                         else strtemp.Format("%d-Fobid:",tp->tm_sec);
643                                         m_Edit15+=strtemp;
644                                         
645                     j = Lhex_to_hex(u32_buff.buf,rxdata,3);
646                     for(k=0;k<j;k++)
647                     {
648                         BYTE bt = *(char*)(rxdata+k);
649                         strtemp.Format("%c",bt);
650                         m_Edit15+=strtemp;
651                     }
652                     C2HFUN c2hF=(C2HFUN)::GetProcAddress(hModule,"char_to_hex");
653                     if(c2hF==NULL)
654                     {
655                         SetCurrentDirectory(chCurDir);
656                         ::FreeLibrary(hModule);
657                         AfxMessageBox("AES_DLL.dll is invalid.");
658                         return;
659                     }
660                     if(!c2hF((char*)rxdata,fob_buf,24))
661                     {
662                         m_Edit12="Failed to creat secret keys.";
663                     }
664                     else
665                     {
666                         for(k=0;k<4;k++)
667                             fob_buf[12+k]=0;
668                         for(k=0,index=0;k<12;k++)
669                             key_buf[index++]=fob_buf[k]^0x38;
670                         
671                         newAes(dll_key_fob,fob_buf);//create 16byte fobkey
672                         peplen=0;
673                         rkelen=0;
674                         hModule = ::LoadLibrary("LaMotta FOB Signup_1.1_REL.dll");
675                         if(hModule==NULL)
676                         {
677                             SetCurrentDirectory(chCurDir);
678                             ::FreeLibrary(hModule);
679                             AfxMessageBox("Lost LaMotta FOB Signup_1.1_REL.dll.");
680                             return;
681                         }
682                         PFUN newfun = (PFUN)::GetProcAddress(hModule,"FobSignup");
683                         if(newfun==NULL)
684                         {
685                             SetCurrentDirectory(chCurDir);
686                             ::FreeLibrary(hModule);
687                             AfxMessageBox("LaMotta FOB Signup_1.1_REL.dll is invalid.");
688                             return;
689                         }
690                         j=newfun(16,16,16,fob_buf,&peplen,&rkelen,pep_buf,rke_buf);
691                         if(j==0)    //OK
692                         {
693                             hModule = ::LoadLibrary("AES_DLL.dll");
694                             //re-decrypt
695                             newAes = (AESFUN)::GetProcAddress(hModule,"ContraryAesEncrypt");
696                             if(newAes==NULL)
697                             {
698                                 SetCurrentDirectory(chCurDir);
699                                 ::FreeLibrary(hModule);
700                                 AfxMessageBox("AES_DLL.dll is invalid.");
701                                 return;
702                             }
703                             newAes(dll_key_PEP,pep_buf);    //BCM PEP
704                             newAes(dll_key_RKE,rke_buf);    //BCM RKE
705                             newAes(bcm_key_PEP,pep_buf);    //PEP
706                             newAes(bcm_key_RKE,rke_buf);    //RKE
707                             
708                             for(k=0;k<16;k++)
709                                 key_buf[index++]=pep_buf[k]^0x38;
710                             for(k=0;k<16;k++)
711                                 key_buf[index++]=rke_buf[k]^0x38;
712                             key_buf[index]=cal_crc(key_buf,index)^0x83;
713                             CString str(pep_buf);
714                             m_Edit15+=" rkes:";
715                             for(k=0;k<16;k++)
716                             {
717                                 str.Format("%X",rke_buf[k]);
718                                 if(strlen(str)==0)
719                                     m_Edit15+="00";
720                                 else if(strlen(str)==1)
721                                 {
722                                     m_Edit15+="0";
723                                     m_Edit15+=str;
724                                 }
725                                 else 
726                                     m_Edit15+=str;
727                             }
728                             m_Edit15+=" peps:";
729                             for(k=0;k<16;k++)
730                             {
731                                 str.Format("%X",pep_buf[k]);
732                                 if(strlen(str)==0)
733                                     m_Edit15+="00";
734                                 else if(strlen(str)==1)
735                                 {
736                                     m_Edit15+="0";
737                                     m_Edit15+=str;
738                                 }
739                                 else 
740                                     m_Edit15+=str;
741                             }
742                             m_Edit12="";
743                             for(k=0;k<index+1;k++)
744                             {
745                                 str.Format("%X",key_buf[k]);
746                                 if(strlen(str)==0)
747                                     m_Edit12+="00";
748                                 else if(strlen(str)==1)
749                                 {
750                                     m_Edit12+="0";
751                                     m_Edit12+=str;
752                                 }
753                                 else 
754                                     m_Edit12+=str;
755                             }
756                             pcom->SetOutput(COleVariant(m_Edit12));
757                         }
758                     }
759                                         m_Edit15 +="  \r\n";
760                 }
761             }
762             else
763             {
764                 m_Edit2+="Fob Invalid.\r\n";
765                 m_Edit16+="Fob Invalid.\r\n";
766             }
767           //To avoid refreshing the whole dialog, Using SetDlgItemText() instead of UpdateData(FALSE).
768           //SetDlgItemText(IDC_EDIT2,m_Edit2);
769           UpdateData(false);
770           CEdit *pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT1);
771           pEdit->LineScroll(pEdit->GetLineCount());
772           pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT2);
773           pEdit->LineScroll(pEdit->GetLineCount());
774           pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT16);
775           pEdit->LineScroll(pEdit->GetLineCount());
776           pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT15);
777           pEdit->LineScroll(pEdit->GetLineCount());
778           SetCurrentDirectory(chCurDir);
779           ::FreeLibrary(hModule);
780     }
781 }
782 
783 void CDemo1Dlg::OnButton3() 
784 {
785     // TODO: Add your control notification handler code here
786     CMSComm *pcom = (CMSComm *)this->GetDlgItem(IDC_MSCOMM1);
787     GetDlgItem(IDC_BUTTON3)->ShowWindow(SW_HIDE);
788     GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_SHOW);
789     if(pcom->GetPortOpen())        //
790     {
791         pcom->SetPortOpen(FALSE);    //close
792     }
793 }
794 
795 void CDemo1Dlg::OnButton4() //clear
796 {
797     // TODO: Add your control notification handler code here
798     m_Edit1="";
799     m_Edit2 = "";
800     m_Edit16 = "";
801     m_Edit12="";
802     m_Edit15="";
803     m_Edit18="";
804     for(int i=0;i<3;i++)
805         LastBuf[i]=0;
806     UpdateData(false);
807     CEdit *pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT15);
808     pEdit->LineScroll(pEdit->GetLineCount());
809 }
810 
811 //#define     backgound
812 BOOL CDemo1Dlg::OnEraseBkgnd(CDC* pDC) 
813 {
814 #ifdef backgound
815     // TODO: Add your message handler code here and/or call default
816     CBitmap   bitmap;
817     bitmap.LoadBitmap(IDB_BITMAP1);
818 
819     BITMAP   bmp;
820     bitmap.GetBitmap(&bmp);
821 
822     CDC   dcCompatible;
823     dcCompatible.CreateCompatibleDC(pDC);
824 
825     dcCompatible.SelectObject(&bitmap);
826 
827     CRect   rect;
828     //GetClientRect(&rect);
829     GetWindowRect(&rect);
830 
831     pDC-> StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,
832     0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
833     
834     //return CDialog::OnEraseBkgnd(pDC);
835 #endif
836     return CDialog::OnEraseBkgnd(pDC);
837 }
838 
839 HBRUSH CDemo1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
840 {
841     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
842     
843     //if (nCtlColor ==  CTLCOLOR_STATIC)
844     //{
845     //    pDC->SetBkMode(TRANSPARENT);//transparent
846     //    return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
847 //    }
848     // TODO: Change any attributes of the DC here
849     //pDC->SetBkMode(TRANSPARENT);
850     //pDC->SetTextColor(RGB(0,0,0));
851 
852     //return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
853     // TODO: Return a different brush if the default is not desired
854     return hbr;
855 }
856 
857 
858 #define IDC_HAND            MAKEINTRESOURCE(32649)
859 BOOL CDemo1Dlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
860 {
861     // TODO: Add your message handler code here and/or call default
862     
863     if(pWnd == GetDlgItem(IDC_COMBO1)||pWnd == GetDlgItem(IDC_BUTTON4)||pWnd == GetDlgItem(IDC_BUTTON1)||pWnd == GetDlgItem(IDC_BUTTON3))
864     {
865         SetCursor(LoadCursor(NULL,   IDC_HAND));
866         return true;
867     }
868     return CDialog::OnSetCursor(pWnd, nHitTest, message);
869 }
870 
871 void CDemo1Dlg::OnClose() 
872 {
873   // TODO: Add your message handler code here and/or call default    
874   CString str1,str2;
875   int rst;
876   int result=MessageBox("Are you want to save context before Exit ?","AlertDialog",MB_ICONINFORMATION|MB_YESNOCANCEL);
877   if(result==IDCANCEL)
878   {
879     SetCurrentDirectory("D:\\LamttaFobData");
880     return;
881   }
882   else if(result==IDYES)
883   {
884     CreateDirectory("D:\\LamttaFobData",NULL);
885     SetCurrentDirectory("D:\\LamttaFobData");
886     CTime tm = CTime::GetCurrentTime();
887     
888     str2.Format(_T("%d"),tm.GetYear());
889     str1=str2;
890     
891     if(tm.GetMonth()<10)
892       str2.Format(_T("0%d"),tm.GetMonth());
893     else
894       str2.Format(_T("%d"),tm.GetMonth());
895     str1+=str2;
896     
897     if(tm.GetDay()<10)
898       str2.Format(_T("0%d.log"),tm.GetDay());
899     else
900       str2.Format(_T("%d.log"),tm.GetDay());
901     str1+=str2;
902     CFile c_flie;
903     UpdateData(TRUE);
904     if(m_Edit15=="")
905     {
906       AfxMessageBox("No Context Save.");
907     }
908     else 
909     {
910         rst=c_flie.Open(str1,CFile::modeWrite);
911         if(!rst)//|CFile::modeCreate
912         {
913             rst=c_flie.Open(str1,CFile::modeWrite|CFile::modeCreate);
914         }
915         if(rst)
916         {
917             CString m_str;
918             CEdit *pEdit = (CEdit *)this->GetDlgItem(IDC_EDIT15);
919             pEdit->GetWindowText(m_str);
920             c_flie.SeekToEnd();
921             c_flie.Write(m_str,m_str.GetLength());
922             c_flie.Close();
923         }
924     }
925   }
926   CDialog::OnClose();
927 }

对话框设计UI:

谢谢。

posted on 2019-10-15 09:44  Milo_lu  阅读(1354)  评论(0编辑  收藏  举报

导航