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

📄 rc4dlg.cpp

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

#include "stdafx.h"
#include "030300816.h"
#include "Rc4Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CRc4Dlg dialog


CRc4Dlg::CRc4Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRc4Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRc4Dlg)
	m_key[0] = 1;
	m_key[1] = 2;
	m_key[2] = 3;
	m_key[3] = 4;
	m_cycleNum = 8;
	m_keyStream = _T("");
	m_plainString = _T("");
	m_radio = 0;
	m_cipherString = _T("");
	m_plainFile = _T("");
	m_cipherFile = _T("");
	//}}AFX_DATA_INIT
}


void CRc4Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRc4Dlg)
	DDX_Control(pDX, IDC_SBOX, m_sBox);
	DDX_Control(pDX, IDC_KBOX, m_kBox);
	DDX_Text(pDX, IDC_KEY1, m_key[0]);
	DDV_MinMaxUInt(pDX, m_key[0], 0, 255);
	DDX_Text(pDX, IDC_KEY2, m_key[1]);
	DDV_MinMaxUInt(pDX, m_key[1], 0, 255);
	DDX_Text(pDX, IDC_KEY3, m_key[2]);
	DDV_MinMaxUInt(pDX, m_key[2], 0, 255);
	DDX_Text(pDX, IDC_KEY4, m_key[3]);
	DDV_MinMaxUInt(pDX, m_key[3], 0, 255);
	DDX_Text(pDX, IDC_CYCLENUM, m_cycleNum);
	DDV_MinMaxUInt(pDX, m_cycleNum, 0, 10000);
	DDX_Text(pDX, IDC_KEYSTREAM, m_keyStream);
	DDX_Text(pDX, IDC_PLAIN_STRING, m_plainString);
	DDX_Radio(pDX, IDC_RADIO_STRING, m_radio);
	DDX_Text(pDX, IDC_CIPHER_STRING, m_cipherString);
	DDX_Text(pDX, IDC_PLAIN_FILE, m_plainFile);
	DDX_Text(pDX, IDC_CIPHER_FILE, m_cipherFile);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRc4Dlg, CDialog)
	//{{AFX_MSG_MAP(CRc4Dlg)
	ON_BN_CLICKED(IDC_RADIO_STRING, OnRadioString)
	ON_BN_CLICKED(IDC_RADIO_FILE, OnRadioFile)
	ON_BN_CLICKED(IDC_SETKEY, OnSetkey)
	ON_BN_CLICKED(IDC_SHOWKEY, OnShowkey)
	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()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRc4Dlg message handlers

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

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

void CRc4Dlg::OnSetkey() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_kBox.ResetContent();
	m_sBox.ResetContent();
	InitKey();
	GetFinalSArray();
	ShowKeyStream();
	UpdateData(false);
}

void CRc4Dlg::OnShowkey() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	UINT temp = 0;
	UINT num = ( m_cycleNum - 1 ) / 8 + 1;
	CString tempStream = _T("");
	for ( UINT i = 0 ; i < num ; i ++ )
	{
		bitset<8> bit (NextRand());
		for ( int j = 7 ; j >= 0 ; j -- )
		{
			if ( bit[j] == 0 )
				tempStream += "0";
			else
				tempStream += "1";
		}
		if ( i % 2 == 1 )
			tempStream += " ";
		else
			tempStream += "";
	}
	m_keyStream = tempStream;
	UpdateData(false);
}

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

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

BOOL CRc4Dlg::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 CRc4Dlg::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.		m_tooltip.Create(this);		m_tooltip.Activate(TRUE);
		m_iStream = 0;
		m_jStream = 0;
		m_iMethod = 0;
		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);
		ShowStringGroup(true);
		ShowFileGroup(false);
		OnSetkey();		// 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 CRc4Dlg::InitKey()
{
	m_iStream = 0;
	m_jStream = 0;
	for ( int i = 0 ; i < 256 ; i ++ )
	{
		m_S[i] = i;
		m_K[i] = m_key[i%4];
	}
}

void CRc4Dlg::GetFinalSArray()
{
	UINT temp;
	for ( int j = 0 , i = 0 ; i < 256 ; i ++ )
	{
		j = ( j + m_S[i] + m_K[i] ) % 256;
		temp = m_S[i];
		m_S[i] = m_S[j];
		m_S[j] = temp;
	}
}

void CRc4Dlg::ShowKeyStream()
{
	CString temp;
	for ( int i = 0 ; i < 256 ; i ++ )
	{
		temp.FormatMessage(_T("%1!d!        %2!d!   "),i,m_K[i]);
		m_kBox.AddString(temp);
	}
	for ( i = 0 ; i < 256 ; i ++ )
	{
		temp.FormatMessage(_T("%1!d!        %2!d!   "),i,m_S[i]);
		m_sBox.AddString(temp);
	}
	UpdateData(false);
}

UINT CRc4Dlg::NextRand()
{
	UINT t;
	m_iStream = ( m_iStream + 1 ) % 256;
	m_jStream = ( m_jStream + m_S[m_iStream] ) % 256;
	t = m_S[m_iStream]; m_S[m_iStream] = m_S[m_jStream]; m_S[m_jStream] = t;
    t = ( m_S[m_iStream] + m_S[m_jStream] ) % 256;
	return m_S[t];
}

void CRc4Dlg::EncipherString()
{
	UpdateData();
	OnSetkey();
	CRc4 rc4;
	for ( int i = 0 ; i < 256 ; i ++ )
	{
		rc4.m_K[i] = m_K[i];
		rc4.m_S[i] = m_S[i];
	}
	rc4.m_plainString = m_plainString;
	rc4.Encipher();
	m_plainString = "";
	m_cipherString = rc4.m_cipherString;
	UpdateData(false);
}

void CRc4Dlg::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();
	OnSetkey();
	CRc4 rc4;
	for ( int i = 0 ; i < 256 ; i ++ )
	{
		rc4.m_K[i] = m_K[i];
		rc4.m_S[i] = m_S[i];
	}
	rc4.m_plainString = pBuf;
	rc4.Encipher();
	int length = rc4.m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, rc4.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 CRc4Dlg::DecipherString()
{
	UpdateData();
	OnSetkey();
	CRc4 rc4;
	for ( int i = 0 ; i < 256 ; i ++ )
	{
		rc4.m_K[i] = m_K[i];
		rc4.m_S[i] = m_S[i];
	}
	rc4.m_cipherString = m_cipherString;
	rc4.Decipher();
	m_cipherString = "";
	m_plainString = rc4.m_plainString;
	UpdateData(false);
}

void CRc4Dlg::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();
	OnSetkey();
	CRc4 rc4;
	for ( int i = 0 ; i < 256 ; i ++ )
	{
		rc4.m_K[i] = m_K[i];
		rc4.m_S[i] = m_S[i];
	}
	rc4.m_cipherString = pBuf;
	rc4.Decipher();
	int length = rc4.m_cipherString.GetLength() +1;
	unsigned char * cipherBlock = (unsigned char*)malloc(length);
	memset(cipherBlock, 0, length);
	memcpy(cipherBlock, rc4.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 CRc4Dlg::OnOpenPlain()
{
	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 CRc4Dlg::OnOpenCipher()
{
	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 CRc4Dlg::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 CRc4Dlg::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);
}

BOOL CRc4Dlg::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)
		|| pWnd == GetDlgItem(IDC_SHOWKEY)
		)
		::SetCursor(m_hHand);
	else if ( pWnd == GetDlgItem(IDC_PLAIN_STRING)
		|| pWnd == GetDlgItem(IDC_CIPHER_STRING)
		|| pWnd == GetDlgItem(IDC_PLAIN_FILE)
		|| pWnd == GetDlgItem(IDC_CIPHER_FILE)
		|| pWnd == GetDlgItem(IDC_KEY1)
		|| pWnd == GetDlgItem(IDC_KEY2)
		|| pWnd == GetDlgItem(IDC_KEY3)
		|| pWnd == GetDlgItem(IDC_KEY5)
		|| pWnd == GetDlgItem(IDC_KEYSTREAM)
		|| pWnd == GetDlgItem(IDC_CYCLENUM)
		)
		::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;
}

⌨️ 快捷键说明

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