⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sprashdlg.cpp

📁 aes win prog. text encryption
💻 CPP
字号:
// sprashDlg.cpp : implementation file

#include "stdafx.h"
#include "sprash.h"
#include "sprashDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSprashDlg dialog

CSprashDlg::CSprashDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSprashDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSprashDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);	
}

void CSprashDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSprashDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSprashDlg, CDialog)
	//{{AFX_MSG_MAP(CSprashDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
	ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSprashDlg message handlers

BOOL CSprashDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSprashDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CSprashDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSprashDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CSprashDlg::OnOK() 
{
	// TODO: Add extra validation here
	CWnd * temp;
	CWnd * key;

	temp = GetDlgItem(IDC_PLAIN);
	temp->SetWindowText ("I gEeK tHeReFoRe I aM !");

	key = GetDlgItem(IDC_KEY);
	key->SetWindowText("1234567890123456");

	CWnd *cipher,*result, *msgbox;

	cipher = GetDlgItem(IDC_CIPHER);
	result= GetDlgItem(IDC_RESULT);
	msgbox = GetDlgItem(IDC_MSGBOX);

	cipher->SetWindowText("");
	result->SetWindowText("");
	msgbox->SetWindowText("");


}

void CSprashDlg::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;
}

void CSprashDlg::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;
	}
}



void CSprashDlg::CharStr2HexStr(const char *pucCharStr, char *pszHexStr, int iSize)
{
	int i;
	char szHex[3];
	pszHexStr[0] = 0;
	for(i=0; i<iSize; i++)
	{
		Char2Hex(pucCharStr[i], szHex);
		strcat(pszHexStr, szHex);
	}
}

void CSprashDlg::HexStr2CharStr(const char *pszHexStr, char *pucCharStr, int iSize)
{
	int i;
	unsigned char ch;
	for(i=0; i<iSize; i++)
	{
		Hex2Char(pszHexStr+2*i, ch);
		pucCharStr[i] = ch;
	}
}

void CSprashDlg::OnEncrypt() 
{
	// TODO: Add your control notification handler code here
	
	 h = 0;

tag:

	CWnd *input, *output, *result; 
	CWnd *cipher, *msgbox;
	CString  key,text, message;
	char buf[300],hex[600]; 
		
	memset(buf,0,300);
	memset(hex,0,600);


	// Get the pointers
	input = GetDlgItem(IDC_PLAIN);
	output = GetDlgItem(IDC_KEY);
	cipher = GetDlgItem(IDC_CIPHER);
	msgbox = GetDlgItem(IDC_MSGBOX);
	result = GetDlgItem(IDC_RESULT);

	result->SetWindowText("");
	// Get the window Text
	output->GetWindowText(key);
	input->GetWindowText(text);

	int flag = 0;
	if(key.GetLength()==0)
	{
		output->SetWindowText("1234567890123456");
		msgbox->SetWindowText("Default Value of key used");
	}

	if(text.GetLength() == 0){
		input->SetWindowText("I gEeK tHeReFoRe I aM !");
	
		output->GetWindowText(key);
      	input->GetWindowText(text);
		flag = 1;
	}

	

	// Initilalize the object
	rijndael.MakeKey ((LPCTSTR)key);

	int length = text.GetLength();

	
	if((text.GetLength() % 16) != 0)
	{
		
		// pad up the text
		while((text.GetLength() % 16) != 0)
		{
			text.Insert(length,'0');
			length++;
		}
		if (flag == 0)
			msgbox->SetWindowText("Zeroes have been padded to make the length of string divisible by 16 ");
		else
			
			msgbox->SetWindowText("I'm taking default values since you provided none. Note: Zeroes have been padded to make the length of string divisible by 16");
	}	

	// Encrypt the string
	rijndael.Encrypt((LPCTSTR)text, buf, text.GetLength());

	// convert to hex and display
	CharStr2HexStr(buf, hex, strlen(buf));
	cipher->SetWindowText(hex);

	if (h == 0)
	{ 
		h=1; 
		goto tag;
	}

	memset(buf,0,300);
	memset(hex,0,600);

}

void CSprashDlg::OnDecrypt() 
{
	// TODO: Add your control notification handler code here
	CWnd *key,*cipher,*result, *msgbox, *plain;
	CString key_text, cipher_text;
	char buf[300],buf1[300];
	
	key_text = '\0';
	cipher_text = '\0';
	key = NULL;

	memset(buf,0,300);
	memset(buf1,0,300);



	// Get the pointers
	key = GetDlgItem(IDC_KEY);
	cipher = GetDlgItem(IDC_CIPHER);
	result= GetDlgItem(IDC_RESULT);
	msgbox = GetDlgItem(IDC_MSGBOX);
	plain = GetDlgItem(IDC_PLAIN);

	plain->SetWindowText("");

	// read the key and cipher text
	key->GetWindowText(key_text);
	cipher->GetWindowText(cipher_text);
	
	if( cipher_text.GetLength()==0){
		cipher->SetWindowText("C9377FD2C4D57AA3BD84B247EF0995264CB4378A4E0EA21DA98B7016F64B121C");
		cipher->GetWindowText(cipher_text);
	}


	if(key_text.GetLength() == 0 ){
		msgbox->SetWindowText("Ciphertext or Key, decrypting using default values");
		plain->SetWindowText("");
		key->SetWindowText("1234567890123456");
		key->GetWindowText(key_text);
		
	}

    
	
	rijndael.MakeKey ((LPCTSTR)key_text);

	// decrypt them
	HexStr2CharStr((LPCTSTR)cipher_text, buf1, cipher_text.GetLength ());

	rijndael.Decrypt((LPCTSTR)buf1, buf, strlen(buf1));

	//convert and display
	result->SetWindowText(buf);
	
	memset(buf,0,300);
	memset(buf1,0,300);


}

void CSprashDlg::OnButton2() 
{
	CWnd *plain, *key,*cipher,*result, *msgbox;

	plain = GetDlgItem(IDC_PLAIN);
	key = GetDlgItem(IDC_KEY);
	cipher = GetDlgItem(IDC_CIPHER);
	result= GetDlgItem(IDC_RESULT);
	msgbox = GetDlgItem(IDC_MSGBOX);

	plain->SetWindowText("");
	key->SetWindowText("");
	cipher->SetWindowText("");
	result->SetWindowText("");
	msgbox->SetWindowText("");
	h = 0;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -