Crypt Library Demo 1.00
|
00001 /* This file is part of Crypt Library Demo application developed by Mihai MOGA. 00002 00003 Image Converter is free software: you can redistribute it and/or modify it 00004 under the terms of the GNU General Public License as published by the Open 00005 Source Initiative, either version 3 of the License, or any later version. 00006 00007 Image Converter is distributed in the hope that it will be useful, but 00008 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00009 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 00010 00011 You should have received a copy of the GNU General Public License along with 00012 Crypt Library Demo. If not, see <http://www.opensource.org/licenses/gpl-3.0.html>*/ 00013 00014 // CryptLibraryDemoDlg.cpp : implementation file 00015 // 00016 00017 #include "stdafx.h" 00018 #include "CryptLibraryDemo.h" 00019 #include "CryptLibraryDemoDlg.h" 00020 #include "CryptographyExt.h" 00021 #include "VersionInfo.h" 00022 #include "HyperlinkStatic.h" 00023 00024 #ifdef _DEBUG 00025 #define new DEBUG_NEW 00026 #endif 00027 00029 // CAboutDlg dialog used for App About 00031 00032 class CAboutDlg : public CDialog 00033 { 00034 public: 00035 CAboutDlg(); 00036 00037 // Dialog Data 00038 enum { IDD = IDD_ABOUTBOX }; 00039 00040 protected: 00041 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 00042 00043 // Implementation 00044 public: 00045 virtual BOOL OnInitDialog(); 00046 afx_msg void OnDestroy(); 00047 00048 protected: 00049 CFont m_fontCourier; 00050 CStatic m_ctrlVersion; 00051 CEdit m_ctrlWarning; 00052 CVersionInfo m_pVersionInfo; 00053 CHyperlinkStatic m_ctrlWebsite; 00054 CHyperlinkStatic m_ctrlEmail; 00055 00056 DECLARE_MESSAGE_MAP() 00057 }; 00058 00059 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 00060 { 00061 } 00062 00063 void CAboutDlg::DoDataExchange(CDataExchange* pDX) 00064 { 00065 CDialog::DoDataExchange(pDX); 00066 DDX_Control(pDX, IDC_VERSION, m_ctrlVersion); 00067 DDX_Control(pDX, IDC_WARNING, m_ctrlWarning); 00068 DDX_Control(pDX, IDC_WEBSITE, m_ctrlWebsite); 00069 DDX_Control(pDX, IDC_EMAIL, m_ctrlEmail); 00070 } 00071 00072 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 00073 ON_WM_DESTROY() 00074 END_MESSAGE_MAP() 00075 00076 BOOL CAboutDlg::OnInitDialog() 00077 { 00078 CDialog::OnInitDialog(); 00079 00080 if (m_pVersionInfo.Load(AfxGetApp()->m_pszExeName)) 00081 { 00082 CString strName = m_pVersionInfo.GetProductName(); 00083 CString strVersion = m_pVersionInfo.GetProductVersionAsString(); 00084 strVersion.Replace(_T(" "), _T("")); 00085 strVersion.Replace(_T(","), _T(".")); 00086 const int nFirst = strVersion.Find(_T('.')); 00087 const int nSecond = strVersion.Find(_T('.'), nFirst + 1); 00088 strVersion.Truncate(nSecond); 00089 m_ctrlVersion.SetWindowText(strName + _T(" version ") + strVersion); 00090 } 00091 00092 // set up window's terminal fixed font 00093 LOGFONT logfont; 00094 memset(&logfont, 0, sizeof(logfont)); 00095 logfont.lfCharSet = OEM_CHARSET; 00096 logfont.lfPitchAndFamily = FIXED_PITCH; 00097 // logfont.lfQuality = ANTIALIASED_QUALITY; 00098 logfont.lfOutPrecision = OUT_TT_PRECIS; 00099 logfont.lfWeight = FW_NORMAL; 00100 logfont.lfHeight = 10; 00101 _tcscpy_s(logfont.lfFaceName, LF_FACESIZE, _T("Tahoma")); 00102 VERIFY(m_fontCourier.CreateFontIndirect(&logfont)); 00103 00104 m_ctrlWarning.SetFont(&m_fontCourier); 00105 m_ctrlWarning.SetWindowText(_T("THE SOFTWARE IS PROVIDED \"AS-IS\" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MIHAI MOGA BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.")); 00106 00107 m_ctrlWebsite.SetHyperlink(_T("http://www.mihaimoga.com/")); 00108 m_ctrlEmail.SetHyperlink(_T("mailto:contact@mihaimoga.com")); 00109 00110 return TRUE; // return TRUE unless you set the focus to a control 00111 // EXCEPTION: OCX Property Pages should return FALSE 00112 } 00113 00114 void CAboutDlg::OnDestroy() 00115 { 00116 CDialog::OnDestroy(); 00117 00118 VERIFY(m_fontCourier.DeleteObject()); 00119 } 00120 00122 // CCryptLibraryDemoDlg dialog 00124 00125 CCryptLibraryDemoDlg::CCryptLibraryDemoDlg(CWnd* pParent /*=NULL*/) 00126 : CDialog(CCryptLibraryDemoDlg::IDD, pParent) 00127 { 00128 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 00129 } 00130 00131 void CCryptLibraryDemoDlg::DoDataExchange(CDataExchange* pDX) 00132 { 00133 CDialog::DoDataExchange(pDX); 00134 DDX_Control(pDX, IDC_FILENAME, m_editFilename); 00135 DDX_Control(pDX, IDC_CHECKSUM, m_editChecksum); 00136 DDX_Control(pDX, IDC_INPUTNAME, m_editInputName); 00137 DDX_Control(pDX, IDC_OUTPUTNAME, m_editOutputName); 00138 DDX_Control(pDX, IDC_COMPUTE, m_btnCompute); 00139 DDX_Control(pDX, IDC_ENCRYPT, m_btnEncrypt); 00140 DDX_Control(pDX, IDC_DECRYPT, m_btnDecrypt); 00141 } 00142 00143 BEGIN_MESSAGE_MAP(CCryptLibraryDemoDlg, CDialog) 00144 ON_WM_SYSCOMMAND() 00145 ON_WM_PAINT() 00146 ON_WM_QUERYDRAGICON() 00147 //}}AFX_MSG_MAP 00148 ON_BN_CLICKED(IDC_SELECT, &CCryptLibraryDemoDlg::OnBnClickedSelect) 00149 ON_BN_CLICKED(IDC_COMPUTE, &CCryptLibraryDemoDlg::OnBnClickedCompute) 00150 ON_BN_CLICKED(IDC_INPUTFILE, &CCryptLibraryDemoDlg::OnBnClickedInputfile) 00151 ON_BN_CLICKED(IDC_OUTPUTFILE, &CCryptLibraryDemoDlg::OnBnClickedOutputfile) 00152 ON_BN_CLICKED(IDC_ENCRYPT, &CCryptLibraryDemoDlg::OnBnClickedEncrypt) 00153 ON_BN_CLICKED(IDC_DECRYPT, &CCryptLibraryDemoDlg::OnBnClickedDecrypt) 00154 END_MESSAGE_MAP() 00155 00157 // CCryptLibraryDemoDlg message handlers 00159 00160 BOOL CCryptLibraryDemoDlg::OnInitDialog() 00161 { 00162 CDialog::OnInitDialog(); 00163 00164 // Add "About..." menu item to system menu. 00165 00166 // IDM_ABOUTBOX must be in the system command range. 00167 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 00168 ASSERT(IDM_ABOUTBOX < 0xF000); 00169 00170 CMenu* pSysMenu = GetSystemMenu(FALSE); 00171 if (pSysMenu != NULL) 00172 { 00173 BOOL bNameValid; 00174 CString strAboutMenu; 00175 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); 00176 ASSERT(bNameValid); 00177 if (!strAboutMenu.IsEmpty()) 00178 { 00179 pSysMenu->AppendMenu(MF_SEPARATOR); 00180 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 00181 } 00182 } 00183 00184 // Set the icon for this dialog. The framework does this automatically 00185 // when the application's main window is not a dialog 00186 SetIcon(m_hIcon, TRUE); // Set big icon 00187 SetIcon(m_hIcon, FALSE); // Set small icon 00188 00189 #ifdef _DEBUG 00190 CString strBuffer1 = _T("abc"), strResult1; 00191 VERIFY(GetChecksumString(CALG_MD5, strResult1, strBuffer1)); 00192 TRACE(_T("MD5(%s) => %s\n"), strBuffer1, strResult1); 00193 00194 CString strBuffer2 = _T("abc"), strResult2; 00195 VERIFY(GetChecksumFile(CALG_SHA1, strResult2, _T("D:\\AddressBook.csv"))); 00196 TRACE(_T("SHA1(%s) => %s\n"), strBuffer2, strResult2); 00197 00198 CString strSecretKey = GetComputerID(); 00199 LPBYTE lpszSecretKey = (LPBYTE)(LPCTSTR)strSecretKey; 00200 DWORD dwSecretKey = (strSecretKey.GetLength() + 1) * sizeof(TCHAR); 00201 CString strFilename = _T("D:\\AddressBook.csv"); 00202 CString strEncrypt = _T("D:\\AddressBook.rc4"); 00203 CString strDecrypt = _T("D:\\AddressBook.txt"); 00204 VERIFY(EncryptFile(CALG_RC4, strEncrypt, strFilename, lpszSecretKey, dwSecretKey)); 00205 VERIFY(DecryptFile(CALG_RC4, strDecrypt, strEncrypt, lpszSecretKey, dwSecretKey)); 00206 #endif 00207 00208 CRect rectCryptLibraryDemo; 00209 GetClientRect(&rectCryptLibraryDemo); 00210 TRACE(_T("[CCryptLibraryDemoDlg] Width = %d, Height = %d\n"), 00211 rectCryptLibraryDemo.Width(), rectCryptLibraryDemo.Height()); 00212 00213 m_btnCompute.EnableWindow(FALSE); 00214 m_btnEncrypt.EnableWindow(FALSE); 00215 m_btnDecrypt.EnableWindow(FALSE); 00216 00217 return TRUE; // return TRUE unless you set the focus to a control 00218 } 00219 00220 void CCryptLibraryDemoDlg::OnSysCommand(UINT nID, LPARAM lParam) 00221 { 00222 if ((nID & 0xFFF0) == IDM_ABOUTBOX) 00223 { 00224 CAboutDlg dlgAbout; 00225 dlgAbout.DoModal(); 00226 } 00227 else 00228 { 00229 CDialog::OnSysCommand(nID, lParam); 00230 } 00231 } 00232 00233 // If you add a minimize button to your dialog, you will need the code below 00234 // to draw the icon. For MFC applications using the document/view model, 00235 // this is automatically done for you by the framework. 00236 00237 void CCryptLibraryDemoDlg::OnPaint() 00238 { 00239 if (IsIconic()) 00240 { 00241 CPaintDC dc(this); // device context for painting 00242 00243 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); 00244 00245 // Center icon in client rectangle 00246 int cxIcon = GetSystemMetrics(SM_CXICON); 00247 int cyIcon = GetSystemMetrics(SM_CYICON); 00248 CRect rect; 00249 GetClientRect(&rect); 00250 int x = (rect.Width() - cxIcon + 1) / 2; 00251 int y = (rect.Height() - cyIcon + 1) / 2; 00252 00253 // Draw the icon 00254 dc.DrawIcon(x, y, m_hIcon); 00255 } 00256 else 00257 { 00258 CDialog::OnPaint(); 00259 } 00260 } 00261 00262 // The system calls this function to obtain the cursor to display while the user drags 00263 // the minimized window. 00264 HCURSOR CCryptLibraryDemoDlg::OnQueryDragIcon() 00265 { 00266 return static_cast<HCURSOR>(m_hIcon); 00267 } 00268 00269 void CCryptLibraryDemoDlg::OnBnClickedSelect() 00270 { 00271 DWORD dwFlags = OFN_DONTADDTORECENT | OFN_ENABLESIZING | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_LONGNAMES; 00272 LPCTSTR lpszFilter = _T("All files (*.*)|*.*\0"); 00273 CFileDialog pFileDialog(TRUE, NULL, NULL, dwFlags, lpszFilter, this); 00274 if (pFileDialog.DoModal() == IDOK) 00275 { 00276 m_strFilename = pFileDialog.GetPathName(); 00277 m_editFilename.SetWindowText(m_strFilename); 00278 m_btnCompute.EnableWindow(!m_strFilename.IsEmpty()); 00279 } 00280 } 00281 00282 void CCryptLibraryDemoDlg::OnBnClickedCompute() 00283 { 00284 CString strResult; 00285 if (!m_strFilename.IsEmpty()) 00286 { 00287 if (GetChecksumFile(CALG_MD5, strResult, m_strFilename)) 00288 { 00289 m_editChecksum.SetWindowText(strResult); 00290 MessageBox(_T("MD5 checksum of selected file is ready!"), _T("Crypt Library Demo"), MB_OK); 00291 } 00292 } 00293 } 00294 00295 void CCryptLibraryDemoDlg::OnBnClickedInputfile() 00296 { 00297 DWORD dwFlags = OFN_DONTADDTORECENT | OFN_ENABLESIZING | OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_LONGNAMES; 00298 LPCTSTR lpszFilter = _T("All files (*.*)|*.*\0"); 00299 CFileDialog pFileDialog(TRUE, NULL, NULL, dwFlags, lpszFilter, this); 00300 if (pFileDialog.DoModal() == IDOK) 00301 { 00302 m_strInputName = pFileDialog.GetPathName(); 00303 m_editInputName.SetWindowText(m_strInputName); 00304 m_btnEncrypt.EnableWindow(!m_strInputName.IsEmpty() && !m_strOutputName.IsEmpty()); 00305 m_btnDecrypt.EnableWindow(!m_strInputName.IsEmpty() && !m_strOutputName.IsEmpty()); 00306 } 00307 } 00308 00309 void CCryptLibraryDemoDlg::OnBnClickedOutputfile() 00310 { 00311 DWORD dwFlags = OFN_DONTADDTORECENT | OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_LONGNAMES; 00312 LPCTSTR lpszFilter = _T("All files (*.*)|*.*\0"); 00313 CFileDialog pFileDialog(FALSE, NULL, NULL, dwFlags, lpszFilter, this); 00314 if (pFileDialog.DoModal() == IDOK) 00315 { 00316 m_strOutputName = pFileDialog.GetPathName(); 00317 m_editOutputName.SetWindowText(m_strOutputName); 00318 m_btnEncrypt.EnableWindow(!m_strInputName.IsEmpty() && !m_strOutputName.IsEmpty()); 00319 m_btnDecrypt.EnableWindow(!m_strInputName.IsEmpty() && !m_strOutputName.IsEmpty()); 00320 } 00321 } 00322 00323 void CCryptLibraryDemoDlg::OnBnClickedEncrypt() 00324 { 00325 CString strSecretKey = GetComputerID(); 00326 LPBYTE lpszSecretKey = (LPBYTE)(LPCTSTR)strSecretKey; 00327 DWORD dwSecretKey = (strSecretKey.GetLength() + 1) * sizeof(TCHAR); 00328 if (!m_strInputName.IsEmpty() && !m_strOutputName.IsEmpty()) 00329 { 00330 if (EncryptFile(CALG_RC4, m_strOutputName, m_strInputName, lpszSecretKey, dwSecretKey)) 00331 { 00332 MessageBox(_T("The file have been successfully encrypted!"), _T("Crypt Library Demo"), MB_OK); 00333 } 00334 } 00335 } 00336 00337 void CCryptLibraryDemoDlg::OnBnClickedDecrypt() 00338 { 00339 CString strSecretKey = GetComputerID(); 00340 LPBYTE lpszSecretKey = (LPBYTE)(LPCTSTR)strSecretKey; 00341 DWORD dwSecretKey = (strSecretKey.GetLength() + 1) * sizeof(TCHAR); 00342 if (!m_strInputName.IsEmpty() && !m_strOutputName.IsEmpty()) 00343 { 00344 if (EncryptFile(CALG_RC4, m_strOutputName, m_strInputName, lpszSecretKey, dwSecretKey)) 00345 { 00346 MessageBox(_T("The file have been successfully decrypted!"), _T("Crypt Library Demo"), MB_OK); 00347 } 00348 } 00349 }