Posted September 12, 200519 yr My first WIN32 app. Here you go. I know it's a little sloppy, but I didn't try to clean it up. #include <windows.h> #include "resource.h" const char g_szClassName[] = "myWindowClass"; const char windowname[] = "Notepad Encryption"; char strname[100]; HWND hwnd; HWND hEditz; HWND hEncrypt; bool isshowing; void encrypt(char* encryptstr) { int count; for (count = 0; count < strlen(encryptstr); count++){ if (count % 10 == 0) encryptstr[count] += 22; if (count % 9 == 0) encryptstr[count] -= 41; if (count % 8 == 0) encryptstr[count] += 35; if (count % 7 == 0) encryptstr[count] -= 15; if (count % 6 == 0) encryptstr[count] += 75; if (count % 5 == 0) encryptstr[count] -= 85; if (count % 4 == 0) encryptstr[count] += 62; if (count % 3 == 0) encryptstr[count] -= 71; if (count % 2 == 0) encryptstr[count] += 55; encryptstr[count] -= 21; if (encryptstr[count] == 0) encryptstr[count] += 1; } } void encryptkey(char* encryptstr, int key) { int count; for (count = 0; count < strlen(encryptstr); count++){ if (count % 10 == 0) encryptstr[count] += 22+key%5; if (count % 9 == 0) encryptstr[count] -= 41+key%7; if (count % 8 == 0) encryptstr[count] += 35+key%8; if (count % 7 == 0) encryptstr[count] -= 15+key%2; if (count % 6 == 0) encryptstr[count] += 75+key%5; if (count % 5 == 0) encryptstr[count] -= 85+key%7; if (count % 4 == 0) encryptstr[count] += 62+key%6; if (count % 3 == 0) encryptstr[count] -= 71+key%4; if (count % 2 == 0) encryptstr[count] += 55+key%2; encryptstr[count] -= 21+key%7; if (encryptstr[count] == 0) encryptstr[count] += 1; } } void decryptkey(char* encryptstr, int key) { int count; for (count = 0; count < strlen(encryptstr); count++){ if (count % 10 == 0) encryptstr[count] -= 22+key%5; if (count % 9 == 0) encryptstr[count] += 41+key%7; if (count % 8 == 0) encryptstr[count] -= 35+key%8; if (count % 7 == 0) encryptstr[count] += 15+key%2; if (count % 6 == 0) encryptstr[count] -= 75+key%5; if (count % 5 == 0) encryptstr[count] += 85+key%7; if (count % 4 == 0) encryptstr[count] -= 62+key%6; if (count % 3 == 0) encryptstr[count] += 71+key%4; if (count % 2 == 0) encryptstr[count] -= 55+key%2; encryptstr[count] += 21+key%7; } } void decrypt(char* encryptstr) { int count; for (count = 0; count < strlen(encryptstr); count++){ if (count % 10 == 0) encryptstr[count] -= 22; if (count % 9 == 0) encryptstr[count] += 41; if (count % 8 == 0) encryptstr[count] -= 35; if (count % 7 == 0) encryptstr[count] += 15; if (count % 6 == 0) encryptstr[count] -= 75; if (count % 5 == 0) encryptstr[count] += 85; if (count % 4 == 0) encryptstr[count] -= 62; if (count % 3 == 0) encryptstr[count] += 71; if (count % 2 == 0) encryptstr[count] -= 55; encryptstr[count] += 21; } } BOOL CALLBACK EnDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { HWND idcode; long int count; long int TextLength; int KeyCount; char* Buffer; char* Key; switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDENCRYPT:{ KeyCount = 0; idcode = GetDlgItem(hEncrypt,ID_CODE); TextLength = GetWindowTextLength(hEditz); if (TextLength == 0){ MessageBox(hwnd, "No text to encrypt.", "Error", MB_OK | MB_ICONERROR); break; } Buffer = (char*)GlobalAlloc(GPTR, (TextLength+1)); GetWindowText(hEditz, Buffer, (TextLength+1)); Buffer[(TextLength+1)] = 0; Key = (char*)GlobalAlloc(GPTR, (GetWindowTextLength(idcode)+1)); GetWindowText(idcode,Key,GetWindowTextLength(idcode)+1); Key[(GetWindowTextLength(idcode)+1)] = 0; for (count = 0; count <= GetWindowTextLength(idcode); count++) KeyCount += Key[count]; encryptkey(Buffer,KeyCount); SetWindowText(hEditz, Buffer); MessageBox(hwnd, "File Encrypted!", windowname, MB_OK | MB_ICONEXCLAMATION); break; } case IDDECRYPT:{ KeyCount = 0; idcode = GetDlgItem(hEncrypt,ID_CODE); TextLength = GetWindowTextLength(hEditz); if (TextLength == 0){ MessageBox(hwnd, "No text to decrypt.", "Error", MB_OK | MB_ICONERROR); break; } Buffer = (char*)GlobalAlloc(GPTR, (TextLength+1)); GetWindowText(hEditz, Buffer, (TextLength+1)); Buffer[(TextLength+1)] = 0; Key = (char*)GlobalAlloc(GPTR, (GetWindowTextLength(idcode)+1)); GetWindowText(idcode,Key,GetWindowTextLength(idcode)+1); Key[(GetWindowTextLength(idcode)+1)] = 0; for (count = 0; count <= GetWindowTextLength(idcode); count++) KeyCount += Key[count]; decryptkey(Buffer,KeyCount); SetWindowText(hEditz, Buffer); MessageBox(hwnd, "File Decrypted!", windowname, MB_OK | MB_ICONEXCLAMATION); break; } } break; case WM_CLOSE: ShowWindow(hEncrypt, SW_HIDE); isshowing = false; break; default: return FALSE; } return TRUE; } BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName) { HANDLE hFile; BOOL bSuccess = FALSE; hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); if(hFile != INVALID_HANDLE_VALUE) { DWORD dwFileSize; dwFileSize = GetFileSize(hFile, NULL); if(dwFileSize != 0xFFFFFFFF) { LPSTR pszFileText; pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1); if(pszFileText != NULL) { DWORD dwRead; if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL)) { pszFileText[dwFileSize] = 0; // Add null terminator if(SetWindowText(hEdit, pszFileText)){ strcpy(strname,windowname); strcat(strname," - "); strcat(strname,pszFileName); SetWindowText(hwnd, strname); bSuccess = TRUE; // It worked! } } GlobalFree(pszFileText); } } CloseHandle(hFile); } return bSuccess; } BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName) { HANDLE hFile; BOOL bSuccess = FALSE; hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile != INVALID_HANDLE_VALUE) { DWORD dwTextLength; dwTextLength = GetWindowTextLength(hEdit); // No need to bother if there's no text. if(dwTextLength > 0) { LPSTR pszText; DWORD dwBufferSize = dwTextLength + 1; pszText = (LPSTR)GlobalAlloc(GPTR, dwBufferSize); if(pszText != NULL) { if(GetWindowText(hEdit, pszText, dwBufferSize)) { DWORD dwWritten; if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)){ strcpy(strname,windowname); strcat(strname," - "); strcat(strname,pszFileName); SetWindowText(hwnd, strname); bSuccess = TRUE; } } GlobalFree(pszText); } } CloseHandle(hFile); } return bSuccess; } Due to length, continued in the next post.
September 12, 200519 yr Author LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND hEdit; long int TextLength; long int BufferSize; char* test; hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT); switch(msg) { case WM_CREATE:{ HMENU hMenu, hSubMenu; HFONT hfDefault; HWND hEdit; hMenu = CreateMenu(); hSubMenu = CreatePopupMenu(); AppendMenu(hSubMenu, MF_STRING, ID_FILE_NEW, "&New"); AppendMenu(hSubMenu, MF_STRING, ID_FILE_SAVE, "&Save"); AppendMenu(hSubMenu, MF_STRING, ID_FILE_LOAD, "&Load"); AppendMenu(hSubMenu, MF_STRING, ID_FILE_EXIT, "E&xit"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&File"); hSubMenu = CreatePopupMenu(); AppendMenu(hSubMenu, MF_STRING, ID_ENCRYPTION_ENCRYPT, "&General Encryption"); AppendMenu(hSubMenu, MF_STRING, ID_ENCRYPTION_DECRYPT, "&General Decryption"); AppendMenu(hSubMenu, MF_STRING, ID_ENCRYPTION_ENCRYPT_KEY, "&Key Encryption"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Encryption"); hSubMenu = CreatePopupMenu(); AppendMenu(hSubMenu, MF_STRING, ID_ABOUT_ABOUT, "&About"); AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&About"); SetMenu(hwnd, hMenu); hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL); if(hEdit == NULL) MessageBox(hwnd, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR); else hEditz = hEdit; hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT); hEncrypt = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ENCRYPT), hwnd, EnDlgProc); isshowing = false; SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0)); } break; case WM_SIZE:{ RECT rcClient; GetClientRect(hwnd, &rcClient); SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER); } break; case WM_COMMAND: switch(LOWORD(wParam)) { case ID_FILE_EXIT: PostMessage(hwnd, WM_CLOSE, 0, 0); break; case ID_FILE_NEW: SetWindowText(hEdit, "\0"); break; case ID_FILE_SAVE:{ OPENFILENAME ofn; char szFileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT; ofn.lpstrDefExt = "txt"; if(GetSaveFileName(&ofn)) { SaveTextFileFromEdit(hEdit, ofn.lpstrFile); } } break; case ID_FILE_LOAD:{ OPENFILENAME ofn; char szFileName[MAX_PATH] = ""; ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrDefExt = "txt"; if(GetOpenFileName(&ofn)) { LoadTextFileToEdit(hEdit, ofn.lpstrFile); } } break; case ID_ENCRYPTION_ENCRYPT:{ long int count; TextLength = GetWindowTextLength(hEdit); if (TextLength == 0){ MessageBox(hwnd, "No text to encrypt.", "Error", MB_OK | MB_ICONERROR); break; } BufferSize = (TextLength + 1); test = (char*)GlobalAlloc(GPTR, BufferSize); GetWindowText(hEdit, test, BufferSize); test[bufferSize] = 0; encrypt(test); SetWindowText(hEdit, test); MessageBox(hwnd, "File Encrypted!", windowname, MB_OK | MB_ICONEXCLAMATION); } break; case ID_ENCRYPTION_ENCRYPT_KEY:{ if (isshowing){ ShowWindow(hEncrypt, SW_HIDE); isshowing = false; } else{ ShowWindow(hEncrypt, SW_SHOW); isshowing = true; } break; } break; case ID_ENCRYPTION_DECRYPT:{ long int count; TextLength = GetWindowTextLength(hEdit); if (TextLength == 0){ MessageBox(hwnd, "No text to decrypt.", "Error", MB_OK | MB_ICONERROR); break; } BufferSize = TextLength + 1; test = (char*)GlobalAlloc(GPTR, BufferSize); GetWindowText(hEdit, test, BufferSize); test[bufferSize] = 0; decrypt(test); SetWindowText(hEdit, test); MessageBox(hwnd, "File Decrypted!", windowname, MB_OK | MB_ICONEXCLAMATION); } break; case ID_ABOUT_ABOUT: MessageBox(NULL, "A simple text editor, with an encryption\nfunction, created by Eriond.", windowname, MB_ICONQUESTION | MB_OK); break; } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, windowname, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } resource.rc #include <windows.h> #include "resource.h" IDD_ENCRYPT DIALOG DISCARDABLE 0, 0, 160, 50 STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION "Notepad Encryption" FONT 8, "MS Sans Serif" BEGIN EDITTEXT ID_CODE, 10, 20, 100, 15 DEFPUSHBUTTON "&Encrypt",IDENCRYPT,120,16,30,10 PUSHBUTTON "&Decrypt",IDDECRYPT,120,27,30,10 GROUPBOX "Encryption Key",IDC_STATIC,5,5,150,40 END Resource.h #define IDC_STATIC -1 #define IDD_ENCRYPT 10 #define ID_CODE 11 #define IDC_MAIN_EDIT 101 #define IDC_MAIN_ENCRYPT 102 #define ID_FILE_EXIT 1000 #define ID_FILE_SAVE 1001 #define ID_FILE_LOAD 1002 #define ID_FILE_NEW 1003 #define ID_ABOUT_ABOUT 3000 #define ID_ENCRYPTION_ENCRYPT 2000 #define ID_ENCRYPTION_DECRYPT 2001 #define ID_ENCRYPTION_ENCRYPT_KEY 2002 #define IDENCRYPT 100 #define IDDECRYPT 101 Anyway. It's basically notepad, but there's an encrypt function. You can either encrypt with or without a key. I know, it's not very complex, but common'.... it's my first app :)
September 12, 200519 yr Author Anyway. It's basically notepad, but there's an encrypt function. You can either encrypt with or without a key. I know, it's not very complex, but common'.... it's my first app :).
September 12, 200519 yr That's a nice first project. It's actully pretty neat. Not really that sloppy. Just kind of jumbled around. Good work, keep on contributing.