md5实现对用户名与密码的保护
这里我们说一下关于如何运用md5算法实现对用户名与密码的保护。
我们都知道,有一些应用程序需要以用户名与密码登录应用程序时,在应用程序后台就需要一个保存用户名与密码的数据库。这类数据库有,数据库,文件,服务器云台等。
我们为了更好的让大家理解md5算法保护重要信息,所以我们这里选用文件来作为用户名与密码存储空间。
下面先设计UI,分别添加如下对话框:
- 主对话框:
- 登录对话框:
- 注册对话框:
- 用户信息存储文件名称: NamePassword.cfg
- 这打开应用程序时,主对话框首先判断是否需要创建存储文件:
CFile fileNamePwd; fileNamePwd.Open(_T("NamePassword.cfg"), CFile::modeRead); if ((int)fileNamePwd.m_hFile < 0) { fileNamePwd.Open(_T("NamePassword.cfg"), CFile::modeCreate); } fileNamePwd.Close(); CFobsignupDlg::OnOK(); LoginDlg loginDlg; loginDlg.DoModal();
如果文件不存在,则创建一个文件。
- 在登录对话框中添加登录事件:读取文件内容--md5计算输入内容,比较计算结果与文件内容,如果相同,则输入正确,否则失败:
void LoginDlg::BN_Clicked_Login() { // TODO: Add your control notification handler code here unsigned char verify[17] = { 0 }; unsigned char my_cfg[17] = { 0 }; unsigned char*pStr; unsigned int length; int i; CString str; UpdateData(true); if (m_Edit1_userName.IsEmpty()) { MessageBox(_T("Please enter your name.")); GetDlgItem(IDC_EDIT1)->SetFocus(); return; } if (m_Edit2_passwords.IsEmpty()) { MessageBox(_T("Please enter your password.")); GetDlgItem(IDC_EDIT2)->SetFocus(); return; } HMODULE hmd5; hmd5 = ::LoadLibrary(_T("md5_dll.dll")); if (hmd5) { MD5 md5_computeHash = (MD5)::GetProcAddress(hmd5, "md5_ComputeHash"); str = m_Edit1_userName + m_Edit2_passwords; pStr = (unsigned char*)(LPCTSTR)str; length = str.GetLength(); md5_computeHash(pStr, length, verify); } else { MessageBox(_T("Lost md5_dll.dll,Please Reinstall.")); return; } ::FreeLibrary(hmd5); CFile cfile; cfile.Open(_T("NamePassword.cfg"), CFile::modeRead); if ((int)cfile.m_hFile < 0) { MessageBox(_T("Lost configuration file,please Reinstall.")); return; } i = 1; while (cfile.Read(my_cfg, 16)) { if (!strcmp((char*)my_cfg, (char*)verify)) { i = 0; break; } } cfile.Close(); if (i == 1) { MessageBox(_T("user name or passwords are invalid.")); return; } if (g_bool_symbol == false) { g_bool_symbol = true; LoginDlg::OnOK(); CFobsignupDlg dlgApp; dlgApp.DoModal(); g_bool_symbol = false; } }
我们比较内容运用 strcmp ,所以数组申明一定要以NULL /0作为结束,否则该比较永远为真。
- 注册对话框添加提交事件:输入用户名、密码内容md5计算,计算结果写入文件:
void RegisterDlg::BN_Clicked_Submit() { // TODO: Add your control notification handler code here CFile cfile; unsigned char output[16], *pdata; CString str, str2, str3; GetDlgItem(IDC_EDIT1)->GetWindowText(str3); GetDlgItem(IDC_EDIT2)->GetWindowText(str2); if (str3.IsEmpty())return; if (str2.IsEmpty())return; cfile.Open(_T("NamePassword.cfg"), CFile::modeWrite); if ((int)cfile.m_hFile > 0) { //get dll file module HMODULE hml = ::LoadLibrary(_T("md5_dll.dll")); if (!hml)return; //get dll interface's name MD5 md5_computeHash = (MD5)::GetProcAddress(hml, "md5_ComputeHash"); //get all of the user's information str = str3 + str2; // a poniter to string pdata = (unsigned char*)(LPCTSTR)str; md5_computeHash(pdata, str.GetLength(), output); cfile.Write(output, 16); cfile.Close(); //free dll file module ::FreeLibrary(hml); str = _T("Regiter OK.\r\nName:") + str3 + _T("\r\nPasswords:") + str2; MessageBox(str); //switch to login dlg RegisterDlg::OnOK(); LoginDlg dlgApp; dlgApp.DoModal(); } else { MessageBox(_T("Lost configuration file.please reinstall.")); } }
文件写入后,直接跳转到登录对话框。
- 源码:
// FobsignupDlg.cpp : implementation file // #include "stdafx.h" #include "Fobsignup.h" #include "FobsignupDlg.h" #include "LoginDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif typedef int(*PFUN)(const unsigned int fobIdLen , const unsigned int maxPepArrayLen , const unsigned int maxRkeArrayLen , const unsigned char *encryptedFobId , unsigned int *actualPepArrayLen , unsigned int *actualRkeArrayLen , unsigned char *dtEncryptedPepArray , unsigned char *dtEncryptedRkeArray); typedef void(*AESFUN)(unsigned char *pskey, unsigned char *blk); //e.g, input="123456"--(length=6)-->output={0x12,0x34,0x56} typedef int(*C2HFUN)(char*input, unsigned char *output, int length); unsigned char dll_key_fob[16] = { 0x56, 0x38, 0x97, 0x3f, 0x56, 0x70, 0x22, 0x88, 0x3b, 0x52, 0xfe, 0x5a, 0x01, 0x32, 0x08, 0x49 }; unsigned char dll_key_PEP[16] = { 0x13, 0xff, 0x13, 0x84, 0x43, 0x86, 0x43, 0x4a, 0x23, 0x53, 0xe6, 0x54, 0xd3, 0x02, 0x00, 0x34 }; unsigned char dll_key_RKE[16] = { 0x15, 0x42, 0x71, 0x73, 0x6a, 0x41, 0xe3, 0x45, 0xde, 0x40, 0xba, 0x34, 0x42, 0x82, 0xaa, 0xe2 }; unsigned char bcm_key_PEP[16] = { 0x18, 0x92, 0x14, 0x81, 0x89, 0xf2, 0x4f, 0xa4, 0x92, 0x52, 0xfe, 0x5a, 0x4e, 0x32, 0xcc, 0x3d }; unsigned char bcm_key_RKE[16] = { 0x12, 0x52, 0x63, 0x54, 0x25, 0x4b, 0xc3, 0x4b, 0x23, 0x00, 0xdd, 0xbb, 0xc3, 0x82, 0xfd, 0xf4 }; //CRC-8 for ATM HEC (x8+x2+x+1) unsigned char cal_crc(unsigned char *vptr, unsigned char len) { const unsigned char *data = vptr; unsigned int crc = 0; int i, j; for (j = len; j; j--, data++) { crc ^= (*data << 8); for (i = 8; i; i--) { if (crc & 0x8000) crc ^= (unsigned int)(0x1070 << 3); crc <<= 1; } } return (unsigned char)(crc >> 8); } BOOL g_bool_symbol = false; // CAboutDlg dialog used for App About class CAboutDlg : public CDialogEx { public: CAboutDlg(); // Dialog Data enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CFobsignupDlg dialog CFobsignupDlg::CFobsignupDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CFobsignupDlg::IDD, pParent) , m_Edit1(_T("")) , m_Edit2(_T("")) , m_Edit3(_T("")) , m_Edit4(_T("")) , m_Edit5(_T("")) , m_Edit6(_T("")) , m_Edit7(_T("")) , m_Edit8(_T("")) , m_Edit9(_T("")) { m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1); //IDR_MAINFRAME } void CFobsignupDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, m_Edit1); DDX_Text(pDX, IDC_EDIT2, m_Edit2); DDX_Text(pDX, IDC_EDIT3, m_Edit3); DDX_Text(pDX, IDC_EDIT4, m_Edit4); DDX_Text(pDX, IDC_EDIT5, m_Edit5); DDX_Text(pDX, IDC_EDIT6, m_Edit6); DDX_Text(pDX, IDC_EDIT7, m_Edit7); DDX_Text(pDX, IDC_EDIT8, m_Edit8); DDX_Text(pDX, IDC_EDIT9, m_Edit9); // DDX_Control(pDX, IDC_MSCOMM1, m_mscom); } BEGIN_MESSAGE_MAP(CFobsignupDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, &CFobsignupDlg::create_BN_CLICKED) ON_BN_CLICKED(IDC_BUTTON2, &CFobsignupDlg::Clear_BN_CLICKED) // ON_BN_CLICKED(IDC_BUTTON3, &CFobsignupDlg::OnBnClickedButton3) ON_BN_CLICKED(IDC_BUTTON3, &CFobsignupDlg::write_BN_CLICKED) ON_BN_CLICKED(IDC_BUTTON4, &CFobsignupDlg::Open_Close_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK1, &CFobsignupDlg::checkBox1_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK2, &CFobsignupDlg::checkBox2_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK3, &CFobsignupDlg::checkBox3_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK4, &CFobsignupDlg::checkBox4_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK5, &CFobsignupDlg::checkBox5_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK6, &CFobsignupDlg::checkBox6_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK7, &CFobsignupDlg::checkBox7_BN_CLICKED) ON_BN_CLICKED(IDC_CHECK8, &CFobsignupDlg::checkBox8_BN_CLICKED) ON_BN_CLICKED(IDC_BUTTON5, &CFobsignupDlg::BN_Clicked_Logout) END_MESSAGE_MAP() // CFobsignupDlg message handlers BOOL CFobsignupDlg::PreTranslateMessage(MSG *pMsg) { if (pMsg->message == WM_KEYDOWN) { switch (pMsg->wParam) { case VK_ESCAPE: { return true; break; } case VK_RETURN: { return true; break; } default: break; } } return CDialog::PreTranslateMessage(pMsg); } BOOL CFobsignupDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here if (g_bool_symbol == false) { CFile fileNamePwd; fileNamePwd.Open(_T("NamePassword.cfg"), CFile::modeRead); if ((int)fileNamePwd.m_hFile < 0) { fileNamePwd.Open(_T("NamePassword.cfg"), CFile::modeCreate); } fileNamePwd.Close(); CFobsignupDlg::OnOK(); LoginDlg loginDlg; loginDlg.DoModal(); } else { CEdit *pEdit = (CEdit*)(this)->GetDlgItem(IDC_EDIT1); pEdit->SetLimitText(24); CComboBox* pct = (CComboBox *)this->GetDlgItem(IDC_COMBO1); CComboBox* pport = (CComboBox *)this->GetDlgItem(IDC_COMBO2); CComboBox* pbaud = (CComboBox *)this->GetDlgItem(IDC_COMBO3); CButton *pbtn = (CButton*)this->GetDlgItem(IDC_BUTTON3); pct->SetCurSel(0); pport->SetCurSel(1); pbaud->SetCurSel(2); pbtn->EnableWindow(FALSE); } return TRUE; // return TRUE unless you set the focus to a control } void CFobsignupDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CFobsignupDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CFobsignupDlg::OnQueryDragIcon() { return static_cast<HCURSOR>(m_hIcon); } void CFobsignupDlg::create_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[50]; unsigned char chr_buf[12]; unsigned char fob_buf[16]; unsigned char pep_buf[16]; unsigned char rke_buf[16]; unsigned char message_buf[49]; //unsigned char key_buf[16]; int i, k, result; unsigned int peplen = 0, rkelen = 0; unsigned int index; CString str; CString str_show(fob_buf); CButton*pbutton; TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT1)->GetWindowText(str); strcpy(buf, str); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 24)) { AfxMessageBox(_T("data invalid.")); } else { fob_buf[12] = 0; fob_buf[13] = 0; fob_buf[14] = 0; fob_buf[15] = 0; m_Edit1 = ""; for (i = 0,index=0; i < 12; i++) { if (IsDlgButtonChecked(IDC_CHECK1) == BST_CHECKED) { message_buf[index++] = fob_buf[11-i] ^ 0x38; str_show.Format("%X", fob_buf[11-i]); } else { message_buf[index++] = fob_buf[i] ^ 0x38; str_show.Format("%X", fob_buf[i]); } chr_buf[i] = fob_buf[i]; k = strlen(str_show); if (k == 0) { m_Edit1 = "00"; } else if (k == 1) { m_Edit1 += "0"; m_Edit1 += str_show; } else { m_Edit1 += str_show; } } if (IsDlgButtonChecked(IDC_CHECK1) == BST_CHECKED) { for (i = 0; i < 12; i++) { fob_buf[i] = chr_buf[11 - i]; } pbutton = (CButton*)GetDlgItem(IDC_CHECK1); pbutton->SetCheck(false); } if (IsDlgButtonChecked(IDC_CHECK2) == BST_CHECKED) { pbutton = (CButton*)GetDlgItem(IDC_CHECK2); pbutton->SetCheck(false); } if (IsDlgButtonChecked(IDC_CHECK3) == BST_CHECKED) { pbutton = (CButton*)GetDlgItem(IDC_CHECK3); pbutton->SetCheck(false); } if (IsDlgButtonChecked(IDC_CHECK4) == BST_CHECKED) { pbutton = (CButton*)GetDlgItem(IDC_CHECK4); pbutton->SetCheck(false); } if (IsDlgButtonChecked(IDC_CHECK5) == BST_CHECKED) { pbutton = (CButton*)GetDlgItem(IDC_CHECK5); pbutton->SetCheck(false); } if (IsDlgButtonChecked(IDC_CHECK6) == BST_CHECKED) { pbutton = (CButton*)GetDlgItem(IDC_CHECK6); pbutton->SetCheck(false); } if (IsDlgButtonChecked(IDC_CHECK7) == BST_CHECKED) { pbutton = (CButton*)GetDlgItem(IDC_CHECK7); pbutton->SetCheck(false); } if (IsDlgButtonChecked(IDC_CHECK8) == BST_CHECKED) { pbutton = (CButton*)GetDlgItem(IDC_CHECK8); pbutton->SetCheck(false); } AESFUN m_ase = (AESFUN)::GetProcAddress(hModule, "AesEncrypt"); m_ase(dll_key_fob, fob_buf); m_Edit2 = m_Edit3 = m_Edit4 = m_Edit5 = m_Edit6 = m_Edit7 = m_Edit8 = m_Edit9 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit2 = "00"; } else if (k == 1) { m_Edit2 += "0"; m_Edit2 += str_show; } else { m_Edit2 += str_show; } } hModule = ::LoadLibrary(_T("LaMotta FOB Signup_1.1_REL.dll")); PFUN m_dll = (PFUN)::GetProcAddress(hModule, "FobSignup"); result = m_dll(16, 16, 16, fob_buf, &peplen, &rkelen, pep_buf, rke_buf); if (result == 0) { for (i = 0; i < 16; i++) { str_show.Format("%X", pep_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit3 = "00"; } else if (k == 1) { m_Edit3 += "0"; m_Edit3 += str_show; } else { m_Edit3 += str_show; } str_show.Format("%X", rke_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit4 = "00"; } else if (k == 1) { m_Edit4 += "0"; m_Edit4 += str_show; } else { m_Edit4 += str_show; } } hModule = ::LoadLibrary(_T("AES_DLL.dll")); m_ase = (AESFUN)::GetProcAddress(hModule, "ContraryAesEncrypt"); m_ase(dll_key_PEP, pep_buf); m_ase(dll_key_RKE, rke_buf); for (i = 0; i < 16; i++) { str_show.Format("%X", pep_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit5 = "00"; } else if (k == 1) { m_Edit5 += "0"; m_Edit5 += str_show; } else { m_Edit5 += str_show; } str_show.Format("%X", rke_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit6 = "00"; } else if (k == 1) { m_Edit6 += "0"; m_Edit6 += str_show; } else { m_Edit6 += str_show; } } m_ase(bcm_key_PEP, pep_buf); m_ase(bcm_key_RKE, rke_buf); for (i = 0; i < 16; i++) { message_buf[index++] = pep_buf[i] ^ 0x38; str_show.Format("%X", pep_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit7 = "00"; } else if (k == 1) { m_Edit7 += "0"; m_Edit7 += str_show; } else { m_Edit7 += str_show; } message_buf[index + 15] = rke_buf[i] ^ 0x38; str_show.Format("%X", rke_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit8 = "00"; } else if (k == 1) { m_Edit8 += "0"; m_Edit8 += str_show; } else { m_Edit8 += str_show; } } index += 16; message_buf[index] = cal_crc(message_buf, index) ^ 0x83; for (i = 0; i < int(index + 1); i++) { str_show.Format("%X", message_buf[i]); k = strlen(str_show); if (k == 0) { m_Edit9 = "00"; } else if (k == 1) { m_Edit9 += "0"; m_Edit9 += str_show; } else { m_Edit9 += str_show; } } CComboBox* pct = (CComboBox *)this->GetDlgItem(IDC_COMBO1); if (pct->GetCurSel() == 2) { m_Edit9 += "315MHz"; } else if (pct->GetCurSel() == 1) { m_Edit9 += "433MHz"; } else if (pct->GetCurSel() == 0) { m_Edit9 += "Scaner"; } else { m_Edit9 += "Scaner"; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::Clear_BN_CLICKED() { // TODO: Add your control notification handler code here DWORD DADA = 5631; CString str; m_Edit2 = m_Edit3 = m_Edit4 = m_Edit5 = m_Edit6 = m_Edit7 = m_Edit8 = m_Edit9 = ""; UpdateData(false); } BEGIN_EVENTSINK_MAP(CFobsignupDlg, CDialogEx) ON_EVENT(CFobsignupDlg, IDC_MSCOMM1, 1, CFobsignupDlg::OnCommMscomm1, VTS_NONE) END_EVENTSINK_MAP() void CFobsignupDlg::OnCommMscomm1() { // TODO: Add your message handler code here long len, i; BYTE rxdata[64]; //rx buffer VARIANT variant_inp; COleSafeArray safearray_inp; CButton*pt = (CButton*)GetDlgItem(IDC_CHECK1); CMscomm1 *pcom = (CMscomm1*)this->GetDlgItem(IDC_MSCOMM1); if (pcom->get_CommEvent() == 2) { variant_inp = pcom->get_Input(); //read rx buffer safearray_inp = variant_inp; //VARIANT to COleSafeArray calss len = safearray_inp.GetOneDimSize(); //get the active length of data byte if (len != 30) return; if (IsDlgButtonChecked(IDC_CHECK1) == BST_CHECKED) { pt->SetCheck(false); } m_Edit1 = ""; for (i = 0; i<(len - 2); i++) //ignore '\r\n' symbol { safearray_inp.GetElement(&i, rxdata + i); if ((i+1)%5 == 0); //ignore space symbols else { m_Edit1 += rxdata[i]; } } UpdateData(false); } } void CFobsignupDlg::write_BN_CLICKED() { // TODO: Add your control notification handler code here CMscomm1 *pcomm = (CMscomm1 *)this->GetDlgItem(IDC_MSCOMM1); if (pcomm->get_PortOpen()==false) { MessageBox(_T("Serial Port DID NOT Open.")); } else if (m_Edit9 == "") { MessageBox(_T("No message Write.")); } else { pcomm->put_Output(COleVariant(m_Edit9)); } } void CFobsignupDlg::Open_Close_BN_CLICKED() { // TODO: Add your control notification handler code here CMscomm1 *pcm = (CMscomm1 *)this ->GetDlgItem(IDC_MSCOMM1); CComboBox* pFreq = (CComboBox *)this->GetDlgItem(IDC_COMBO1); CComboBox* pport = (CComboBox *)this->GetDlgItem(IDC_COMBO2); CComboBox* pbaud = (CComboBox *)this->GetDlgItem(IDC_COMBO3); CButton *pBtn = (CButton*)this->GetDlgItem(IDC_BUTTON3); if (pcm->get_PortOpen() == false) { pcm->put_CommPort(pport->GetCurSel()); if (pbaud->GetCurSel() == 0) { pcm->put_Settings(_T("2400,n,8,1")); } else if (pbaud->GetCurSel() == 1) { pcm->put_Settings(_T("4800,n,8,1")); } else if (pbaud->GetCurSel() == 2) { pcm->put_Settings(_T("9600,n,8,1")); } else if (pbaud->GetCurSel() == 3) { pcm->put_Settings(_T("38400,n,8,1")); } else if (pbaud->GetCurSel() == 4) { pcm->put_Settings(_T("115200,n,8,1")); } else { pcm->put_Settings(_T("9600,n,8,1")); } pcm->put_InputMode(1); //1: read data by binary checking pcm->put_RThreshold(30); //onComm event trigger when N byte in input buffer pcm->put_InputLen(0); //set input length 0. pcm->put_PortOpen(true); } else { pcm->put_PortOpen(false); } if (pcm->get_PortOpen() == false) { SetDlgItemText(IDC_BUTTON4, _T("Open")); pport->EnableWindow(true); pbaud->EnableWindow(true); pFreq->EnableWindow(true); pBtn->EnableWindow(false); } else { SetDlgItemText(IDC_BUTTON4, _T("Close")); pport->EnableWindow(false); pbaud->EnableWindow(false); pFreq->EnableWindow(false); pBtn->EnableWindow(true); } } void CFobsignupDlg::checkBox1_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[50]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK1); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT1)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK1) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 24)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { fob_buf[12] = 0; fob_buf[13] = 0; fob_buf[14] = 0; fob_buf[15] = 0; m_Edit1 = ""; for (i = 0; i < 12; i++) { str_show.Format("%X", fob_buf[11 - i]); k = strlen(str_show); if (k == 0) { m_Edit1 = "00"; } else if (k == 1) { m_Edit1 += "0"; m_Edit1 += str_show; } else { m_Edit1 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::checkBox2_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[96]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK2); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT2)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK2) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 32)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { m_Edit2 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[15 - i]); k = strlen(str_show); if (k == 0) { m_Edit2 = "00"; } else if (k == 1) { m_Edit2 += "0"; m_Edit2 += str_show; } else { m_Edit2 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::checkBox3_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[96]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK3); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT3)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK3) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 32)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { m_Edit3 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[15 - i]); k = strlen(str_show); if (k == 0) { m_Edit3 = "00"; } else if (k == 1) { m_Edit3 += "0"; m_Edit3 += str_show; } else { m_Edit3 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::checkBox4_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[96]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK4); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT4)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK4) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 32)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { m_Edit4 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[15 - i]); k = strlen(str_show); if (k == 0) { m_Edit4 = "00"; } else if (k == 1) { m_Edit4 += "0"; m_Edit4 += str_show; } else { m_Edit4 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::checkBox5_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[96]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK5); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT5)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK5) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 32)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { m_Edit5 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[15 - i]); k = strlen(str_show); if (k == 0) { m_Edit5 = "00"; } else if (k == 1) { m_Edit5 += "0"; m_Edit5 += str_show; } else { m_Edit5 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::checkBox6_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[96]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK6); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT6)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK6) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 32)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { m_Edit6 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[15 - i]); k = strlen(str_show); if (k == 0) { m_Edit6 = "00"; } else if (k == 1) { m_Edit6 += "0"; m_Edit6 += str_show; } else { m_Edit6 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::checkBox7_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[96]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK7); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT7)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK7) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 32)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { m_Edit7 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[15 - i]); k = strlen(str_show); if (k == 0) { m_Edit7 = "00"; } else if (k == 1) { m_Edit7 += "0"; m_Edit7 += str_show; } else { m_Edit7 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::checkBox8_BN_CLICKED() { // TODO: Add your control notification handler code here char buf[96]; int i, k; unsigned char fob_buf[16]; CString str; CString str_show(fob_buf); CButton*pt = (CButton*)GetDlgItem(IDC_CHECK8); //get context from edit1 UpdateData(true); GetDlgItem(IDC_EDIT8)->GetWindowText(str); if (IsDlgButtonChecked(IDC_CHECK8) == BST_CHECKED) { if (str.GetLength() == 0) { pt->SetCheck(false); AfxMessageBox(_T("zero length")); return; } } else if (str.GetLength() == 0) { return; } strcpy(buf, str); TCHAR chCurDir[MAX_PATH] = { 0 }; GetCurrentDirectory(MAX_PATH, chCurDir); SetCurrentDirectory(_T("C:\\Windows")); HMODULE hModule = ::LoadLibrary(_T("AES_DLL.dll")); C2HFUN ToChar = (C2HFUN)::GetProcAddress(hModule, "char_to_hex"); if (!ToChar(buf, fob_buf, 32)) { pt->SetCheck(false); AfxMessageBox(_T("data invalid.")); } else { m_Edit8 = ""; for (i = 0; i < 16; i++) { str_show.Format("%X", fob_buf[15 - i]); k = strlen(str_show); if (k == 0) { m_Edit8 = "00"; } else if (k == 1) { m_Edit8 += "0"; m_Edit8 += str_show; } else { m_Edit8 += str_show; } } } UpdateData(false); SetCurrentDirectory(chCurDir); ::FreeLibrary(hModule); } void CFobsignupDlg::BN_Clicked_Logout() { // TODO: Add your control notification handler code here g_bool_symbol = false; CFobsignupDlg::OnOK(); LoginDlg dlgApp; dlgApp.DoModal(); }
// LoginDlg.cpp : implementation file // #include "stdafx.h" #include "Fobsignup.h" #include "LoginDlg.h" #include "RegisterDlg.h" #include "FobsignupDlg.h" #include "afxdialogex.h" extern BOOL g_bool_symbol; // LoginDlg dialog typedef void (*MD5)(unsigned char *pInputBuf, unsigned int inputLength, unsigned char *pOutputBuf); IMPLEMENT_DYNAMIC(LoginDlg, CDialogEx) LoginDlg::LoginDlg(CWnd* pParent /*=NULL*/) : CDialogEx(LoginDlg::IDD, pParent) , m_Edit1_userName(_T("")) , m_Edit2_passwords(_T("")) { } LoginDlg::~LoginDlg() { } void LoginDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, m_Edit1_userName); DDX_Text(pDX, IDC_EDIT2, m_Edit2_passwords); } BEGIN_MESSAGE_MAP(LoginDlg, CDialogEx) ON_BN_CLICKED(IDC_BUTTON1, &LoginDlg::BN_Clicked_Login) ON_BN_CLICKED(IDC_BUTTON2, &LoginDlg::BN_Clicked_Exit) ON_BN_CLICKED(IDC_CHECK1, &LoginDlg::BN_Clicked_showPwd) ON_BN_CLICKED(IDC_BUTTON3, &LoginDlg::BN_Clicked_Register) END_MESSAGE_MAP() // LoginDlg message handlers BOOL LoginDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_KEYDOWN) { switch (pMsg->wParam) { case VK_ESCAPE: { return true; break; } case VK_RETURN: { return true; break; } default: break; } } return CDialog::PreTranslateMessage(pMsg); } BOOL LoginDlg::OnInitDialog() { CDialogEx::OnInitDialog(); CEdit *pEdit = (CEdit*)(this)->GetDlgItem(IDC_EDIT2); pEdit->SetLimitText(16); pEdit->SetPasswordChar('*'); UpdateData(FALSE); return false; } void LoginDlg::BN_Clicked_Login() { // TODO: Add your control notification handler code here unsigned char verify[17] = { 0 }; unsigned char my_cfg[17] = { 0 }; unsigned char*pStr; unsigned int length; int i; CString str; UpdateData(true); if (m_Edit1_userName.IsEmpty()) { MessageBox(_T("Please enter your name.")); GetDlgItem(IDC_EDIT1)->SetFocus(); return; } if (m_Edit2_passwords.IsEmpty()) { MessageBox(_T("Please enter your password.")); GetDlgItem(IDC_EDIT2)->SetFocus(); return; } HMODULE hmd5; hmd5 = ::LoadLibrary(_T("md5_dll.dll")); if (hmd5) { MD5 md5_computeHash = (MD5)::GetProcAddress(hmd5, "md5_ComputeHash"); str = m_Edit1_userName + m_Edit2_passwords; pStr = (unsigned char*)(LPCTSTR)str; length = str.GetLength(); md5_computeHash(pStr, length, verify); } else { MessageBox(_T("Lost md5_dll.dll,Please Reinstall.")); return; } ::FreeLibrary(hmd5); CFile cfile; cfile.Open(_T("NamePassword.cfg"), CFile::modeRead); if ((int)cfile.m_hFile < 0) { MessageBox(_T("Lost configuration file,please Reinstall.")); return; } i = 1; while (cfile.Read(my_cfg, 16)) { if (!strcmp((char*)my_cfg, (char*)verify)) { i = 0; break; } } cfile.Close(); if (i == 1) { MessageBox(_T("user name or passwords are invalid.")); return; } if (g_bool_symbol == false) { g_bool_symbol = true; LoginDlg::OnOK(); CFobsignupDlg dlgApp; dlgApp.DoModal(); g_bool_symbol = false; } } void LoginDlg::BN_Clicked_Exit() { // TODO: Add your control notification handler code here LoginDlg::OnOK(); } void LoginDlg::BN_Clicked_showPwd() { // TODO: Add your control notification handler code here if (IsDlgButtonChecked(IDC_CHECK1) == BST_CHECKED) { UpdateData(true); CEdit *pEdit = (CEdit*)(this)->GetDlgItem(IDC_EDIT2); pEdit->SetPasswordChar(0); SetDlgItemText(IDC_EDIT2, m_Edit2_passwords); UpdateData(false); } else { UpdateData(true); CEdit *pEdit = (CEdit*)(this)->GetDlgItem(IDC_EDIT2); pEdit->SetPasswordChar('*'); SetDlgItemText(IDC_EDIT2, m_Edit2_passwords); UpdateData(false); } GetDlgItem(IDC_EDIT2)->SetFocus(); } void LoginDlg::BN_Clicked_Register() { // TODO: Add your control notification handler code here LoginDlg::OnOK(); RegisterDlg dlgApp; dlgApp.DoModal(); }
// RegisterDlg.cpp : implementation file // #include "stdafx.h" #include "Fobsignup.h" #include "LoginDlg.h" #include "RegisterDlg.h" #include "afxdialogex.h" typedef void(*MD5)(unsigned char *pInputBuf, unsigned int inputLength, unsigned char *pOutputBuf); // RegisterDlg dialog IMPLEMENT_DYNAMIC(RegisterDlg, CDialogEx) RegisterDlg::RegisterDlg(CWnd* pParent /*=NULL*/) : CDialogEx(RegisterDlg::IDD, pParent) { } RegisterDlg::~RegisterDlg() { } void RegisterDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(RegisterDlg, CDialogEx) ON_BN_CLICKED(IDC_BUTTON2, &RegisterDlg::BN_Clicked_Return) ON_BN_CLICKED(IDC_BUTTON1, &RegisterDlg::BN_Clicked_Submit) END_MESSAGE_MAP() // RegisterDlg message handlers void RegisterDlg::BN_Clicked_Return() { // TODO: Add your control notification handler code here RegisterDlg::OnOK(); LoginDlg dlgApp; dlgApp.DoModal(); } void RegisterDlg::BN_Clicked_Submit() { // TODO: Add your control notification handler code here CFile cfile; unsigned char output[16], *pdata; CString str, str2, str3; GetDlgItem(IDC_EDIT1)->GetWindowText(str3); GetDlgItem(IDC_EDIT2)->GetWindowText(str2); if (str3.IsEmpty())return; if (str2.IsEmpty())return; cfile.Open(_T("NamePassword.cfg"), CFile::modeWrite); if ((int)cfile.m_hFile > 0) { //get dll file module HMODULE hml = ::LoadLibrary(_T("md5_dll.dll")); if (!hml)return; //get dll interface's name MD5 md5_computeHash = (MD5)::GetProcAddress(hml, "md5_ComputeHash"); //get all of the user's information str = str3 + str2; // a poniter to string pdata = (unsigned char*)(LPCTSTR)str; md5_computeHash(pdata, str.GetLength(), output); cfile.Write(output, 16); cfile.Close(); //free dll file module ::FreeLibrary(hml); str = _T("Regiter OK.\r\nName:") + str3 + _T("\r\nPasswords:") + str2; MessageBox(str); //switch to login dlg RegisterDlg::OnOK(); LoginDlg dlgApp; dlgApp.DoModal(); } else { MessageBox(_T("Lost configuration file.please reinstall.")); } }
- End. 谢谢.