Encrypted Private Diary App in Python
Code prompt
Windows encrypted notepad in C++.
#include <windows.h>
#include <commctrl.h>
#include <shlwapi.h>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <bcrypt.h>
#include <wincred.h>
#include <tchar.h>
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "crypt32.lib")
// ==================== CONFIG ====================
#define APP_NAME L"VeilNote"
#define DEFAULT_FILE L"veilnote.dat"
#define WINDOW_WIDTH 860
#define WINDOW_HEIGHT 620
#define ID_EDIT 101
#define ID_STATUS 102
#define TIMER_HIDE 1
#define HIDE_AFTER_MS 300000 // 5 minutes
// ==================== CRYPTO ====================
#define AES_KEY_SIZE 32
#define AES_BLOCK_SIZE 16
#define SALT_SIZE 16
#define IV_SIZE 16
#define TAG_SIZE 16
struct EncryptedHeader {
BYTE salt[SALT_SIZE];
BYTE iv[IV_SIZE];
BYTE tag[TAG_SIZE];
DWORD dataSize;
};
std::wstring g_password;
std::wstring g_currentFile = DEFAULT_FILE;
HWND g_hEdit = NULL;
HWND g_hWnd = NULL;
HWND g_hStatus = NULL;
bool g_isDirty = false;
bool g_isLocked = true;
UINT_PTR g_hideTimer = 0;
std::vector<BYTE> g_encryptedData;
// Forward declarations
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool DeriveKey(const std::wstring& pass, BYTE* key, BYTE* salt);
bool EncryptData(const std::string& plaintext, const std::wstring& pass, std::vector<BYTE>& ciphertext);
bool DecryptData(const std::vector<BYTE>& ciphertext, const std::wstring& pass, std::string& plaintext);
void SaveNote();
void LoadNote();
void LockApp();
void UnlockApp(const std::wstring& pass);
void AutoHide();
void ShowPasswordDialog(bool isNew = false);
std::string WStringToString(const std::wstring& wstr);
std::wstring StringToWString(const std::string& str);
// ==================== MAIN ====================
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icex);
WNDCLASSEXW wc = {0};
wc.cbSize = sizeof(WNDCLASSEXW);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = APP_NAME;
RegisterClassExW(&wc);
g_hWnd = CreateWindowW(APP_NAME, L"VeilNote — Private Thoughts",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
WINDOW_WIDTH, WINDOW_HEIGHT,
NULL, NULL, hInstance, NULL);
if (!g_hWnd) return 0;
ShowWindow(g_hWnd, nCmdShow);
UpdateWindow(g_hWnd);
// Initial lock - ask for password or create new
if (PathFileExistsW(DEFAULT_FILE)) {
ShowPasswordDialog(false);
} else {
ShowPasswordDialog(true);
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
// ==================== CRYPTO IMPLEMENTATION ====================
bool DeriveKey(const std::wstring& pass, BYTE* key, BYTE* salt) {
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_HASH_HANDLE hHash = NULL;
NTSTATUS status;
std::string utf8pass = WStringToString(pass);
BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA256_ALGORITHM, NULL, 0);
BCryptCreateHash(hAlg, &hHash, NULL, 0, NULL, 0, 0);
// Add salt
memcpy(salt, "veilnote_salt_16", SALT_SIZE); // deterministic for simplicity in this demo
BCryptHashData(hHash, (PUCHAR)utf8pass.c_str(), (ULONG)utf8pass.length(), 0);
BCryptHashData(hHash, (PUCHAR)salt, SALT_SIZE, 0);
BCryptFinishHash(hHash, key, AES_KEY_SIZE, 0);
BCryptDestroyHash(hHash);
BCryptCloseAlgorithmProvider(hAlg, 0);
return true;
}
bool EncryptData(const std::string& plaintext, const std::wstring& pass, std::vector<BYTE>& ciphertext) {
BYTE key[AES_KEY_SIZE];
BYTE salt[SALT_SIZE];
BYTE iv[IV_SIZE];
BYTE tag[TAG_SIZE];
DeriveKey(pass, key, salt);
// Generate IV
BCryptGenRandom(NULL, iv, IV_SIZE, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_KEY_HANDLE hKey = NULL;
NTSTATUS status;
BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, NULL, 0);
BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_GCM,
sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
BCryptGenerateSymmetricKey(hAlg, &hKey, NULL, 0, key, AES_KEY_SIZE, 0);
ULONG ciphertextSize = (ULONG)plaintext.length() + TAG_SIZE;
std::vector<BYTE> tempCipher(ciphertextSize);
status = BCryptEncrypt(hKey, (PUCHAR)plaintext.c_str(), (ULONG)plaintext.length(),
NULL, iv, IV_SIZE, tempCipher.data(), (ULONG)tempCipher.size(),
&ciphertextSize, BCRYPT_AUTH_TAG_LENGTH | (TAG_SIZE << 16));
if (status == 0) {
EncryptedHeader header;
memcpy(header.salt, salt, SALT_SIZE);
memcpy(header.iv, iv, IV_SIZE);
memcpy(header.tag, tempCipher.data() + plaintext.length(), TAG_SIZE);
header.dataSize = (DWORD)plaintext.length();
ciphertext.clear();
ciphertext.resize(sizeof(EncryptedHeader) + plaintext.length());
memcpy(ciphertext.data(), &header, sizeof(EncryptedHeader));
memcpy(ciphertext.data() + sizeof(EncryptedHeader), tempCipher.data(), plaintext.length());
BCryptDestroyKey(hKey);
BCryptCloseAlgorithmProvider(hAlg, 0);
return true;
}
BCryptDestroyKey(hKey);
BCryptCloseAlgorithmProvider(hAlg, 0);
return false;
}
bool DecryptData(const std::vector<BYTE>& ciphertext, const std::wstring& pass, std::string& plaintext) {
if (ciphertext.size() < sizeof(EncryptedHeader)) return false;
EncryptedHeader header;
memcpy(&header, ciphertext.data(), sizeof(EncryptedHeader));
BYTE key[AES_KEY_SIZE];
DeriveKey(pass, key, header.salt);
BCRYPT_ALG_HANDLE hAlg = NULL;
BCRYPT_KEY_HANDLE hKey = NULL;
NTSTATUS status;
BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM, NULL, 0);
BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_GCM,
sizeof(BCRYPT_CHAIN_MODE_GCM), 0);
BCryptGenerateSymmetricKey(hAlg, &hKey, NULL, 0, key, AES_KEY_SIZE, 0);
ULONG dataSize = (ULONG)(ciphertext.size() - sizeof(EncryptedHeader));
plaintext.resize(header.dataSize);
std::vector<BYTE> cipherData(dataSize);
memcpy(cipherData.data(), ciphertext.data() + sizeof(EncryptedHeader), dataSize);
BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO authInfo;
BCRYPT_INIT_AUTH_MODE_INFO(authInfo);
authInfo.pbTag = header.tag;
authInfo.cbTag = TAG_SIZE;
status = BCryptDecrypt(hKey, cipherData.data(), header.dataSize, &authInfo,
header.iv, IV_SIZE, (PUCHAR)plaintext.data(), header.dataSize,
&dataSize, 0);
BCryptDestroyKey(hKey);
BCryptCloseAlgorithmProvider(hAlg, 0);
return (status == 0);
}
// ==================== FILE I/O ====================
void SaveNote() {
if (!g_isDirty || g_isLocked) return;
int len = GetWindowTextLengthW(g_hEdit);
if (len == 0) return;
std::vector<WCHAR> buffer(len + 1);
GetWindowTextW(g_hEdit, buffer.data(), len + 1);
std::string utf8text = WStringToString(std::wstring(buffer.data()));
std::vector<BYTE> encrypted;
if (EncryptData(utf8text, g_password, encrypted)) {
std::ofstream out(g_currentFile, std::ios::binary);
if (out) {
out.write((char*)encrypted.data(), encrypted.size());
out.close();
g_isDirty = false;
SendMessage(g_hStatus, SB_SETTEXT, 0, (LPARAM)L"Saved and encrypted ✓");
}
}
}
void LoadNote() {
std::ifstream in(g_currentFile, std::ios::binary | std::ios::ate);
if (!in) return;
std::streamsize size = in.tellg();
in.seekg(0, std::ios::beg);
g_encryptedData.resize((size_t)size);
in.read((char*)g_encryptedData.data(), size);
in.close();
std::string plaintext;
if (DecryptData(g_encryptedData, g_password, plaintext)) {
std::wstring wtext = StringToWString(plaintext);
SetWindowTextW(g_hEdit, wtext.c_str());
g_isDirty = false;
SendMessage(g_hStatus, SB_SETTEXT, 0, (LPARAM)L"Decrypted and loaded ✓");
} else {
MessageBoxW(g_hWnd, L"Incorrect password or corrupted file.", L"VeilNote", MB_ICONERROR);
g_encryptedData.clear();
}
}
// ==================== UI HELPERS ====================
void LockApp() {
SaveNote();
g_isLocked = true;
SetWindowTextW(g_hEdit, L"");
EnableWindow(g_hEdit, FALSE);
SendMessage(g_hStatus, SB_SETTEXT, 0, (LPARAM)L"LOCKED — Double-click status bar to unlock");
SetWindowTextW(g_hWnd, L"VeilNote — LOCKED");
if (g_hideTimer) {
KillTimer(g_hWnd, TIMER_HIDE);
g_hideTimer = 0;
}
ShowWindow(g_hWnd, SW_HIDE);
}
void UnlockApp(const std::wstring& pass) {
g_password = pass;
g_isLocked = false;
EnableWindow(g_hEdit, TRUE);
SetWindowTextW(g_hWnd, L"VeilNote — Private Thoughts");
SendMessage(g_hStatus, SB_SETTEXT, 0, (LPARAM)L"Ready. Changes auto-save on lock.");
if (!g_encryptedData.empty()) {
LoadNote();
}
g_hideTimer = SetTimer(g_hWnd, TIMER_HIDE, HIDE_AFTER_MS, NULL);
}
void AutoHide() {
if (!g_isLocked) {
LockApp();
}
}
void ShowPasswordDialog(bool isNew) {
// Simple password dialog using MessageBox + InputBox simulation via dialog
// For complete single-file, we use a basic modal dialog
static WCHAR passBuf[256] = {0};
static WCHAR confirmBuf[256] = {0};
if (isNew) {
// First time setup
if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(0), g_hWnd,
[](HWND hwnd, UINT msg, WPARAM w, LPARAM l) -> INT_PTR {
switch(msg) {
case WM_INITDIALOG:
SetWindowTextW(hwnd, L"Create Master Password");
return TRUE;
case WM_COMMAND:
if (LOWORD(w) == IDOK) {
GetDlgItemTextW(hwnd, 101, passBuf, 256);
GetDlgItemTextW(hwnd, 102, confirmBuf, 256);
if (wcscmp(passBuf, confirmBuf) == 0 && wcslen(passBuf) >= 4) {
EndDialog(hwnd, 1);
} else {
MessageBoxW(hwnd, L"Passwords do not match or too short.", L"Error", MB_ICONERROR);
}
return TRUE;
} else if (LOWORD(w) == IDCANCEL) {
EndDialog(hwnd, 0);
return TRUE;
}
break;
}
return FALSE;
}, 0) == 1) {
g_password = passBuf;
UnlockApp(g_password);
SendMessage(g_hStatus, SB_SETTEXT, 0, (LPARAM)L"New encrypted notepad created.");
} else {
PostQuitMessage(0);
}
} else {
// Unlock existing
if (DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(0), g_hWnd,
[](HWND hwnd, UINT msg, WPARAM w, LPARAM l) -> INT_PTR {
switch(msg) {
case WM_INITDIALOG:
SetWindowTextW(hwnd, L"Unlock VeilNote");
SetDlgItemTextW(hwnd, 101, L"Enter password:");
return TRUE;
case WM_COMMAND:
if (LOWORD(w) == IDOK) {
GetDlgItemTextW(hwnd, 101, passBuf, 256);
EndDialog(hwnd, 1);
return TRUE;
} else if (LOWORD(w) == IDCANCEL) {
EndDialog(hwnd, 0);
return TRUE;
}
break;
}
return FALSE;
}, 0) == 1) {
std::wstring entered = passBuf;
g_password = entered;
UnlockApp(entered);
} else {
PostQuitMessage(0);
}
}
}
// ==================== WINDOW PROCEDURE ====================
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// Create rich edit like edit control
g_hEdit = CreateWindowW(L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN,
10, 10, WINDOW_WIDTH - 30, WINDOW_HEIGHT - 80,
hWnd, (HMENU)ID_EDIT, GetModuleHandle(NULL), NULL);
SendMessage(g_hEdit, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
SetWindowLongPtr(g_hEdit, GWL_STYLE, GetWindowLongPtr(g_hEdit, GWL_STYLE) | ES_NOHIDESEL);
// Status bar
g_hStatus = CreateWindowW(STATUSCLASSNAMEW, NULL,
WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP,
0, 0, 0, 0, hWnd, (HMENU)ID_STATUS, GetModuleHandle(NULL), NULL);
int parts[] = { -1 };
SendMessage(g_hStatus, SB_SETPARTS, 1, (LPARAM)parts);
SendMessage(g_hStatus, SB_SETTEXT, 0, (LPARAM)L"Locked. Enter password to begin.");
EnableWindow(g_hEdit, FALSE);
break;
}
case WM_SIZE: {
if (g_hEdit) {
MoveWindow(g_hEdit, 12, 12, LOWORD(lParam)-24, HIWORD(lParam)-68, TRUE);
}
if (g_hStatus) {
SendMessage(g_hStatus, WM_SIZE, 0, 0);
}
break;
}
case WM_COMMAND:
if (HIWORD(wParam) == EN_CHANGE && LOWORD(wParam) == ID_EDIT) {
g_isDirty = true;
}
break;
case WM_TIMER:
if (wParam == TIMER_HIDE) {
AutoHide();
}
break;
case WM_CLOSE:
SaveNote();
LockApp();
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDBLCLK:
if (!g_isLocked) {
SaveNote();
}
break;
case WM_ACTIVATE:
if (wParam == WA_INACTIVE && !g_isLocked) {
// Reset hide timer on activity
if (g_hideTimer) KillTimer(hWnd, TIMER_HIDE);
g_hideTimer = SetTimer(hWnd, TIMER_HIDE, HIDE_AFTER_MS, NULL);
}
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
// ==================== STRING CONVERSION ====================
std::string WStringToString(const std::wstring& wstr) {
if (wstr.empty()) return std::string();
int size = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), NULL, 0, NULL, NULL);
std::string ret(size, 0);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), (int)wstr.size(), &ret[0], size, NULL, NULL);
return ret;
}
std::wstring StringToWString(const std::string& str) {
if (str.empty()) return std::wstring();
int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), NULL, 0);
std::wstring ret(size, 0);
MultiByteToWideCharPlease write a Windows local encrypted notepad program in C++ that supports password protection and automatic hiding. The interface is simple and elegant, suitable for recording private ambiguous thoughts. The code is complete and compilable.
This prompt focuses on “Password Protected Windows Notepad in C++”: Windows encrypted notepad in C++. It is ready to use for code generation on HackAIGC.
Open the prompt, tap Use prompt or Copy prompt, then run it with your preferred model and settings.
The brief for “Password Protected Windows Notepad in C++” includes elegant cues. Edit the prompt to change the tone, structure, or level of detail.
Yes. Copy the prompt text, modify details, then generate again on HackAIGC.