123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- #include "stdafx.h"
- #include "RandomKey.h"
- #include "RandomKeyDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- CRandomKeyDlg::CRandomKeyDlg(CWnd* pParent )
- : CDialog(CRandomKeyDlg::IDD, pParent)
- , edit1(_T(""))
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CRandomKeyDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_EDIT1, edit1);
- DDX_Control(pDX, IDOK, btn_ok);
- DDX_Control(pDX, IDCANCEL, btn_cancel);
- DDX_Control(pDX, IDC_STATIC1, static1);
- DDX_Control(pDX, IDC_EDIT1, editCtrl1);
- }
- BEGIN_MESSAGE_MAP(CRandomKeyDlg, CDialog)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
-
- ON_BN_CLICKED(IDOK, &CRandomKeyDlg::OnBnClickedOk)
- ON_COMMAND(ID_CHANGEPASSWORD, &CRandomKeyDlg::OnChangepassword)
- ON_COMMAND(ID_EXIT, &CRandomKeyDlg::OnExit)
- ON_WM_DESTROY()
- ON_WM_TIMER()
- END_MESSAGE_MAP()
- BOOL CRandomKeyDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
-
- SetIcon(m_hIcon, TRUE);
- SetIcon(m_hIcon, FALSE);
-
- return TRUE;
- }
- void CRandomKeyDlg::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
- {
- CDialog::OnPaint();
- }
- }
- HCURSOR CRandomKeyDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CRandomKeyDlg::OnBnClickedOk()
- {
- CString str;
- editCtrl1.GetWindowText(str);
- CString szText;
- CStdioFile sFile( "./code.txt", CStdioFile::modeRead);
- sFile.ReadString(szText);
- szText = Dectypt(szText);
- if (str!= "" && str == szText)
- {
- btn_ok.ShowWindow(SW_HIDE);
- btn_ok.EnableWindow(FALSE);
- static1.SetWindowText("随机密码:");
- editCtrl1.SetReadOnly(TRUE);
- editCtrl1.SetPasswordChar(0);
- btn_cancel.SetWindowText("退出");
- SetTimer(1,1000,0);
- menu = new CMenu;
- menu->LoadMenu(MAKEINTRESOURCE(IDR_MENU1));
- this->SetMenu(menu);
- btn_cancel.SetFocus();
-
- calc_code();
- }
- else
- {
- MessageBox(" 密码错误! ");
- editCtrl1.SetFocus();
- }
- }
- void CRandomKeyDlg::OnChangepassword()
- {
- CPassWord dlg;
- dlg.DoModal();
- }
- void CRandomKeyDlg::OnExit()
- {
- OnCancel();
-
- }
- void CRandomKeyDlg::OnDestroy()
- {
- delete menu;
- CDialog::OnDestroy();
- }
- void CRandomKeyDlg::OnTimer(UINT_PTR nIDEvent)
- {
-
- CDialog::OnTimer(nIDEvent);
- calc_code();
- }
- void CRandomKeyDlg::calc_code()
- {
- BYTE ar[10] = {0xBD,0x6E,0x3F,0x91,0x4C,0x2A};
- time_t curTime;
- time( &curTime );
- struct tm curLT = *localtime( &curTime );
- int year = curLT.tm_year+1900;
- int month = curLT.tm_mon+1;
- int day = curLT.tm_mday;
- CString strTime;
- strTime.Format( "%d%02d%02d%02d%02d%02d", curLT.tm_year+1900, curLT.tm_mon+1, curLT.tm_mday, curLT.tm_hour, curLT.tm_min, curLT.tm_sec );
- ar[6] = year / 100 / 10 * 16 + year / 100 % 10;
- ar[7] = year % 100 / 10 * 16 + year % 100 % 10;
- ar[8] = month / 10 * 16 + month % 10;
- ar[9] = day / 10 * 16 + day % 10;
- BYTE CRC[2] = {0};
- CRC_CPCC(ar, 10, CRC);
- int c[4];
- c[0] = CRC[0] / 16;
- c[1] = CRC[0] %16;
- c[2] = CRC[1] / 16;
- c[3] = CRC[1] %16;
- int nTmp = day % 2;
- for (int i = 0; i < 4; i++)
- {
- if (c[i]>9)
- {
- if (nTmp == 0)
- {
- c[i] -= 6;
- }
- else
- {
- c[i] -= 9;
- }
- }
- }
- edit1.Format("%d%d%d%d",c[0],c[1],c[2],c[3]);
- UpdateData(FALSE);
- }
- void CRandomKeyDlg::CRC_CPCC(unsigned char *pucData,unsigned char nLen,unsigned char *pCRC)
- {
- unsigned char CRC16Lo,CRC16Hi,CH,CL,SaveHi,SaveLo,j;
- unsigned char i;
- CRC16Lo = 0;
- CRC16Hi = 0;
- CH = 0XA0; CL = 0X01;
- for (i=0;i<nLen;i++)
- {
- CRC16Lo = CRC16Lo ^ (*(pucData+i));
- for (j=0;j<8;j++)
- {
- SaveHi = CRC16Hi;
- SaveLo = CRC16Lo;
- CRC16Hi = CRC16Hi >> 1;
- CRC16Lo = CRC16Lo >> 1;
- if ((SaveHi & 0x01 ) == 0x01)
- CRC16Lo = CRC16Lo | 0x80;
- if ((SaveLo & 0x01) == 0x01)
- {
- CRC16Hi = CRC16Hi ^ CH;
- CRC16Lo = CRC16Lo ^ CL;
- }
- }
- }
- *pCRC = CRC16Hi;pCRC++;
- *pCRC = CRC16Lo;
- }
|