-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPluginDefinition.cpp
executable file
·437 lines (367 loc) · 14.5 KB
/
PluginDefinition.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//this file is part of notepad++
//Copyright (C)2003 Don HO <[email protected]>
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "PluginDefinition.h"
#include "menuCmdID.h"
//
// Globals
//
HINSTANCE hInstance = NULL;
unsigned char cryptkey[MAX_CRYPT_KEY];
//
// Fwd decl
//
void EncryptDoc() { CryptDoc(CryptAction::Encrypt); }
void DecryptDoc() { CryptDoc(CryptAction::Decrypt); }
void EncryptSelection() { CryptSelection(CryptAction::Encrypt); }
void DecryptSelection() { CryptSelection(CryptAction::Decrypt); }
LRESULT CALLBACK DlgProcCryptKey(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
void StrCrypt(unsigned char *buf1, size_t buf1len, unsigned char *buf2, size_t buf2len, unsigned char *key, CryptAction action);
std::wstring widen(const std::string& str);
std::string narrow(const std::wstring& str);
//
// The plugin data that Notepad++ needs
//
FuncItem funcItem[nbFunc];
//
// The data of Notepad++ that you can use in your plugin commands
//
NppData nppData;
//
// Initialize your plugin data here
// It will be called while plugin loading
void pluginInit(HANDLE hModule)
{
hInstance = (HINSTANCE)hModule;
}
//
// Here you can do the clean up, save the parameters (if any) for the next session
//
void pluginCleanUp()
{
}
//
// Initialization of your plugin commands
// You should fill your plugins commands here
void commandMenuInit()
{
//--------------------------------------------//
//-- STEP 3. CUSTOMIZE YOUR PLUGIN COMMANDS --//
//--------------------------------------------//
// with function :
// setCommand(int index, // zero based number to indicate the order of command
// TCHAR *commandName, // the command name that you want to see in plugin menu
// PFUNCPLUGINCMD functionPointer, // the symbol of function (function pointer) associated with this command. The body should be defined below. See Step 4.
// ShortcutKey *shortcut, // optional. Define a shortcut to trigger this command
// bool check0nInit // optional. Make this menu item be checked visually
// );
setCommand(0, TEXT("Encrypt Document"), EncryptDoc, NULL, false);
setCommand(1, TEXT("Decrypt Document"), DecryptDoc, NULL, false);
setCommand(2, TEXT("Encrypt Selected Text"), EncryptSelection, NULL, false);
setCommand(3, TEXT("Decrypt Selected Text"), DecryptSelection, NULL, false);
setCommand(4, TEXT("About SecurePad"), AboutDlg, NULL, false);
}
//
// Here you can do the clean up (especially for the shortcut)
//
void commandMenuCleanUp()
{
// Don't forget to deallocate your shortcut here
}
//
// This function help you to initialize your plugin commands
//
bool setCommand(size_t index, TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
{
if (index >= nbFunc)
return false;
if (!pFunc)
return false;
lstrcpy(funcItem[index]._itemName, cmdName);
funcItem[index]._pFunc = pFunc;
funcItem[index]._init2Check = check0nInit;
funcItem[index]._pShKey = sk;
return true;
}
//----------------------------------------------//
//-- STEP 4. DEFINE YOUR ASSOCIATED FUNCTIONS --//
//----------------------------------------------//
void CryptDoc(CryptAction action)
{
int currentEdit = -1;
HWND hCurrentEditView = NULL;
size_t textLength = 0L, textLengthOut = 0L;
TCHAR *textBuf = NULL, *textBufOut = NULL;
// Prompt for crypt key
INT_PTR ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKey));
if(ret == 0)
{
return;
}
// Get edit view handle
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
if(currentEdit == 0) hCurrentEditView = nppData._scintillaMainHandle;
else hCurrentEditView = nppData._scintillaSecondHandle;
// Get document text length
textLength = (ULONG)SendMessage(hCurrentEditView, SCI_GETTEXTLENGTH, 0, 0);
if(textLength == 0)
{
MessageBox(nppData._nppHandle, TEXT("The document is empty!"), TEXT("Error"), MB_OK);
return;
}
textLength++;
textLengthOut = textLength;
if(action == CryptAction::Encrypt) textLengthOut *= 2;
// Create input buffer (round up to nearest 8 multiplier)
while(textLength % 8 != 0)textLength++;
textBuf = (TCHAR*)VirtualAlloc(NULL, textLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// Create output buffer (round up to nearest 8 multiplier)
while(textLengthOut % 8 != 0)textLengthOut++;
textBufOut = (TCHAR*)VirtualAlloc(NULL, textLengthOut, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// Get text
SendMessage(hCurrentEditView, SCI_GETTEXT, textLength, (LPARAM)textBuf);
// Crypt the text
if(action == CryptAction::Encrypt)
StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, CryptAction::Encrypt);
else
StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, CryptAction::Decrypt);
// Output to view
SendMessage(hCurrentEditView, SCI_SETTEXT, 0, (LPARAM)textBufOut);
// Cleanup
if(textBuf)
{
VirtualFree(textBuf, 0, MEM_RELEASE);
}
if (textBufOut)
{
VirtualFree(textBufOut, 0, MEM_RELEASE);
}
}
void CryptSelection(CryptAction action)
{
int currentEdit = -1;
HWND hCurrentEditView = NULL;
size_t textLength = 0L, textLengthOut = 0L;
TCHAR *textBuf = NULL, *textBufOut = NULL;
// Prompt for crypt key
INT_PTR ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKey));
if(ret == 0)
{
return;
}
// Get edit view handle
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
if(currentEdit == 0) hCurrentEditView = nppData._scintillaMainHandle;
else hCurrentEditView = nppData._scintillaSecondHandle;
// Get selected text length
Sci_PositionCR selectStart = SendMessage(hCurrentEditView, SCI_GETSELECTIONSTART, 0, 0);
Sci_PositionCR selectEnd = SendMessage(hCurrentEditView, SCI_GETSELECTIONEND, 0, 0);
if(selectEnd == 0 || (selectEnd - selectStart) == 0)
{
MessageBox(nppData._nppHandle, TEXT("No text was selected!"), TEXT("Error"), MB_OK);
return;
}
textLength = (selectEnd - selectStart);
textLength++;
textLengthOut = textLength;
if(action == CryptAction::Encrypt) textLengthOut *= 2;
// Create input buffer (round up to nearest 8 multiplier)
while(textLength % 8 != 0)textLength++;
textBuf = (TCHAR*)VirtualAlloc(NULL, textLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// Create output buffer (round up to nearest 8 multiplier)
while(textLengthOut % 8 != 0)textLengthOut++;
textBufOut = (TCHAR*)VirtualAlloc(NULL, textLengthOut, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
// Get text range
struct Sci_TextRange tr = {{selectStart, selectEnd}, reinterpret_cast<char*>(textBuf)};
SendMessage(hCurrentEditView, SCI_GETTEXTRANGE, 0, (LPARAM)&tr);
// Crypt the text
if(action == CryptAction::Encrypt)
StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, CryptAction::Encrypt);
else
StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut, textLengthOut, (unsigned char*)cryptkey, CryptAction::Decrypt);
// Output to view
SendMessage(hCurrentEditView, SCI_REPLACESEL, 0, (LPARAM)textBufOut);
// Cleanup
if (textBuf)
{
VirtualFree(textBuf, 0, MEM_RELEASE);
}
if (textBufOut)
{
VirtualFree(textBufOut, 0, MEM_RELEASE);
}
}
void AboutDlg()
{
::MessageBox(NULL, TEXT("SecurePad can be used to securely encrypt plaintext documents with a key of your choice. Be careful, once encrypted you will only be able to decrypt with the key you used!\r\n\r\nAny questions please visit www.dominictobias.com."), TEXT("SecurePad v2.4"), MB_OK);
}
// ===============================================
// Dialog for key entry
// ===============================================
LRESULT CALLBACK DlgProcCryptKey(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM /*lParam*/)
{
switch(msg)
{
case WM_DESTROY:
case WM_CLOSE:
{
EndDialog(hWndDlg, 0);
return TRUE;
}
case WM_INITDIALOG:
{
SetFocus(GetDlgItem(hWndDlg, IDC_EDIT1));
break;
}
case WM_COMMAND:
{
if(LOWORD(wParam) == IDOK)
{
TCHAR box1[MAX_CRYPT_KEY] = { 0 };
TCHAR box2[MAX_CRYPT_KEY] = { 0 };
LRESULT keyLen = 0;
SendMessage(GetDlgItem(hWndDlg, IDC_EDIT1), WM_GETTEXT, sizeof(box1), (LPARAM)box1);
SendMessage(GetDlgItem(hWndDlg, IDC_EDIT2), WM_GETTEXT, sizeof(box2), (LPARAM)box2);
if(_tcslen(box1) == 0 || _tcslen(box2) == 0)
{
MessageBox(nppData._nppHandle, TEXT("Both fields must be filled in!"), TEXT("Please try again"), MB_OK);
break;
}
if(_tcscmp(box1, box2))
{
MessageBox(nppData._nppHandle, TEXT("The keys did not match!"), TEXT("Please try again"), MB_OK);
break;
}
keyLen = SendMessage(GetDlgItem(hWndDlg, IDC_EDIT1), WM_GETTEXTLENGTH, 0, 0);
if(keyLen > MAX_CRYPT_KEY)
{
TCHAR buf[128];
_stprintf(buf, TEXT("The key was too long! (key=%Id, max=%d)\0"), keyLen, MAX_CRYPT_KEY);
MessageBox(nppData._nppHandle, buf, TEXT("Please try again"), MB_OK);
break;
}
if(keyLen < MIN_CRYPT_KEY)
{
TCHAR buf[128];
_stprintf(buf, TEXT("The key was too short! (key=%Id, min=%d)\0"), keyLen, MIN_CRYPT_KEY);
MessageBox(nppData._nppHandle, buf, TEXT("Please try again"), MB_OK);
break;
}
// Convert the key to narrow ANSII format
memset(&cryptkey, 0, sizeof(cryptkey));
std::string narrowKey = narrow(box1);
memcpy(cryptkey, narrowKey.c_str(), narrowKey.size() > MAX_CRYPT_KEY ? MAX_CRYPT_KEY : narrowKey.size());
EndDialog(hWndDlg, 1);
}
if(LOWORD(wParam) == IDCANCEL)
{
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
}
}
return FALSE;
}
// ===============================================
// Blowfish cypher
// ===============================================
//Function to convert unsigned char to string of length 2
void Char2Hex(const unsigned char ch, char* szHex)
{
unsigned char byte[2]{};
byte[0] = ch/16;
byte[1] = ch%16;
for(int i=0; i<2; i++)
{
if(byte[i] >= 0 && byte[i] <= 9)
szHex[i] = '0' + byte[i];
else
szHex[i] = 'A' + byte[i] - 10;
}
szHex[2] = 0;
}
//Function to convert string of length 2 to unsigned char
void Hex2Char(const char* szHex, unsigned char& rch)
{
rch = 0;
for(int i=0; i<2; i++)
{
if(*(szHex + i) >='0' && *(szHex + i) <= '9')
rch = (rch << 4) + (*(szHex + i) - '0');
else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')
rch = (rch << 4) + (*(szHex + i) - 'A' + 10);
else
break;
}
}
//Function to convert string of unsigned chars to string of chars
void CharStr2HexStr(const unsigned char* pucCharStr, char* pszHexStr, size_t lSize)
{
char szHex[3]{};
pszHexStr[0] = 0;
for(size_t i=0; i<lSize; i++)
{
Char2Hex(pucCharStr[i], szHex);
strcat(pszHexStr, szHex);
}
}
//Function to convert string of chars to string of unsigned chars
void HexStr2CharStr(const char* pszHexStr, unsigned char* pucCharStr, size_t lSize)
{
unsigned char ch;
for(size_t i=0; i<lSize; i++)
{
Hex2Char(pszHexStr+2*i, ch);
pucCharStr[i] = ch;
}
}
void StrCrypt(unsigned char *buf1, size_t buf1len, unsigned char *buf2, size_t buf2len, unsigned char *key, CryptAction action)
{
CBlowFish *BlowFish = new CBlowFish(key, strlen((char*)key));
if(action == CryptAction::Encrypt)
{
BlowFish->Encrypt(buf1, buf1, buf1len, CBlowFish::ECB);
CharStr2HexStr(buf1, (char*)buf2, buf1len);
}
else if(action == CryptAction::Decrypt)
{
HexStr2CharStr((char*)buf1, buf2, buf2len/2);
BlowFish->Decrypt(buf2, buf2, buf2len, CBlowFish::ECB);
}
delete BlowFish;
};
// ===============================================
// Helper funcs
// ===============================================
std::wstring widen(const std::string& str)
{
std::wostringstream wstm;
const std::ctype<wchar_t>& ctfacet =
std::use_facet< std::ctype<wchar_t> >(wstm.getloc());
for( size_t i=0 ; i<str.size() ; ++i )
wstm << ctfacet.widen( str[i] ) ;
return wstm.str() ;
}
std::string narrow(const std::wstring& str)
{
std::ostringstream stm;
const std::ctype<wchar_t>& ctfacet =
std::use_facet< std::ctype<wchar_t> >(stm.getloc());
for( size_t i=0 ; i<str.size() ; ++i )
stm << ctfacet.narrow( str[i], 0 ) ;
return stm.str() ;
}