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

📄 keyworddlg.cpp

📁 1、对于凯撒密文
💻 CPP
字号:
// KeywordDlg.cpp : implementation file
//

#include "stdafx.h"
#include "030300816.h"
#include "KeywordDlg.h"
#include "resource.h"

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

/////////////////////////////////////////////////////////////////////////////
// CKeywordDlg dialog

CKeywordDlg::CKeywordDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKeywordDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKeywordDlg)
	m_cipherFile = _T("");
	m_cipherString = _T("");
	m_plainFile = _T("");
	m_plainString = _T("");
	m_radio = 0;
	m_key = _T("a");
	m_at = _T("a");
	m_iMethod = 0;
	m_suba = _T("a");
	m_subb = _T("b");
	m_subc = _T("c");
	m_subd = _T("d");
	m_sube = _T("e");
	m_subf = _T("f");
	m_subg = _T("g");
	m_subh = _T("h");
	m_subi = _T("i");
	m_subj = _T("j");
	m_subk = _T("k");
	m_subl = _T("l");
	m_subm = _T("m");
	m_subn = _T("n");
	m_subo = _T("o");
	m_subp = _T("p");
	m_subq = _T("q");
	m_subr = _T("r");
	m_subs = _T("s");
	m_subt = _T("t");
	m_subu = _T("u");
	m_subv = _T("v");
	m_subw = _T("w");
	m_subx = _T("x");
	m_suby = _T("y");
	m_subz = _T("z");
	//}}AFX_DATA_INIT
}


void CKeywordDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKeywordDlg)
	DDX_Text(pDX, IDC_CIPHER_FILE, m_cipherFile);
	DDX_Text(pDX, IDC_CIPHER_STRING, m_cipherString);
	DDX_Text(pDX, IDC_PLAIN_FILE, m_plainFile);
	DDX_Text(pDX, IDC_PLAIN_STRING, m_plainString);
	DDX_Radio(pDX, IDC_RADIO_STRING, m_radio);
	DDX_Text(pDX, IDC_KEY, m_key);
	DDX_Text(pDX, IDC_AT, m_at);
	DDV_MaxChars(pDX, m_at, 1);
	DDX_Text(pDX, IDC_SUBA, m_suba);
	DDX_Text(pDX, IDC_SUBB, m_subb);
	DDX_Text(pDX, IDC_SUBC, m_subc);
	DDX_Text(pDX, IDC_SUBD, m_subd);
	DDX_Text(pDX, IDC_SUBE, m_sube);
	DDX_Text(pDX, IDC_SUBF, m_subf);
	DDX_Text(pDX, IDC_SUBG, m_subg);
	DDX_Text(pDX, IDC_SUBH, m_subh);
	DDX_Text(pDX, IDC_SUBI, m_subi);
	DDX_Text(pDX, IDC_SUBJ, m_subj);
	DDX_Text(pDX, IDC_SUBK, m_subk);
	DDX_Text(pDX, IDC_SUBL, m_subl);
	DDX_Text(pDX, IDC_SUBM, m_subm);
	DDX_Text(pDX, IDC_SUBN, m_subn);
	DDX_Text(pDX, IDC_SUBO, m_subo);
	DDX_Text(pDX, IDC_SUBP, m_subp);
	DDX_Text(pDX, IDC_SUBQ, m_subq);
	DDX_Text(pDX, IDC_SUBR, m_subr);
	DDX_Text(pDX, IDC_SUBS, m_subs);
	DDX_Text(pDX, IDC_SUBT, m_subt);
	DDX_Text(pDX, IDC_SUBU, m_subu);
	DDX_Text(pDX, IDC_SUBV, m_subv);
	DDX_Text(pDX, IDC_SUBW, m_subw);
	DDX_Text(pDX, IDC_SUBX, m_subx);
	DDX_Text(pDX, IDC_SUBY, m_suby);
	DDX_Text(pDX, IDC_SUBZ, m_subz);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CKeywordDlg, CDialog)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CKeywordDlg)
	ON_BN_CLICKED(IDC_RADIO_STRING, OnRadioString)
	ON_BN_CLICKED(IDC_RADIO_FILE, OnRadioFile)
	ON_BN_CLICKED(IDC_ENCIPHER, OnEncipher)
	ON_BN_CLICKED(IDC_DECIPHER, OnDecipher)
	ON_BN_CLICKED(IDC_OPEN_PLAIN, OnOpenPlain)
	ON_BN_CLICKED(IDC_OPEN_CIPHER, OnOpenCipher)
	ON_WM_SETCURSOR()
	ON_BN_CLICKED(IDC_SETKEY, OnSetkey)
	ON_WM_CTLCOLOR()
	ON_WM_CANCELMODE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKeywordDlg message handlers

BOOL CKeywordDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.
	{
		// Let the ToolTip process this message.
		m_tooltip.RelayEvent(pMsg);
	}
	return CDialog::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}
BOOL CKeywordDlg::OnInitDialog()
{
	CDialog::OnInitDialog();	// CG: This was added by the ToolTips component.
	// CG: The following block was added by the ToolTips component.
	{
		// Create the ToolTip control.
		for ( int i = 0 ; i < 200 ; i ++ ) crib[i] = 0;
		for ( i = 0 ; i < 200 ; i ++ ) sub[i] = 0;
		for ( i = 'a' ; i <= 'z' ; i ++ ) sub[i] = i;
		for ( i = 0 ; i < 26 ; i ++ ) key += ('a'+i);
		m_tooltip.Create(this);
		m_tooltip.Activate(TRUE);
		ShowStringGroup(true);
		ShowFileGroup(false);
		ShowKey();
		m_hArrow = AfxGetApp()->LoadCursor(IDC_MYARROW);
		m_hHand = AfxGetApp()->LoadCursor(IDC_MYHAND);
		m_hBeam = AfxGetApp()->LoadCursor(IDC_MYBEAM);
		m_hPen = AfxGetApp()->LoadCursor(IDC_MYPEN);
		m_hMove = AfxGetApp()->LoadCursor(IDC_MYMOVE);
		// TODO: Use one of the following forms to add controls:
		// m_tooltip.AddTool(GetDlgItem(IDC_<name>), <string-table-id>);
		// m_tooltip.AddTool(GetDlgItem(IDC_<name>), "<text>");
	}
//	pSkin2->ApplySkin((long)m_hWnd);
	return TRUE;	// CG: This was added by the ToolTips component.
}

void CKeywordDlg::OnRadioString() 
{
	// TODO: Add your control notification handler code here
	ShowStringGroup(true);
	ShowFileGroup(false);
	m_iMethod = 0;
}

void CKeywordDlg::ShowStringGroup(bool bShow)
{
	CWnd* poWnd;
	poWnd = GetDlgItem(IDC_STATIC_STRING1);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_STRING2);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_STRING3);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_PLAIN_STRING);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_CIPHER_STRING);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
}

void CKeywordDlg::ShowFileGroup(bool bShow)
{
	CWnd* poWnd;
	poWnd = GetDlgItem(IDC_STATIC_FILE1);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_FILE2);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_STATIC_FILE3);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_PLAIN_FILE);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_CIPHER_FILE);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_OPEN_PLAIN);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
	poWnd = GetDlgItem(IDC_OPEN_CIPHER);
	poWnd->EnableWindow(bShow ? SW_SHOW : SW_HIDE);
}

void CKeywordDlg::OnRadioFile() 
{
	// TODO: Add your control notification handler code here
	ShowStringGroup(false);
	ShowFileGroup(true);
	m_iMethod = 1;
}

void CKeywordDlg::OnEncipher() 
{
	// TODO: Add your control notification handler code here
	if ( 0 == m_iMethod )
	{
		EncipherString();
	}
	else // File
	{
		EncipherFile();
	}
}

void CKeywordDlg::EncipherString()
{
	UpdateData();
	CKeyword caeser;
	caeser.m_plainString = m_plainString;
	caeser.m_key = key;
	caeser.Encipher();
//	m_plainString = "";
	m_cipherString = caeser.m_cipherString;
	UpdateData(FALSE);
}

void CKeywordDlg::EncipherFile()
{
	UpdateData();
	if (m_cipherFile=="" || m_plainFile=="")
	{
		MessageBox("Please set file path correctly!","Error",MB_ICONSTOP);
		return;
	}
	CFile rfile(
		m_plainFile,
		CFile::modeRead 
		);
	char *pBuf;
	DWORD dwFileLen;
	dwFileLen=rfile.GetLength();
	pBuf=new char[dwFileLen+1];
	pBuf[dwFileLen]=0;
	rfile.Read(pBuf,dwFileLen);
	rfile.Close();
	CKeyword caeser;
	caeser.m_plainString = pBuf;
	caeser.m_key = key;
	caeser.Encipher();
	int length = caeser.m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, caeser.m_cipherString, length -1);
	CFile wfile(m_cipherFile,CFile::modeCreate | CFile::modeWrite);
	wfile.Write(cipherBlock,length);
	wfile.Close();
	CString succeed = "File encryption succeed!\nThe enciphed file is saved at\n" + 
		m_cipherFile + "\n\npreview:\n" + cipherBlock;
	MessageBox(succeed,"Encrption finish ^o^",MB_ICONINFORMATION);
	UpdateData(FALSE);
}

void CKeywordDlg::OnDecipher() 
{
	// TODO: Add your control notification handler code here
	if ( 0 == m_iMethod )
	{
		DecipherString();
	}
	else // File
	{
		DecipherFile();
	}
}

void CKeywordDlg::DecipherString()
{
	UpdateData();
	CKeyword caeser;
	caeser.m_cipherString = m_cipherString;
	caeser.m_key = key;
	caeser.Decipher();
	m_plainString = caeser.m_plainString;
//	m_cipherString = "";
	UpdateData(FALSE);
}

void CKeywordDlg::DecipherFile()
{
	UpdateData();
	if (m_cipherFile=="" || m_plainFile=="")
	{
		MessageBox("Please set file path correctly!","Error",MB_ICONSTOP);
		return;
	}
	CFile rfile(
		m_plainFile,
		CFile::modeRead 
		);
	char *pBuf;
	DWORD dwFileLen;
	dwFileLen=rfile.GetLength();
	pBuf=new char[dwFileLen+1];
	pBuf[dwFileLen]=0;
	rfile.Read(pBuf,dwFileLen);
	rfile.Close();
	CKeyword caeser;
	caeser.m_cipherString = pBuf;
	caeser.m_key = key;
	caeser.Decipher();
	int length = caeser.m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, caeser.m_plainString, length -1);
	CFile wfile(m_cipherFile,CFile::modeCreate | CFile::modeWrite);
	wfile.Write(cipherBlock,length);
	wfile.Close();
	CString succeed = "File Decryption succeed!\nThe deciphed file is saved at\n" + 
		m_cipherFile + "\n\npreview:\n" + cipherBlock;
	MessageBox(succeed,"Decrption finish ^o^",MB_ICONINFORMATION);
	UpdateData(FALSE);
}

void CKeywordDlg::OnOpenPlain() 
{
	// TODO: Add your control notification handler code here
	CFileDialog oFileOpen(TRUE);
	oFileOpen.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	oFileOpen.m_ofn.lpstrDefExt="txt";
	CString oStrDir;
	if(IDOK == oFileOpen.DoModal())
	{
		CString oStrFilePath = oFileOpen.GetPathName();
		TCHAR szDir[MAX_PATH+1];
		_tcscpy(szDir, LPCTSTR(oStrFilePath));
		oStrFilePath = CString(szDir);
		SetDlgItemText(
			IDC_PLAIN_FILE,
			oStrFilePath
			);
	}
}

void CKeywordDlg::OnOpenCipher() 
{
	// TODO: Add your control notification handler code here
	CFileDialog oFileOpen(TRUE);
	oFileOpen.m_ofn.lpstrFilter = "Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
	oFileOpen.m_ofn.lpstrDefExt="txt";
	CString oStrDir;
	if(IDOK == oFileOpen.DoModal())
	{
		CString oStrFilePath = oFileOpen.GetPathName();
		TCHAR szDir[MAX_PATH+1];
		_tcscpy(szDir, LPCTSTR(oStrFilePath));
		oStrFilePath = CString(szDir);
		SetDlgItemText(
			IDC_CIPHER_FILE,
			oStrFilePath
			);
	}
}


void CKeywordDlg::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	CDialog::CalcWindowRect(lpClientRect, nAdjustType);
}

BOOL CKeywordDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default
	if ( pWnd == GetDlgItem(IDC_ENCIPHER) 
		|| pWnd == GetDlgItem(IDC_DECIPHER)
		|| pWnd == GetDlgItem(IDC_OPEN_PLAIN)
		|| pWnd == GetDlgItem(IDC_OPEN_CIPHER)
		|| pWnd == GetDlgItem(IDC_SETKEY)
		)
		::SetCursor(m_hHand);
	else if ( pWnd == GetDlgItem(IDC_PLAIN_STRING)
		|| pWnd == GetDlgItem(IDC_CIPHER_STRING)
		|| pWnd == GetDlgItem(IDC_KEY)
		|| pWnd == GetDlgItem(IDC_PLAIN_FILE)
		|| pWnd == GetDlgItem(IDC_CIPHER_FILE)
		)
		::SetCursor(m_hBeam);
	else if ( pWnd == GetDlgItem(IDC_RADIO_STRING)
		|| pWnd == GetDlgItem(IDC_RADIO_FILE)
		)
		::SetCursor(m_hPen);
	else if ( nHitTest == HTCAPTION )
		::SetCursor(m_hMove);
	else
		::SetCursor(m_hArrow);
	return true;
	return CDialog::OnSetCursor(pWnd, nHitTest, message);
}

void CKeywordDlg::OnSetkey() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	for ( int j = 0 ; j < 200 ; j ++ ) crib[j] = 0;
	if(!CheckKeyBox())return;
	CString m_tempKey = m_key;
	kk=_T("");
	char temp;
	m_tempKey.MakeLower();
	int length = m_tempKey.GetLength();
	for ( int i = 0 ; i < length ; i ++ )
	{
		temp = m_tempKey.GetAt(i);
		if ( !crib[temp] )
		{
			crib[temp] = 1;
			kk += temp;
		}
	}
	ShowSubstitution(m_at,kk);
	ShowKey();
	UpdateData(false);
}

void CKeywordDlg::ShowSubstitution(CString m_at, CString &kk)
{
	int length = kk.GetLength();
	CString tempAt = m_at;
	tempAt.MakeLower();
	char at = tempAt.GetAt(0);
	char anotherAt = at;
	bool tempSub[200];
	for ( int k = 0 ; k < 200 ; k ++ ) tempSub[k] = 0;
	for ( k = 'a' ; k <= 'z' ; k ++ ) tempSub[k] = 1;
	for ( int i = 0 ; i < length ; i ++ , at ++)
	{
		sub[(at-'a')%26+'a'] = kk.GetAt(i);
		tempSub[kk.GetAt(i)] = 0;
	}
	for ( i = 0 ; i < (26-length) ; i ++ )
	{
		k = 'a';
		while(tempSub[k]==0) k++;
		tempSub[k] = 0;
		sub[(anotherAt+length-'a'+i+26)%26+'a'] = k;
	}
	key = _T("");
	for ( i = 'a';i<='z';i++)
		key += sub[i];
}

void CKeywordDlg::ShowKey()
{
	//UpdateData();
	m_suba = sub['a'];
	m_subb = sub['b'];
	m_subc = sub['c'];
	m_subd = sub['d'];
	m_sube = sub['e'];
	m_subf = sub['f'];
	m_subg = sub['g'];
	m_subh = sub['h'];
	m_subi = sub['i'];
	m_subj = sub['j'];
	m_subk = sub['k'];
	m_subl = sub['l'];
	m_subm = sub['m'];
	m_subn = sub['n'];
	m_subo = sub['o'];
	m_subp = sub['p'];
	m_subq = sub['q'];
	m_subr = sub['r'];
	m_subs = sub['s'];
	m_subt = sub['t'];
	m_subu = sub['u'];
	m_subv = sub['v'];
	m_subw = sub['w'];
	m_subx = sub['x'];
	m_suby = sub['y'];
	m_subz = sub['z'];
	UpdateData(false);
}

bool CKeywordDlg::CheckKeyBox()
{
	if (m_key == "" || m_at == "")
	{
		AfxMessageBox("please set key correctly");
		return false;
	}
	else
		return true;
}

HBRUSH CKeywordDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if (pWnd->GetDlgCtrlID() == IDC_SUBZ
		|| pWnd->GetDlgCtrlID() == IDC_SUBA
		|| pWnd->GetDlgCtrlID() == IDC_SUBB
		|| pWnd->GetDlgCtrlID() == IDC_SUBC
		|| pWnd->GetDlgCtrlID() == IDC_SUBD
		|| pWnd->GetDlgCtrlID() == IDC_SUBE
		|| pWnd->GetDlgCtrlID() == IDC_SUBF
		|| pWnd->GetDlgCtrlID() == IDC_SUBG
		|| pWnd->GetDlgCtrlID() == IDC_SUBH
		|| pWnd->GetDlgCtrlID() == IDC_SUBI
		|| pWnd->GetDlgCtrlID() == IDC_SUBJ
		|| pWnd->GetDlgCtrlID() == IDC_SUBK
		|| pWnd->GetDlgCtrlID() == IDC_SUBL
		|| pWnd->GetDlgCtrlID() == IDC_SUBM
		|| pWnd->GetDlgCtrlID() == IDC_SUBN
		|| pWnd->GetDlgCtrlID() == IDC_SUBO
		|| pWnd->GetDlgCtrlID() == IDC_SUBP
		|| pWnd->GetDlgCtrlID() == IDC_SUBQ
		|| pWnd->GetDlgCtrlID() == IDC_SUBR
		|| pWnd->GetDlgCtrlID() == IDC_SUBS
		|| pWnd->GetDlgCtrlID() == IDC_SUBT
		|| pWnd->GetDlgCtrlID() == IDC_SUBU
		|| pWnd->GetDlgCtrlID() == IDC_SUBV
		|| pWnd->GetDlgCtrlID() == IDC_SUBW
		|| pWnd->GetDlgCtrlID() == IDC_SUBX
		|| pWnd->GetDlgCtrlID() == IDC_SUBY
		)
	{
		// Set the text color to red
		pDC->SetTextColor(RGB(255, 0, 0));
		
		// Set the background mode for text to transparent 
		// so background will show thru.
		pDC->SetBkMode(TRANSPARENT);
		
		// Return handle to our CBrush object
		hbr = m_brush;
	}
	// TODO: Return a different brush if the default is not Ideaired
	return hbr;
}

void CKeywordDlg::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

⌨️ 快捷键说明

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