forked from dominictobias/SecurePad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginDefinition.cpp
More file actions
executable file
·796 lines (672 loc) · 28.1 KB
/
Copy pathPluginDefinition.cpp
File metadata and controls
executable file
·796 lines (672 loc) · 28.1 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//this file is part of notepad++
//Copyright (C)2003 Don HO <donho@altern.org>
//
//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"
#include "Scintilla.h"
#include "Notepad_plus_msgs.h"
#include <shlwapi.h>
//
// Globals
//
HINSTANCE hInstance = NULL;
unsigned char cryptkey[MAX_CRYPT_KEY];
// Per-buffer cached keys for auto encrypt/decrypt of .ctxt files
static std::map<UINT_PTR, std::string> g_bufferKeys;
// Buffer currently mid-save (encrypted, awaiting post-save decrypt)
static UINT_PTR g_pendingDecryptBuffer = 0;
// Buffer pre-encrypted by EncryptSaveAsCtxt — tells OnFileBeforeSave to skip
// re-encrypting (NPPM_SAVECURRENTFILEAS fires BEFORESAVE with the old path,
// so the path-based check in the hook would miss it anyway).
static UINT_PTR g_skipNextBeforeSaveEncrypt = 0;
// Buffers opened as .ctxt awaiting lazy decrypt-on-view (set on FILEOPENED,
// consumed on BUFFERACTIVATED). Lets file-open complete without blocking
// on a modal prompt; the popup only appears when the user actually views
// the document.
static std::set<UINT_PTR> g_pendingOpenDecrypt;
//
// 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);
LRESULT CALLBACK DlgProcCryptKeySingle(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("Encrypt && Save As .ctxt..."), EncryptSaveAsCtxt, NULL, false);
setCommand(5, 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 — encrypt = confirm (two fields), decrypt = single field
INT_PTR ret;
if (action == CryptAction::Encrypt)
ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKey));
else
ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY_SINGLE), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKeySingle));
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 — encrypt = confirm (two fields), decrypt = single field
INT_PTR ret;
if (action == CryptAction::Encrypt)
ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKey));
else
ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY_SINGLE), nppData._nppHandle, reinterpret_cast<DLGPROC>(DlgProcCryptKeySingle));
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.5"), 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;
}
// ===============================================
// Dialog for single-field key entry (decrypt)
// ===============================================
LRESULT CALLBACK DlgProcCryptKeySingle(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_EDIT_PASS));
break;
case WM_COMMAND:
{
if (LOWORD(wParam) == IDOK)
{
TCHAR box[MAX_CRYPT_KEY] = { 0 };
SendMessage(GetDlgItem(hWndDlg, IDC_EDIT_PASS), WM_GETTEXT, sizeof(box), (LPARAM)box);
LRESULT keyLen = SendMessage(GetDlgItem(hWndDlg, IDC_EDIT_PASS), WM_GETTEXTLENGTH, 0, 0);
if (keyLen == 0)
{
MessageBox(nppData._nppHandle, TEXT("Enter a key."), TEXT("Please try again"), MB_OK);
break;
}
if (keyLen > MAX_CRYPT_KEY)
{
TCHAR buf[128];
_stprintf(buf, TEXT("The key was too long! (key=%Id, max=%d)"), keyLen, MAX_CRYPT_KEY);
MessageBox(nppData._nppHandle, buf, TEXT("Please try again"), MB_OK);
break;
}
memset(&cryptkey, 0, sizeof(cryptkey));
std::string narrowKey = narrow(box);
memcpy(cryptkey, narrowKey.c_str(),
narrowKey.size() > MAX_CRYPT_KEY ? MAX_CRYPT_KEY : narrowKey.size());
EndDialog(hWndDlg, 1);
}
else 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() ;
}
// ===============================================
// Auto encrypt/decrypt for .ctxt files
// ===============================================
bool IsCtxtPath(const TCHAR* path)
{
if (!path) return false;
const TCHAR* ext = PathFindExtension(path);
return ext && _tcsicmp(ext, TEXT(".ctxt")) == 0;
}
bool GetBufferPath(UINT_PTR bufferId, std::wstring& outPath)
{
LRESULT len = SendMessage(nppData._nppHandle, NPPM_GETFULLPATHFROMBUFFERID, (WPARAM)bufferId, 0);
if (len < 0) return false;
outPath.resize((size_t)len + 1, L'\0');
SendMessage(nppData._nppHandle, NPPM_GETFULLPATHFROMBUFFERID, (WPARAM)bufferId, (LPARAM)&outPath[0]);
outPath.resize((size_t)len);
return true;
}
// Reuses IDD_DLGKEY dialog (two fields, confirm). Returns true on OK.
bool PromptForKey(unsigned char* keyOut, size_t keyOutSize)
{
INT_PTR ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY), nppData._nppHandle,
reinterpret_cast<DLGPROC>(DlgProcCryptKey));
if (ret == 0) return false;
memset(keyOut, 0, keyOutSize);
size_t copyLen = strlen((const char*)cryptkey);
if (copyLen > keyOutSize) copyLen = keyOutSize;
memcpy(keyOut, cryptkey, copyLen);
return true;
}
// Reuses IDD_DLGKEY_SINGLE dialog (single field). For decrypt flows where the
// user already knows the key — no confirmation needed.
bool PromptForKeyOnce(unsigned char* keyOut, size_t keyOutSize)
{
INT_PTR ret = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGKEY_SINGLE), nppData._nppHandle,
reinterpret_cast<DLGPROC>(DlgProcCryptKeySingle));
if (ret == 0) return false;
memset(keyOut, 0, keyOutSize);
size_t copyLen = strlen((const char*)cryptkey);
if (copyLen > keyOutSize) copyLen = keyOutSize;
memcpy(keyOut, cryptkey, copyLen);
return true;
}
// Encrypt/decrypt current document with provided key (no prompt).
void CryptCurrentDocWithKey(CryptAction action, const unsigned char* key)
{
int currentEdit = -1;
HWND hView = NULL;
size_t textLength = 0, textLengthOut = 0;
TCHAR *textBuf = NULL, *textBufOut = NULL;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
hView = (currentEdit == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
textLength = (size_t)SendMessage(hView, SCI_GETTEXTLENGTH, 0, 0);
if (textLength == 0) return;
textLength++;
textLengthOut = textLength;
if (action == CryptAction::Encrypt) textLengthOut *= 2;
while (textLength % 8 != 0) textLength++;
textBuf = (TCHAR*)VirtualAlloc(NULL, textLength, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
while (textLengthOut % 8 != 0) textLengthOut++;
textBufOut = (TCHAR*)VirtualAlloc(NULL, textLengthOut, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
SendMessage(hView, SCI_GETTEXT, textLength, (LPARAM)textBuf);
// StrCrypt takes non-const key — copy into mutable buffer
unsigned char keyBuf[MAX_CRYPT_KEY] = { 0 };
size_t keyLen = strlen((const char*)key);
if (keyLen > MAX_CRYPT_KEY) keyLen = MAX_CRYPT_KEY;
memcpy(keyBuf, key, keyLen);
StrCrypt((unsigned char*)textBuf, textLength, (unsigned char*)textBufOut,
textLengthOut, keyBuf, action);
SendMessage(hView, SCI_SETTEXT, 0, (LPARAM)textBufOut);
if (textBuf) VirtualFree(textBuf, 0, MEM_RELEASE);
if (textBufOut) VirtualFree(textBufOut, 0, MEM_RELEASE);
}
// Lazy: only mark the buffer as needing decrypt. Do NOT prompt here — that
// would block NPPN_FILEOPENED processing and block batch opens (cmdline,
// session restore, "open recent"). Prompt happens on first BUFFERACTIVATED.
void OnFileOpened(UINT_PTR bufferId)
{
std::wstring path;
if (!GetBufferPath(bufferId, path)) return;
if (!IsCtxtPath(path.c_str())) return;
g_pendingOpenDecrypt.insert(bufferId);
}
// Fires when a buffer becomes the visible tab. Lazy-decrypts pending .ctxt
// buffers here so the prompt only appears for documents the user actually views.
void OnBufferActivated(UINT_PTR bufferId)
{
auto it = g_pendingOpenDecrypt.find(bufferId);
if (it == g_pendingOpenDecrypt.end()) return;
g_pendingOpenDecrypt.erase(it);
unsigned char key[MAX_CRYPT_KEY] = { 0 };
if (!PromptForKeyOnce(key, sizeof(key))) return;
CryptCurrentDocWithKey(CryptAction::Decrypt, key);
g_bufferKeys[bufferId] = std::string((const char*)key);
int currentEdit = -1;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hView = (currentEdit == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
SendMessage(hView, SCI_SETSAVEPOINT, 0, 0);
SendMessage(hView, SCI_EMPTYUNDOBUFFER, 0, 0);
}
void OnFileBeforeSave(UINT_PTR bufferId)
{
// EncryptSaveAsCtxt already encrypted the buffer in-place. NP++ will write
// it as-is; FILESAVED hook still handles the post-save decrypt back.
if (g_skipNextBeforeSaveEncrypt == bufferId)
{
g_skipNextBeforeSaveEncrypt = 0;
return;
}
std::wstring path;
if (!GetBufferPath(bufferId, path)) return;
if (!IsCtxtPath(path.c_str())) return;
// Find or prompt for key (Save As .ctxt on a new buffer)
auto it = g_bufferKeys.find(bufferId);
unsigned char key[MAX_CRYPT_KEY] = { 0 };
if (it == g_bufferKeys.end())
{
if (!PromptForKey(key, sizeof(key)))
{
MessageBox(nppData._nppHandle,
TEXT("No key provided. File will be saved as plaintext."),
TEXT("SecurePad"), MB_OK | MB_ICONWARNING);
return;
}
g_bufferKeys[bufferId] = std::string((const char*)key);
}
else
{
size_t n = it->second.size();
if (n > MAX_CRYPT_KEY) n = MAX_CRYPT_KEY;
memcpy(key, it->second.data(), n);
}
CryptCurrentDocWithKey(CryptAction::Encrypt, key);
g_pendingDecryptBuffer = bufferId;
}
void OnFileSaved(UINT_PTR bufferId)
{
if (g_pendingDecryptBuffer != bufferId) return;
g_pendingDecryptBuffer = 0;
auto it = g_bufferKeys.find(bufferId);
if (it == g_bufferKeys.end()) return;
unsigned char key[MAX_CRYPT_KEY] = { 0 };
size_t n = it->second.size();
if (n > MAX_CRYPT_KEY) n = MAX_CRYPT_KEY;
memcpy(key, it->second.data(), n);
CryptCurrentDocWithKey(CryptAction::Decrypt, key);
int currentEdit = -1;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hView = (currentEdit == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
SendMessage(hView, SCI_SETSAVEPOINT, 0, 0);
}
void OnFileClosed(UINT_PTR bufferId)
{
g_bufferKeys.erase(bufferId);
g_pendingOpenDecrypt.erase(bufferId);
if (g_pendingDecryptBuffer == bufferId) g_pendingDecryptBuffer = 0;
}
// Menu action: prompt for path + key, save current buffer as .ctxt (encrypted),
// leaving buffer associated with the new .ctxt path showing plaintext.
void EncryptSaveAsCtxt()
{
// Document must be non-empty
int currentEdit = -1;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
HWND hView = (currentEdit == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
if (SendMessage(hView, SCI_GETTEXTLENGTH, 0, 0) == 0)
{
MessageBox(nppData._nppHandle, TEXT("The document is empty!"), TEXT("SecurePad"), MB_OK);
return;
}
// Default filename = current name with .ctxt appended
TCHAR currentName[MAX_PATH] = { 0 };
SendMessage(nppData._nppHandle, NPPM_GETFILENAME, MAX_PATH, (LPARAM)currentName);
TCHAR pathBuf[MAX_PATH] = { 0 };
if (currentName[0] != 0)
{
_tcsncpy(pathBuf, currentName, MAX_PATH - 6);
// Strip existing extension, append .ctxt
TCHAR* ext = PathFindExtension(pathBuf);
if (ext && *ext) *ext = 0;
_tcscat(pathBuf, TEXT(".ctxt"));
}
else
{
_tcscpy(pathBuf, TEXT("untitled.ctxt"));
}
// Default directory = current file's directory
TCHAR initDir[MAX_PATH] = { 0 };
SendMessage(nppData._nppHandle, NPPM_GETCURRENTDIRECTORY, MAX_PATH, (LPARAM)initDir);
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = nppData._nppHandle;
ofn.lpstrFilter = TEXT("SecurePad cipher (*.ctxt)\0*.ctxt\0All files (*.*)\0*.*\0");
ofn.lpstrFile = pathBuf;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrInitialDir = initDir[0] ? initDir : NULL;
ofn.lpstrDefExt = TEXT("ctxt");
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrTitle = TEXT("Encrypt & Save As .ctxt");
if (!GetSaveFileName(&ofn)) return;
// Force .ctxt extension if user typed something else
if (!IsCtxtPath(pathBuf))
{
TCHAR* ext = PathFindExtension(pathBuf);
if (ext && *ext) *ext = 0;
_tcscat(pathBuf, TEXT(".ctxt"));
}
// Prompt for password — single field (no verify)
unsigned char key[MAX_CRYPT_KEY] = { 0 };
if (!PromptForKeyOnce(key, sizeof(key))) return;
UINT_PTR bufferId = (UINT_PTR)SendMessage(nppData._nppHandle, NPPM_GETCURRENTBUFFERID, 0, 0);
g_bufferKeys[bufferId] = std::string((const char*)key);
// Encrypt the buffer in-place BEFORE Save As. NPPM_SAVECURRENTFILEAS fires
// NPPN_FILEBEFORESAVE with the OLD path (pre-rename), so relying on the
// hook's path check would skip encryption and write plaintext.
CryptCurrentDocWithKey(CryptAction::Encrypt, key);
// Suppress the hook's encrypt pass; FILESAVED still decrypts back.
g_skipNextBeforeSaveEncrypt = bufferId;
g_pendingDecryptBuffer = bufferId;
LRESULT ok = SendMessage(nppData._nppHandle, NPPM_SAVECURRENTFILEAS, (WPARAM)FALSE, (LPARAM)pathBuf);
if (!ok)
{
// Save failed — restore plaintext buffer and drop the cached key.
CryptCurrentDocWithKey(CryptAction::Decrypt, key);
g_pendingDecryptBuffer = 0;
g_skipNextBeforeSaveEncrypt = 0;
g_bufferKeys.erase(bufferId);
MessageBox(nppData._nppHandle, TEXT("Save failed."), TEXT("SecurePad"), MB_OK | MB_ICONERROR);
}
}