简单异或加密实现

写着玩的,输入一个加密密码,程序用这个密码对目标文件进行异或加密,再次异或就解密了

core.h

#ifndef CORE_H
#define CORE_H

#include <windows.h>
#include <commdlg.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

bool code(TCHAR filename[], char passwd[])
{
	char c;
	fstream file(filename,ios::out|ios::in);
	if (!file)
	{
		MessageBox(NULL,TEXT("文件打开失败"),TEXT("错误"),0);
		return false;
	}

	while(file>>c)
	{
	    file.seekg( -1, ios_base::cur );//将文件指针前移一个位置
		c^=atoi(passwd);
	    file<<c;
		file.seekg( 1, ios_base::cur );
	    //file.sync();//sync buffer
	}

	if(file.bad()) 
	{
		MessageBox(NULL,TEXT("文件流损坏"),TEXT("错误"),0); 
		return false;
	}
	file.close();
	return true;
}

#endif


 

#include "core.h"
#include "resource.h"

BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK AboutDlgProc2(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);


BOOL PopFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName);
void PopFileInitialize (HWND hwnd);
BOOL PopFileRead (HWND hwndEdit, PTSTR pstrFileName);

HINSTANCE hInst;
char passwd[15];

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	//AllocConsole();
	//freopen("CONOUT$","w",stdout);
	hInst = hInstance;
	DialogBoxParam (hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DialogProc, 0);
	return 1;
}

BOOL CALLBACK DialogProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static TCHAR     szFileName[MAX_PATH], szTitleName[MAX_PATH] ;
	static HWND hwndEdit;

	bool haveFile	= false;
	char file[MAX_PATH];
	int i;
	HBRUSH hBrush ;
    //BITMAP  bitmap;
	static HBITMAP hBitmap;
	HDC hdc, hdcMem;
	PAINTSTRUCT ps;

	switch (message)
	{
		case WM_INITDIALOG:
			//SetWindowTextA (GetDlgItem (hDlg, IDC_INPUT), "0");
			PopFileInitialize (hwnd);
			hwndEdit	= GetDlgItem (hwnd, IDC_EDIT1);

			hBitmap	= LoadBitmap(hInst, L"BitBlt");
			//GetObject (hBitmap, sizeof (bitmap), &bitmap);
			return true;

		case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);
			hdcMem = CreateCompatibleDC(hdc);
			SelectObject(hdcMem, hBitmap);
			BitBlt (hdc, 0, 0, 550, 400,
                       hdcMem, 0, 0, SRCCOPY) ;

			DeleteDC(hdcMem);
			EndPaint (hwnd, &ps);
			return true;

		case WM_CTLCOLORDLG:
			hBrush = CreateSolidBrush(RGB(250,250,250));
			return (BOOL)hBrush;

		case WM_COMMAND:
			switch (LOWORD (wParam))
			{
				case IDC_OPEN:
					if (PopFileOpenDlg (hwnd, szFileName, szTitleName))
					{
						SetWindowText (hwndEdit, szFileName);
						haveFile	= true;
					}
					return true;

				case IDC_BUTTON1:
					if (DialogBox (hInst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, AboutDlgProc) )
					{
						for (i = 0;file[i] != L'\0'; i++)
							if ( file[i] == L'\\')
								file[i] = L'/';

						if (code(szFileName, passwd) )
							MessageBox (hwnd, TEXT("加密成功"), TEXT("提示"), 0);
					}
					return true;

				case IDC_BUTTON2:
					if (DialogBox (hInst, MAKEINTRESOURCE(IDD_DIALOG2), hwnd, AboutDlgProc2) )
					{
						for (i = 0;file[i] != L'\0'; i++)
							if ( file[i] == L'\\')
								file[i] = L'/';

						if (code(szFileName, passwd) )
							MessageBox (hwnd, TEXT("解密成功"), TEXT("提示"), 0);
					}
					return true;

				case IDCANCEL:
					EndDialog(hwnd,0);
					return true;
			}
			return true;
	}
	return false;
}

BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, 
                            WPARAM wParam, LPARAM lParam)
{
	static HWND hwndEdit;
	TCHAR Passwd[15];
	HBRUSH hBrush;

     switch (message)
     {
     case WM_INITDIALOG :
		 hwndEdit	= GetDlgItem (hDlg, IDC_EDIT2);
		 SendMessage (hwndEdit, EM_LIMITTEXT, 8, 0L) ;
		 //SetFocus (hwndEdit);
          return TRUE ;

		case WM_CTLCOLORDLG:
			hBrush = CreateSolidBrush(RGB(255,255,255));
			return (BOOL)hBrush;
          
     case WM_COMMAND :
          switch (LOWORD (wParam))
          {
		  case IDOK :
			GetWindowText(hwndEdit, Passwd, 10);
			WideCharToMultiByte (CP_ACP, 0, (PWSTR) Passwd, -1, passwd, 
                               15, NULL, NULL) ;

			EndDialog(hDlg, 1);
			return true;

          case IDCANCEL :
			  strcpy (passwd,"");
               EndDialog (hDlg, 0) ;
               return true ;
          }
          break ;
     }
     return false ;
}

BOOL CALLBACK AboutDlgProc2(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HWND hwndEdit;
	TCHAR Passwd[15];
	HBRUSH hBrush;

     switch (message)
     {
     case WM_INITDIALOG :
		 hwndEdit	= GetDlgItem (hDlg, IDC_EDIT3);
		 SendMessage (hwndEdit, EM_LIMITTEXT, 8, 0L) ;
		 //SetFocus (hwndEdit);
          return TRUE ;

		case WM_CTLCOLORDLG:
			hBrush = CreateSolidBrush(RGB(255,255,255));
			return (BOOL)hBrush;
          
     case WM_COMMAND :
          switch (LOWORD (wParam))
          {
		  case IDOK :printf("OK");
			GetWindowText(hwndEdit, Passwd, 10);
			WideCharToMultiByte (CP_ACP, 0, (PWSTR) Passwd, -1, passwd, 
                               15, NULL, NULL) ;
			printf("%s",passwd);
			EndDialog(hDlg, 1);
			return true;

          case IDCANCEL :
			  strcpy (passwd,"");
               EndDialog (hDlg, 0) ;
               return true ;
          }
          break ;
     }
     return false ;
}


static OPENFILENAME ofn ;

void PopFileInitialize (HWND hwnd)
{
     static TCHAR szFilter[] = TEXT ("Text Files (*.TXT)\0*.txt\0")  \
                               TEXT ("ASCII Files (*.ASC)\0*.asc\0") \
                               TEXT ("All Files (*.*)\0*.*\0\0") ;
     
     ofn.lStructSize       = sizeof (OPENFILENAME) ;
     ofn.hwndOwner         = hwnd ;
     ofn.hInstance         = NULL ;
     ofn.lpstrFilter       = szFilter ;
     ofn.lpstrCustomFilter = NULL ;
     ofn.nMaxCustFilter    = 0 ;
     ofn.nFilterIndex      = 0 ;
     ofn.lpstrFile         = NULL ;          // Set in Open and Close functions
     ofn.nMaxFile          = MAX_PATH ;
     ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
     ofn.nMaxFileTitle     = MAX_PATH ;
     ofn.lpstrInitialDir   = NULL ;
     ofn.lpstrTitle        = NULL ;
     ofn.Flags             = 0 ;             // Set in Open and Close functions
     ofn.nFileOffset       = 0 ;
     ofn.nFileExtension    = 0 ;
     ofn.lpstrDefExt       = TEXT ("txt") ;
     ofn.lCustData         = 0L ;
     ofn.lpfnHook          = NULL ;
     ofn.lpTemplateName    = NULL ;
}

BOOL PopFileOpenDlg (HWND hwnd, PTSTR pstrFileName, PTSTR pstrTitleName)
{
     ofn.hwndOwner         = hwnd ;
     ofn.lpstrFile         = pstrFileName ;
     ofn.lpstrFileTitle    = pstrTitleName ;
     ofn.Flags             = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
     
     return GetOpenFileName (&ofn) ;
}


 resource.h

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by k.rc
//
#define IDD_DIALOG                      101
#define IDD_DIALOG1                     102
#define IDD_DIALOG2                     103
#define IDB_BITMAP2                     106
#define IDB_BITMAP1                     107
#define IDC_EDIT1                       1001
#define IDC_OPEN                        1003
#define IDC_BUTTON1                     1004
#define IDC_BUTTON2                     1005
#define IDC_EDIT2                       1006
#define IDC_EDIT3                       1007

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        108
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1008
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值