ChgPasswdDlg.cpp
5.68 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
// ChgPasswdDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SN Writer.h"
#include "ChgPasswdDlg.h"
#include "des.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChgPasswdDlg dialog
CChgPasswdDlg::CChgPasswdDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChgPasswdDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChgPasswdDlg)
m_strOldPasswd = _T("");
m_strNewPasswd = _T("");
m_strConfirmPasswd = _T("");
//}}AFX_DATA_INIT
//memset(m_strNewPasswd, 0, sizeof(m_strNewPasswd));
}
void CChgPasswdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChgPasswdDlg)
DDX_Text(pDX, IDC_EDIT_OLD_PASSWD, m_strOldPasswd);
DDV_MaxChars(pDX, m_strOldPasswd, 64);
DDX_Text(pDX, IDC_EDIT_NEW_PASSWD, m_strNewPasswd);
DDV_MaxChars(pDX, m_strNewPasswd, 64);
DDX_Text(pDX, IDC_EDIT_CONFIRM_PASSWD, m_strConfirmPasswd);
DDV_MaxChars(pDX, m_strConfirmPasswd, 64);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChgPasswdDlg, CDialog)
//{{AFX_MSG_MAP(CChgPasswdDlg)
ON_BN_CLICKED(ID_CHG_PASSWD_CANCEL, OnChgPasswdCancel)
ON_BN_CLICKED(ID_CHG_PASSWD_OK, OnChgPasswdOk)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChgPasswdDlg message handlers
void CChgPasswdDlg::OnChgPasswdCancel()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
void CChgPasswdDlg::OnChgPasswdOk()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if(VerifyOldPasswd())
{
if(ConfirmNewPasswd())
{
LPTSTR lpPasswd =(LPTSTR)(LPCTSTR)m_strNewPasswd;
if(SaveLoginPasswd(lpPasswd))
{
MessageBox("Change passwd success!", "Change Passwd", MB_OK);
CDialog::OnOK();
}
else
{
MessageBox("Save new passwd cause fail, recover old passwd!", "Change Passwd", MB_OK);
lpPasswd =(LPTSTR)(LPCTSTR)m_strOldPasswd;
SaveLoginPasswd(lpPasswd);
CDialog::OnOK();
}
}
else
{
MessageBox("New passwd not the same with confirm passwd!!", "Warning", MB_ICONWARNING);
}
}
else
{
MessageBox("Incorrect old password, please try again!", "Warning", MB_ICONWARNING);
}
}
bool CChgPasswdDlg::VerifyOldPasswd()
{
UpdateData(TRUE);
BYTE key[10]="03055030";
BYTE strPassword[LOGIN_PASSWORD_MAX] = {0};
BYTE *pOldPasswd = strPassword;
Login_Identify_e Identify = UNKNOW_IDENTIFY;
if(ReadDataFromFile(strPassword, LOGIN_PASSWORD_MAX, key, g_pPasswdFilepath) == S_DES_SUCCESS)
{
CString tmpStr;
pOldPasswd += 2;
tmpStr.Format(_T("%s"), pOldPasswd);
if (tmpStr != m_strOldPasswd)
{
m_strOldPasswd = "";
UpdateData(FALSE);
MessageBox("Incorrect old password, please try again!", "Warning", MB_ICONWARNING);
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
bool CChgPasswdDlg::ConfirmNewPasswd()
{
if (m_strNewPasswd.GetLength() == 0 || m_strConfirmPasswd.GetLength() == 0)
{
return false;
}
if (m_strNewPasswd != m_strConfirmPasswd)
{
return false;
}
else
{
return true;
}
}
BOOL CChgPasswdDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if (pMsg->message == WM_KEYDOWN && pMsg->wParam ==VK_ESCAPE)
{
return true;
}
int currentID = ID_CHG_PASSWD_OK;
int nextID = ID_CHG_PASSWD_OK;
UpdateData(TRUE);
if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
{
currentID = GetFocus()->GetDlgCtrlID();
switch(currentID)
{
case IDC_EDIT_OLD_PASSWD:
if (VerifyOldPasswd())
{
nextID = IDC_EDIT_NEW_PASSWD;
}
else
{
nextID = IDC_EDIT_OLD_PASSWD;
}
break;
case IDC_EDIT_NEW_PASSWD:
nextID = IDC_EDIT_CONFIRM_PASSWD;
break;
case IDC_EDIT_CONFIRM_PASSWD:
if (ConfirmNewPasswd())
{
if (VerifyOldPasswd())
{
nextID = ID_CHG_PASSWD_OK;
}
else
{
nextID = IDC_EDIT_OLD_PASSWD;
SetDlgItemText(IDC_EDIT_OLD_PASSWD, "");
MessageBox("Incorrect old password, please try again!!", "Warning", MB_ICONWARNING);
}
}
else
{
nextID = IDC_EDIT_NEW_PASSWD;
SetDlgItemText(IDC_EDIT_NEW_PASSWD, "");
SetDlgItemText(IDC_EDIT_CONFIRM_PASSWD, "");
MessageBox("New passwd not the same with confirm passwd!!", "Warning", MB_ICONWARNING);
}
break;
}
}
if (nextID != ID_CHG_PASSWD_OK)
{
GotoDlgCtrl(GetDlgItem(nextID));
return TRUE;
}
else
{
return CDialog::PreTranslateMessage(pMsg);
}
}
BOOL CChgPasswdDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
GotoDlgCtrl(GetDlgItem(IDC_EDIT_OLD_PASSWD));
return FALSE;
//return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}