www.pudn.com > CipherSystem.rar > CipherSystemDlg.cpp
// CipherSystemDlg.cpp : 实现文件 // #include "stdafx.h" #include "CipherSystem.h" #include "CipherSystemDlg.h" #include ".\ciphersystemdlg.h" #include "DESProcess.h" #include "RC4Process.h" #include "LFSRProcess.h" #include "MyAllCipherSystem.h" #include "PlayFairProcess.h" #include "VigenereProcess.h" #include "KeyWordProcess.h" #include "CaeserProcess.h" #include "IDEAProcess.h" #include "GlobalFunctions.h" #includeusing std::string; #ifdef _DEBUG #define new DEBUG_NEW #endif // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialog { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) END_MESSAGE_MAP() // CCipherSystemDlg 对话框 CCipherSystemDlg::CCipherSystemDlg(CWnd* pParent /*=NULL*/) : CDialog(CCipherSystemDlg::IDD, pParent) , m_strPlainText(_T("")) , m_strCipherText(_T("")) , m_strSelectCiphers(_T("")) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCipherSystemDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT_PLANTTEXT, m_strPlainText); DDX_Text(pDX, IDC_EDIT_CIPHERTEXT, m_strCipherText); DDX_CBString(pDX, IDC_COMBO1, m_strSelectCiphers); } BEGIN_MESSAGE_MAP(CCipherSystemDlg, CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP ON_BN_CLICKED(ID_BUTTON_SELECTCIPHER, OnBnClickedButtonSelectcipher) ON_BN_CLICKED(IDC_BUTTON_OPEN_PLAINTEXT, OnBnClickedButtonOpenPlaintext) ON_BN_CLICKED(IDC_BUTTON_OPEN_CIPHERTEXT, OnBnClickedButtonOpenCiphertext) END_MESSAGE_MAP() // CCipherSystemDlg 消息处理程序 BOOL CCipherSystemDlg::OnInitDialog() { CDialog::OnInitDialog(); // 将\“关于...\”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 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); } } // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 // TODO: 在此添加额外的初始化代码 m_strSelectCiphers = "LFSR"; UpdateData(FALSE); return TRUE; // 除非设置了控件的焦点,否则返回 TRUE } void CCipherSystemDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CCipherSystemDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast (dc.GetSafeHdc()), 0); // 使图标在工作矩形中居中 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; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标显示。 HCURSOR CCipherSystemDlg::OnQueryDragIcon() { return static_cast (m_hIcon); } void CCipherSystemDlg::OnBnClickedButtonSelectcipher() { // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); if (m_strPlainText.IsEmpty() && m_strCipherText.IsEmpty()) { AfxMessageBox("请输入明文或密文"); return; } //////////////////////////////////////////////////////// ////////////// IDEA ////////////////////// //////////////////////////////////////////////////////// if (this->m_strSelectCiphers == "IDEA") { IDEAProcess IDEA; IDEA.DoModal(); string key; CStringToString(IDEA.m_strKeyWord, key); if (IDEA.m_iMark == 1 || IDEA.m_iMark == -1) { if (IDEA.m_iMark == 1) { IDEACipherSystem mySystem; string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); } else { IDEACipherSystem mySystem; string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); } UpdateData(FALSE); } } //////////////////////////////////////////////////////// ////////////// DES ////////////////////// //////////////////////////////////////////////////////// if (this->m_strSelectCiphers == "DES") { DESProcess DES; DES.DoModal(); string key; CStringToString(DES.m_strKeyWord, key); if (DES.m_iMark == 1 || DES.m_iMark == -1) { if (DES.m_iMark == 1) { DESCipherSystem mySystem; string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); } else { DESCipherSystem mySystem; string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); } UpdateData(FALSE); } } //////////////////////////////////////////////////////// ////////////// RC4 ////////////////////// //////////////////////////////////////////////////////// else if (m_strSelectCiphers == "RC4") { RC4Process RC4; RC4.DoModal(); int array[4]; array[0] = RC4.m_iArray1; array[1] = RC4.m_iArray2; array[2] = RC4.m_iArray3; array[3] = RC4.m_iArray4; RC4CipherSystem mySystem; switch (RC4.m_iMark) { case 1: { string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(array, 4); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); break; } case -1: { string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(array, 4); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); break; } default:; } UpdateData(FALSE); } //////////////////////////////////////////////////////// ////////////// LFSR ////////////////////// //////////////////////////////////////////////////////// else if (m_strSelectCiphers == "LFSR") { LFSRProcess LFSR; LFSR.DoModal(); int array[4]; array[0] = LFSR.m_iFeedback1; array[1] = LFSR.m_iFeedback2; array[2] = LFSR.m_iFeedback3; array[3] = LFSR.m_iFeedback4; string key; CStringToString(LFSR.m_strKey, key); LFSRCipherSystem mySystem; switch (LFSR.m_iMark) { case 1: { string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(key, array, 4); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); break; } case -1: { string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(key, array, 4); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); break; } default: ; } UpdateData(FALSE); } //////////////////////////////////////////////////////// ////////////// PlayFair ////////////////////// //////////////////////////////////////////////////////// else if (m_strSelectCiphers == "PlayFair") { PlayFairProcess PlayFair; PlayFair.DoModal(); string key; CStringToString(PlayFair.m_strKey, key); PlayFairEncryptSystem mySystem; switch (PlayFair.m_iMark) { case 1: { string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); break; } case -1: { string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); break; } default:; } UpdateData(FALSE); } //////////////////////////////////////////////////////// ////////////// Vigenere ///////////////////// //////////////////////////////////////////////////////// else if (m_strSelectCiphers == "Vigenere") { VigenereProcess V; V.DoModal(); string key; CStringToString(V.m_strKey, key); VigenereEncryptSystem mySystem; switch (V.m_iMark) { case 1: { string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); break; } case -1: { string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); break; } default:; } UpdateData(FALSE); } //////////////////////////////////////////////////////// ///////////////// KeyWord ///////////////////// //////////////////////////////////////////////////////// else if (m_strSelectCiphers == "KeyWord") { KeyWordProcess keyWord; keyWord.DoModal(); string key; CStringToString(keyWord.m_strKey, key); char start = keyWord.m_strStart[0]; KeyWordEncryptSystem mySystem; switch (keyWord.m_iMark) { case 1: { string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(key, start); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); break; } case -1: { string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(key, start); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); break; } default:; } UpdateData(FALSE); } //////////////////////////////////////////////////////// ///////////////// Caeser ///////////////////// //////////////////////////////////////////////////////// else if (m_strSelectCiphers == "Caeser") { CaeserProcess caeser; caeser.DoModal(); int key = caeser.shift; CaeserEncryptSystem mySystem; switch (caeser.m_iMark) { case 1: { string input; CStringToString(m_strPlainText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Encrypt(); StringToCString(mySystem.text, m_strCipherText); break; } case -1: { string input; CStringToString(m_strCipherText, input); mySystem.GetData(input); mySystem.GetKey(key); mySystem.Decrypt(); StringToCString(mySystem.text, m_strPlainText); break; } default:; } UpdateData(FALSE); } } void CCipherSystemDlg::OnBnClickedButtonOpenPlaintext() { // TODO: 在此添加控件通知处理程序代码 CFileDialog m_ldFile(TRUE); CString path, Path; CFile cf; if (m_ldFile.DoModal() == IDOK) { path = m_ldFile.GetFileName(); if (cf.Open(path, CFile::modeRead)) { int length = (int)cf.GetLength(); char* Data = new char[length + 1]; m_strPlainText = ""; Data[length] = 0; try { int nBytesRead = cf.Read(Data, sizeof(char) * length); m_strPlainText = Data; UpdateData(FALSE); } catch (CFileException *e) { e->Delete(); } delete [] Data; cf.Close(); } } } void CCipherSystemDlg::OnBnClickedButtonOpenCiphertext() { // TODO: 在此添加控件通知处理程序代码 CFileDialog m_ldFile(TRUE); CString path, Path; CFile cf; if (m_ldFile.DoModal() == IDOK) { path = m_ldFile.GetFileName(); if (cf.Open( path, CFile::modeRead)) { int length = (int)cf.GetLength(); char* Data = new char[length + 1]; m_strCipherText = ""; Data[length] = 0; try { int nBytesRead = cf.Read(Data, sizeof(char) * length); m_strCipherText = Data; UpdateData(FALSE); } catch (CFileException *e) { e->Delete(); } delete [] Data; cf.Close(); } } }