-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathSecureEdit.cpp
More file actions
executable file
·153 lines (121 loc) · 4.89 KB
/
Copy pathSecureEdit.cpp
File metadata and controls
executable file
·153 lines (121 loc) · 4.89 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
// 100% free Secure Edit control MFC class
// Copyright (c) 2003 Dominik Reichl
// If you use this class I would be more than happy if you mention
// my name somewhere in your application. Thanks!
// Do you have any questions or want to tell me that you are using
// my class, e-mail me: <[email protected]>.
#include "stdafx.h"
#include "SecureEdit.h"
#include "crypt/cryptdefs.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSecureEdit
CSecureEdit::CSecureEdit()
{
m_pBuf = new LockZeroBuffer<WCHAR>(MAX_PASSWORD_LEN + 1, true);
m_strRealText = m_pBuf->m_buf;
m_pBufOld = new LockZeroBuffer<WCHAR>(MAX_PASSWORD_LEN + 1, true);
m_strOldText = m_pBufOld->m_buf;
}
CSecureEdit::~CSecureEdit()
{
if (m_pBuf)
delete m_pBuf;
if (m_pBufOld)
delete m_pBufOld;
}
BEGIN_MESSAGE_MAP(CSecureEdit, CEdit)
//{{AFX_MSG_MAP(CSecureEdit)
ON_CONTROL_REFLECT(EN_UPDATE, OnUpdate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
void CSecureEdit::OnUpdate()
{
LockZeroBuffer<WCHAR> wndBuf(MAX_PASSWORD_LEN + 1, true);
WCHAR *strWnd = wndBuf.m_buf;
LPWSTR lpWnd = NULL;
int iWndLen = 0, iDiff = 0;
int i = 0;
int inxLeft = -1, inxRight = -1;
int nLeft = -1, nRight = -1;
DWORD dwPos = 0;
LockZeroBuffer<WCHAR> newBuf(MAX_PASSWORD_LEN + 1, true);
WCHAR* strNew = newBuf.m_buf;
BOOL bHasChanged = FALSE;
LockZeroBuffer<WCHAR> dotsBuf(MAX_PASSWORD_LEN + 1, true);
WCHAR * strDots = dotsBuf.m_buf;
// Get information about the new contents of the edit control
GetWindowText(strWnd, wndBuf.m_len-1); // The current window text (Windows has updated it already)
dwPos = GetSel() & 0xFFFF; // The current cursor position
iWndLen = lstrlen(strWnd); // The length of the hidden buffer
iDiff = iWndLen - m_nOldLen; // The difference between the new and the old
// Scan buffer for non-password-chars (fast scan, using LockBuffer)
lpWnd = strWnd;
for(i = 0; i < iWndLen; i++)
{
if(lpWnd[i] != SE_PASSWORD_CHAR) // This is a new character!
{
if(inxLeft == -1) inxLeft = i; // Only allow one change
bHasChanged = TRUE; // We have found a new character
}
// If we have found a new character and now find a password character
// again, this _must_ be the end of the new (clip-pasted?) string
if((lpWnd[i] == SE_PASSWORD_CHAR) && (bHasChanged == TRUE))
if(inxRight == -1) inxRight = i - 1; // Change only once
}
if(iDiff < 0) // User has deleted one or more characters
{
iDiff = -iDiff; // Make positive, so we can handle indexes
wcsncpy_s(strNew, MAX_PASSWORD_LEN+1, m_strRealText, dwPos);
wcsncat_s(strNew, MAX_PASSWORD_LEN+1, m_strRealText + wcslen(m_strRealText)-(m_nOldLen - dwPos - iDiff), _TRUNCATE);
wcscpy_s(m_strRealText, MAX_PASSWORD_LEN+1, strNew);
for (i = 0; i < lstrlen(m_strRealText); i++) strDots[i] = SE_PASSWORD_CHAR;
if(bHasChanged == FALSE) // If the encrypted buffer has not changed
{ // execute this code only if no new code has been pasted (clipboard)
wcscpy_s(m_strOldText, MAX_PASSWORD_LEN+1, strNew);
m_nOldLen = (int)wcslen(strDots);
SetWindowText(strDots);
SetSel(dwPos, dwPos, FALSE);
}
}
if(bHasChanged == FALSE) return; // Everything is encrypted/hidden
// Compute numbers of unchanged chars from left and right
if(inxRight == -1) inxRight = iWndLen - 1;
nLeft = inxLeft;
nRight = iWndLen - 1 - inxRight;
wcsncpy_s(strNew, MAX_PASSWORD_LEN+1, strWnd + inxLeft, inxRight - nLeft + 1);
// Insert to old string
wcsncpy_s(strWnd, MAX_PASSWORD_LEN+1, m_strRealText, nLeft);
wcsncat_s(strWnd, MAX_PASSWORD_LEN+1, strNew, _TRUNCATE);
wcsncat_s(strWnd, MAX_PASSWORD_LEN+1, m_strRealText + wcslen(m_strRealText)-nRight , _TRUNCATE);
wcscpy_s(m_strRealText, MAX_PASSWORD_LEN+1, strWnd);
wcscpy_s(m_strOldText, MAX_PASSWORD_LEN+1, strWnd); // Save the new secret text
for(i = 0; i < iWndLen; i++) strDots[i] = SE_PASSWORD_CHAR;
strDots[i] = '\0';
// Set the correct data again, just in case something went wrong with sim
m_nOldLen = iWndLen; // m_strOld is already set a few lines before
SetWindowText(strDots);
SetSel(dwPos, dwPos, FALSE); // Just to not confuse the user ;-)
}
void CSecureEdit::SetRealText(const WCHAR *pszNewString)
{
LockZeroBuffer<WCHAR> dotsBuf(MAX_PASSWORD_LEN+1, true);
WCHAR * strDots = dotsBuf.m_buf;
int i = 0;
if(pszNewString == NULL) return;
wcscpy_s(m_strRealText, MAX_PASSWORD_LEN+1, pszNewString);
wcscpy_s(m_strOldText, MAX_PASSWORD_LEN+1, pszNewString);
m_nOldLen = (int)wcslen(m_strRealText);
for(i = 0; i < m_nOldLen; i++) strDots[i] = SE_PASSWORD_CHAR;
strDots[i] = '\0';
SetWindowText(strDots);
if (wcslen(pszNewString) < 1) {
m_pBuf->Clear();
m_pBufOld->Clear();
}
}