RandomKeyDlg.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // RandomKeyDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "RandomKey.h"
  5. #include "RandomKeyDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. // CRandomKeyDlg 对话框
  10. CRandomKeyDlg::CRandomKeyDlg(CWnd* pParent /*=NULL*/)
  11. : CDialog(CRandomKeyDlg::IDD, pParent)
  12. , edit1(_T(""))
  13. {
  14. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  15. }
  16. void CRandomKeyDlg::DoDataExchange(CDataExchange* pDX)
  17. {
  18. CDialog::DoDataExchange(pDX);
  19. DDX_Text(pDX, IDC_EDIT1, edit1);
  20. DDX_Control(pDX, IDOK, btn_ok);
  21. DDX_Control(pDX, IDCANCEL, btn_cancel);
  22. DDX_Control(pDX, IDC_STATIC1, static1);
  23. DDX_Control(pDX, IDC_EDIT1, editCtrl1);
  24. }
  25. BEGIN_MESSAGE_MAP(CRandomKeyDlg, CDialog)
  26. ON_WM_PAINT()
  27. ON_WM_QUERYDRAGICON()
  28. //}}AFX_MSG_MAP
  29. ON_BN_CLICKED(IDOK, &CRandomKeyDlg::OnBnClickedOk)
  30. ON_COMMAND(ID_CHANGEPASSWORD, &CRandomKeyDlg::OnChangepassword)
  31. ON_COMMAND(ID_EXIT, &CRandomKeyDlg::OnExit)
  32. ON_WM_DESTROY()
  33. ON_WM_TIMER()
  34. END_MESSAGE_MAP()
  35. // CRandomKeyDlg 消息处理程序
  36. BOOL CRandomKeyDlg::OnInitDialog()
  37. {
  38. CDialog::OnInitDialog();
  39. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  40. // 执行此操作
  41. SetIcon(m_hIcon, TRUE); // 设置大图标
  42. SetIcon(m_hIcon, FALSE); // 设置小图标
  43. // TODO: 在此添加额外的初始化代码
  44. // GetDlgItem(IDR_MENU1).ShowWindow(SW_HIDE);
  45. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  46. }
  47. // 如果向对话框添加最小化按钮,则需要下面的代码
  48. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  49. // 这将由框架自动完成。
  50. void CRandomKeyDlg::OnPaint()
  51. {
  52. if (IsIconic())
  53. {
  54. CPaintDC dc(this); // 用于绘制的设备上下文
  55. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  56. // 使图标在工作矩形中居中
  57. int cxIcon = GetSystemMetrics(SM_CXICON);
  58. int cyIcon = GetSystemMetrics(SM_CYICON);
  59. CRect rect;
  60. GetClientRect(&rect);
  61. int x = (rect.Width() - cxIcon + 1) / 2;
  62. int y = (rect.Height() - cyIcon + 1) / 2;
  63. // 绘制图标
  64. dc.DrawIcon(x, y, m_hIcon);
  65. }
  66. else
  67. {
  68. CDialog::OnPaint();
  69. }
  70. }
  71. //当用户拖动最小化窗口时系统调用此函数取得光标显示。
  72. //
  73. HCURSOR CRandomKeyDlg::OnQueryDragIcon()
  74. {
  75. return static_cast<HCURSOR>(m_hIcon);
  76. }
  77. void CRandomKeyDlg::OnBnClickedOk()
  78. {
  79. CString str;
  80. editCtrl1.GetWindowText(str);
  81. CString szText;
  82. CStdioFile sFile( "./code.txt", CStdioFile::modeRead);
  83. sFile.ReadString(szText);
  84. szText = Dectypt(szText);
  85. if (str!= "" && str == szText)
  86. {
  87. btn_ok.ShowWindow(SW_HIDE);
  88. btn_ok.EnableWindow(FALSE);
  89. static1.SetWindowText("随机密码:");
  90. editCtrl1.SetReadOnly(TRUE);
  91. editCtrl1.SetPasswordChar(0);
  92. btn_cancel.SetWindowText("退出");
  93. SetTimer(1,1000,0);
  94. menu = new CMenu;
  95. menu->LoadMenu(MAKEINTRESOURCE(IDR_MENU1));
  96. this->SetMenu(menu);
  97. btn_cancel.SetFocus();
  98. calc_code();
  99. }
  100. else
  101. {
  102. MessageBox(" 密码错误! ");
  103. editCtrl1.SetFocus();
  104. }
  105. }
  106. void CRandomKeyDlg::OnChangepassword()
  107. {
  108. CPassWord dlg;
  109. dlg.DoModal();
  110. }
  111. void CRandomKeyDlg::OnExit()
  112. {
  113. OnCancel();
  114. }
  115. void CRandomKeyDlg::OnDestroy()
  116. {
  117. delete menu;
  118. CDialog::OnDestroy();
  119. }
  120. void CRandomKeyDlg::OnTimer(UINT_PTR nIDEvent)
  121. {
  122. // TODO: 在此添加消息处理程序代码和/或调用默认值
  123. CDialog::OnTimer(nIDEvent);
  124. calc_code();
  125. }
  126. void CRandomKeyDlg::calc_code()
  127. {
  128. BYTE ar[10] = {0xBD,0x6E,0x3F,0x91,0x4C,0x2A};
  129. time_t curTime;
  130. time( &curTime );
  131. struct tm curLT = *localtime( &curTime );
  132. int year = curLT.tm_year+1900;
  133. int month = curLT.tm_mon+1;
  134. int day = curLT.tm_mday;
  135. CString strTime;
  136. 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 );
  137. ar[6] = year / 100 / 10 * 16 + year / 100 % 10;
  138. ar[7] = year % 100 / 10 * 16 + year % 100 % 10;
  139. ar[8] = month / 10 * 16 + month % 10;
  140. ar[9] = day / 10 * 16 + day % 10;
  141. BYTE CRC[2] = {0};
  142. CRC_CPCC(ar, 10, CRC);
  143. int c[4];
  144. c[0] = CRC[0] / 16;
  145. c[1] = CRC[0] %16;
  146. c[2] = CRC[1] / 16;
  147. c[3] = CRC[1] %16;
  148. int nTmp = day % 2;
  149. for (int i = 0; i < 4; i++)
  150. {
  151. if (c[i]>9)
  152. {
  153. if (nTmp == 0)
  154. {
  155. c[i] -= 6;
  156. }
  157. else
  158. {
  159. c[i] -= 9;
  160. }
  161. }
  162. }
  163. edit1.Format("%d%d%d%d",c[0],c[1],c[2],c[3]);
  164. UpdateData(FALSE);
  165. }
  166. void CRandomKeyDlg::CRC_CPCC(unsigned char *pucData,unsigned char nLen,unsigned char *pCRC)
  167. {
  168. unsigned char CRC16Lo,CRC16Hi,CH,CL,SaveHi,SaveLo,j;
  169. unsigned char i;
  170. CRC16Lo = 0;//*(pCRC+1);
  171. CRC16Hi = 0;//*pCRC;
  172. CH = 0XA0; CL = 0X01;
  173. for (i=0;i<nLen;i++)
  174. {
  175. CRC16Lo = CRC16Lo ^ (*(pucData+i));
  176. for (j=0;j<8;j++)
  177. {
  178. SaveHi = CRC16Hi;
  179. SaveLo = CRC16Lo;
  180. CRC16Hi = CRC16Hi >> 1;
  181. CRC16Lo = CRC16Lo >> 1;
  182. if ((SaveHi & 0x01 ) == 0x01)
  183. CRC16Lo = CRC16Lo | 0x80;
  184. if ((SaveLo & 0x01) == 0x01)
  185. {
  186. CRC16Hi = CRC16Hi ^ CH;
  187. CRC16Lo = CRC16Lo ^ CL;
  188. }
  189. }
  190. }
  191. *pCRC = CRC16Hi;pCRC++;
  192. *pCRC = CRC16Lo;
  193. }