Code:
#include <windows.h>
#include <string>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
void showOptions (HWND, HDC, PAINTSTRUCT);
void showPass (HWND, HDC, PAINTSTRUCT);
void showData (HWND, HDC, PAINTSTRUCT);
void EncryptDecrypt (const std::string &, std::string &);
/* Global Variables */
int cyChar, cxChar;
std::string password, data;
const std::string title = "Please select an option from the list";
const std::string line = "-------------------------------------";
const std::string opt1 = "1) Encrypt text";
const std::string opt2 = "2) Decrypt text";
const std::string enc1 = "Enter Password: ";
const std::string dat1 = "Data: ";
const std::string ins1 = "Instructions:";
const std::string ins2 = " Enter your password and data, then";
const std::string ins3 = "press enter to see the result.";
/* End Global Variables */
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR* szAppName = TEXT("EncDec");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT("ERROR This Program Requries NT!"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName, TEXT("Pea - Encryption / Decryption"), WS_OVERLAPPED | WS_CAPTION |
WS_SYSMENU | WS_BORDER | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 310, 200,
NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, iCmdShow);
UpdateWindow (hwnd);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
static short int choice = 0;
static bool pass = false, done = false;
HDC hdc;
TEXTMETRIC tm;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd);
/* First set the font we want */
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
/* Get the verticle size of char */
GetTextMetrics (hdc, &tm);
cyChar = tm.tmHeight + tm.tmExternalLeading;
cxChar = tm.tmAveCharWidth;
/* End */
ReleaseDC (hwnd, hdc);
return 0;
case WM_CHAR:
if (!choice)
{
switch (wParam)
{
case '1': choice = 1; break;
case '2': choice = 2; break;
default:
MessageBox (hwnd, TEXT("ERROR: Invalid Choice"),
TEXT("Pea"), MB_ICONERROR);
}
}
else if (choice == 1 || choice == 2 && !done)
{
if (!pass) // If password has not been entered
{
if (wParam == '\r') // If enter is pressed
{
if (password.empty())
{
MessageBox (hwnd, TEXT("ERROR: Password is empty"),
TEXT("Pea"), MB_ICONERROR);
break;
}
pass = true;
}
else if (wParam == '\b') // If backspace is pressed
{
if (password.empty()) break;
password.erase(password.size() - 1);
}
else
password += wParam;
}
else
{
if (wParam == '\b') // If backspace is pressed
{
if (data.empty()) break;
data.erase(data.size() - 1);
}
else if (wParam == '\r') // If enter is pressed
{
done = true, choice = 3;
EncryptDecrypt (password, data);
}
else
data += wParam;
}
}
else if (done) // Wait for key press to restart
{
choice = 0, done = false, pass = false;
password.erase(); data.erase();
}
InvalidateRect (hwnd, NULL, TRUE); // Update client area
return 0;
case WM_PAINT:
switch (choice)
{
case 0: showOptions (hwnd, hdc, ps); break;
case 1: (!pass) ? showPass (hwnd, hdc, ps) : showData (hwnd, hdc, ps); break;
case 2: (!pass) ? showPass (hwnd, hdc, ps) : showData (hwnd, hdc, ps); break;
case 3: showData (hwnd, hdc, ps); break;
default:
MessageBeep (0);
MessageBox (hwnd, TEXT("INTERNAL ERROR!"), TEXT("Pea"), MB_ICONERROR);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage (0);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
void showOptions (HWND hwnd, HDC hdc, PAINTSTRUCT ps) {
hdc = BeginPaint (hwnd, &ps);
/* Chose font */
SelectObject (hdc, GetStockObject (ANSI_FIXED_FONT));
/* Set background mode to blend with background color */
SetBkMode (hdc, TRANSPARENT);
/* Print text */
TextOut (hdc, 0, 0, title.c_str(), title.size());
TextOut (hdc, 0, cyChar, line.c_str(), line.size());
TextOut (hdc, 0, cyChar * 2, opt1.c_str(), opt1.size());
TextOut (hdc, 0, cyChar * 3, opt2.c_str(), opt2.size());
TextOut (hdc, 0, cyChar * 6, line.c_str(), line.size());
TextOut (hdc, 0, cyChar * 7, ins1.c_str(), ins1.size());
TextOut (hdc, 0, cyChar * 8, ins2.c_str(), ins2.size());
TextOut (hdc, 0, cyChar * 9, ins3.c_str(), ins3.size());
EndPaint(hwnd, &ps);
}
void showPass (HWND hwnd, HDC hdc, PAINTSTRUCT ps) {
std::string mask;
hdc = BeginPaint (hwnd, &ps);
/* Chose font */
SelectObject (hdc, GetStockObject (ANSI_FIXED_FONT));
/* Set background mode to blend with background color */
SetBkMode (hdc, TRANSPARENT);
/* Print text */
TextOut (hdc, 0, 0, enc1.c_str(), enc1.size());
/* Mask password */
mask.append (password.size(), '*');
TextOut (hdc, cxChar * enc1.size(), 0, mask.c_str(), mask.size());
EndPaint (hwnd, &ps);
}
void showData (HWND hwnd, HDC hdc, PAINTSTRUCT ps) {
int loop = 0, index = 1;
hdc = BeginPaint (hwnd, &ps);
/* Chose font */
SelectObject (hdc, GetStockObject (ANSI_FIXED_FONT));
/* Set background mode to blend with background color */
SetBkMode (hdc, TRANSPARENT);
/* Print text */
TextOut (hdc, 0, 0, dat1.c_str(), dat1.size());
/* Print data */
do
{
TextOut (hdc, 0, cyChar * index, data.c_str() + loop,
data.size() - loop);
loop += 38, index++; // 38 characters fit in the window
if (index == 12)
{
MessageBox (hwnd, TEXT("ERROR: Max characters reached"),
TEXT("Pea"), MB_ICONERROR);
data.erase(data.size() - 1);
InvalidateRect (hwnd, NULL, TRUE);
}
}
while (loop < data.size());
EndPaint (hwnd, &ps);
}
void EncryptDecrypt (const std::string &pass, std::string &data) {
for (int loop = 0; loop < data.size(); loop++)
data[loop] ^= (pass[loop % pass.size()] % 6);
}