📄 lfsrdlg.cpp
字号:
// LfsrDlg.cpp : implementation file
//
#include "stdafx.h"
#include "030300816.h"
#include "LfsrDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLfsrDlg dialog
CLfsrDlg::CLfsrDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLfsrDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLfsrDlg)
m_cipherFile = _T("");
m_cipherString = _T("");
m_plainFile = _T("");
m_plainString = _T("");
m_radio = 0;
m_fbkKey = _T("03");
m_initKey = _T("01");
m_keySize = 8;
m_cycleNum = 128;
m_keyString = _T("");
m_hexNum = 2;
//}}AFX_DATA_INIT
}
void CLfsrDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLfsrDlg)
DDX_Control(pDX, IDC_SPIN, m_spin);
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_FBKKEY, m_fbkKey);
DDX_Text(pDX, IDC_INITKEY, m_initKey);
DDX_Text(pDX, IDC_KEYSIZE, m_keySize);
DDV_MinMaxUInt(pDX, m_keySize, 4, 128);
DDX_Text(pDX, IDC_CYCLENUM, m_cycleNum);
DDX_Text(pDX, IDC_KEYSTRING, m_keyString);
DDX_Text(pDX, IDC_HEXNUM, m_hexNum);
DDV_MinMaxUInt(pDX, m_hexNum, 1, 32);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLfsrDlg, CDialog)
//{{AFX_MSG_MAP(CLfsrDlg)
ON_BN_CLICKED(IDC_SETKEY, OnSetkey)
ON_BN_CLICKED(IDC_ENCIPHER, OnEncipher)
ON_BN_CLICKED(IDC_DECIPHER, OnDecipher)
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN, OnDeltaposSpin)
ON_BN_CLICKED(IDC_RADIO_STRING, OnRadioString)
ON_BN_CLICKED(IDC_RADIO_FILE, OnRadioFile)
ON_BN_CLICKED(IDC_SHOWKEY, OnShowkey)
ON_BN_CLICKED(IDC_OPEN_PLAIN, OnOpenPlain)
ON_BN_CLICKED(IDC_OPEN_CIPHER, OnOpenCipher)
ON_WM_SETCURSOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLfsrDlg message handlers
//DEL void CLfsrDlg::OnPlus()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL UpdateData();
//DEL if (m_keySize >= 128)
//DEL return;
//DEL m_keySize += 4;
//DEL UpdateData(false);
//DEL }
//DEL void CLfsrDlg::OnMinus()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL UpdateData();
//DEL if (m_keySize <= 4)
//DEL return;
//DEL m_keySize -= 4;
//DEL UpdateData(false);
//DEL }
void CLfsrDlg::OnSetkey()
{
// TODO: Add your control notification handler code here
UpdateData();
if ( !CheckKey() ) return;
m_decInitKey = Convert_sHex2Int(m_initKey);
m_decFbkKey = Convert_sHex2Int(m_fbkKey);
UpdateData(false);
}
bool CLfsrDlg::CheckKey()
{
if ( (UINT)m_initKey.GetLength() != m_keySize/4
|| (UINT)m_fbkKey.GetLength() != m_keySize/4 )
{
MessageBox("Initial key or feedback key did not match the lfsr key size.",
"Error :-(",MB_OK);
return false;
}
else
return true;
}
UINT CLfsrDlg::Convert_sHex2Int(CString m_hex)
{
UINT len;
char temp;
UINT i;
CString dec[8]={"0"};
UINT result = 0;
if(m_hex == "")
{
AfxMessageBox("Please enter a hex number equal or smaller than 0xffffffff!");
}
else
{
len = m_hex.GetLength();
for(i=0;i<len;i++)
{
temp = m_hex.GetAt(i);
dec[i] = Convert_Hex2Bin(temp);
result = result + (int)pow(16, len-1-i)*Convert_sBin2Int(dec[i]);
}
}
return result;
}
CString CLfsrDlg::Convert_Hex2Bin(char hex)
{
CString bin;
switch(hex)
{
case '0':
bin = "0000";
break;
case '1':
bin = "0001";
break;
case '2':
bin = "0010";
break;
case '3':
bin = "0011";
break;
case '4':
bin = "0100";
break;
case '5':
bin = "0101";
break;
case '6':
bin = "0110";
break;
case '7':
bin = "0111";
break;
case '8':
bin = "1000";
break;
case '9':
bin = "1001";
break;
case 'a':
case 'A':
bin = "1010";
break;
case 'b':
case 'B':
bin = "1011";
break;
case 'c':
case 'C':
bin = "1100";
break;
case 'd':
case 'D':
bin = "1101";
break;
case 'e':
case 'E':
bin = "1110";
break;
case 'f':
case 'F':
bin = "1111";
break;
default:
AfxMessageBox("The Hex number is incorrect!");
break;
}
return bin;
}
UINT CLfsrDlg::Convert_sBin2Int(CString sbin)
{
UINT temp, result;
temp = atoi(sbin);
result = temp/1000 * 8 + (temp%1000)/100 * 4 + (temp%100)/10 * 2 + (temp%10);
return result;
}
void CLfsrDlg::OnEncipher()
{
// TODO: Add your control notification handler code here
if ( 0 == m_iMethod )
{
EncipherString();
}
else // File
{
EncipherFile();
}
}
BOOL CLfsrDlg::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 CLfsrDlg::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_iMethod = 0;
ShowStringGroup(true);
ShowFileGroup(false); OnSetkey();
//ShowKey();
m_spin.SetBuddy(GetDlgItem(IDC_KEYSIZE));
m_spin.SetRange(4,128);
m_spin.SetPos(8);
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 CLfsrDlg::OnDecipher()
{
// TODO: Add your control notification handler code here
if ( 0 == m_iMethod )
{
DecipherString();
}
else // File
{
DecipherFile();
}
}
void CLfsrDlg::OnDeltaposSpin(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
switch ( pNMUpDown->iDelta )
{
case -1:
pNMUpDown->iDelta -= 3;
if ( m_hexNum != 1)
m_hexNum -=1;
break;
case 1:
pNMUpDown->iDelta += 3;
if ( m_hexNum != 32)
m_hexNum +=1;
break;
}
// TODO: Add your control notification handler code here
UpdateData(false);
*pResult = 0;
}
void CLfsrDlg::ShowKey()
{
bitset<128> m_bitInitKey(m_decInitKey);
bitset<128> m_bitFbkKey(m_decFbkKey);
bitset<128> m_bitTemp(0);
CString temp=_T("");
UpdateData();
for ( int i = 0 ; i < m_cycleNum ; i ++ )
{
if ( m_bitInitKey[0] == 1 )
temp += "1";
else
temp += "0";
m_bitTemp = ( m_bitInitKey & m_bitFbkKey );
if ( m_bitTemp.count() % 2 == 1 )
{
m_bitInitKey >>= 1;
m_bitInitKey.set(m_keySize-1);
}
else
{
m_bitInitKey >>= 1;
m_bitInitKey.reset(m_keySize-1);
}
}
m_keyString = temp;
UpdateData(false);
}
void CLfsrDlg::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 CLfsrDlg::OnRadioString()
{
// TODO: Add your control notification handler code here
ShowStringGroup(true);
ShowFileGroup(false);
m_iMethod = 0;
}
void CLfsrDlg::OnRadioFile()
{
// TODO: Add your control notification handler code here
ShowStringGroup(false);
ShowFileGroup(true);
m_iMethod = 1;
}
void CLfsrDlg::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 CLfsrDlg::EncipherString()
{
UpdateData();
CLfsr lfsr;
lfsr.m_keySize = m_keySize;
lfsr.m_plainString = m_plainString;
lfsr.m_decFbkKey = m_decFbkKey;
lfsr.m_decInitKey = m_decInitKey;
lfsr.Encipher();
m_plainString = "";
m_cipherString = lfsr.m_cipherString;
UpdateData(false);
}
void CLfsrDlg::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();
CLfsr lfsr;
lfsr.m_plainString = pBuf;
lfsr.m_keySize = m_keySize;
lfsr.m_decFbkKey = m_decFbkKey;
lfsr.m_decInitKey = m_decInitKey;
lfsr.Encipher();
int length = lfsr.m_cipherString.GetLength() +1;
unsigned char * cipherBlock = (unsigned char*)malloc(length);
memset(cipherBlock, 0, length);
memcpy(cipherBlock, lfsr.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 CLfsrDlg::OnShowkey()
{
// TODO: Add your control notification handler code here
UpdateData();
ShowKey();
UpdateData(false);
}
void CLfsrDlg::DecipherString()
{
UpdateData();
CLfsr lfsr;
lfsr.m_keySize = m_keySize;
lfsr.m_cipherString = m_cipherString;
lfsr.m_decFbkKey = m_decFbkKey;
lfsr.m_decInitKey = m_decInitKey;
lfsr.Decipher();
m_cipherString = "";
m_plainString = lfsr.m_plainString;
UpdateData(false);
}
void CLfsrDlg::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();
CLfsr lfsr;
lfsr.m_keySize = m_keySize;
lfsr.m_cipherString = pBuf;
lfsr.m_decFbkKey = m_decFbkKey;
lfsr.m_decInitKey = m_decInitKey;
lfsr.Decipher();
int length = lfsr.m_cipherString.GetLength() +1;
unsigned char * cipherBlock = (unsigned char*)malloc(length);
memset(cipherBlock, 0, length);
memcpy(cipherBlock, lfsr.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 CLfsrDlg::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 CLfsrDlg::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
);
}
}
BOOL CLfsrDlg::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)
|| pWnd == GetDlgItem(IDC_SPIN)
)
::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_KEYSIZE)
|| pWnd == GetDlgItem(IDC_INITKEY)
|| pWnd == GetDlgItem(IDC_FBKKEY)
|| pWnd == GetDlgItem(IDC_CYCLENUM)
|| pWnd == GetDlgItem(IDC_KEYSTRING)
)
::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 + -