www.pudn.com > DESandRSA.rar > CECREITYDlg.cpp


// CECREITYDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "CECREITY.h" 
#include "CECREITYDlg.h" 
//marvin 
#include  
#include  
#include  
#include  
#include  
//marvin 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
//marvin 
#define	MYRSA	1 
#define	MYDES	0 
#define	MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING|X509_ASN_ENCODING) 
void	HandleError(char *s); 
 
#define	ENCRYPT_ALGORITHM CALG_RC2 
#define	ENCRYPT_BLOCK_SIZE 8 
//¼ÓÃÜÎļþº¯Êý 
BOOL	CryptEncryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPassword,int suanfa); 
BOOL	CryptDecryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPassword,int suanfa); 
//marvin 
BOOL	CryptEncryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPasswords,int suanfa) 
{ 
	FILE*	hSource; 
	FILE*	hDestination; 
 
	HCRYPTPROV	hCryptProv; 
	HCRYPTKEY	hKey; 
	HCRYPTKEY	hXchgKey; 
	HCRYPTHASH	hHash; 
 
	PBYTE	pbKeyBlob; 
	DWORD	dwKeyBlobLen; 
 
	PBYTE	pbBuffer; 
	DWORD	dwBlockLen; 
 
	DWORD	dwBufferLen; 
	DWORD	dwCount; 
	hSource=fopen(szSource,"rb");	 
	hDestination=fopen(szDestination,"wb"); 
//change 
	//if(suanfa==MYRSA) 
	 
		CryptAcquireContext(&hCryptProv,NULL,NULL,PROV_RSA_FULL,0); 
		CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash); 
 
	//------------------------ 
	CryptHashData(hHash,(BYTE *)szPasswords,strlen(szPasswords),0); 
	if(suanfa==MYRSA) 
		CryptDeriveKey(hCryptProv,ENCRYPT_ALGORITHM,hHash,0,&hKey); 
	else 
		CryptDeriveKey(hCryptProv,CALG_RC4,hHash,0,&hKey); 
	CryptDestroyHash(hHash); 
	hHash=0; 
 
	//------------------------ 
	dwBlockLen=1000-1000%ENCRYPT_BLOCK_SIZE; 
	if(ENCRYPT_BLOCK_SIZE>1) 
		dwBufferLen=dwBlockLen+ENCRYPT_BLOCK_SIZE; 
	else 
		dwBufferLen=dwBlockLen; 
 
	pbBuffer=(BYTE * )malloc(dwBufferLen); 
	do 
	{ 
		dwCount=fread(pbBuffer,1,dwBlockLen,hSource); 
		if(ferror(hSource)) 
			HandleError("Opem file error!"); 
		if(!CryptEncrypt(hKey,0,feof(hSource),0,pbBuffer,&dwCount,dwBufferLen)) 
			HandleError("CryptEncrypt	error!"); 
		fwrite(pbBuffer,1,dwCount,hDestination); 
		if(ferror(hDestination)) 
			HandleError("Error on write to destination!"); 
	}while(!feof(hSource)); 
	fclose(hSource); 
	fclose(hDestination); 
	if(pbBuffer) 
		free(pbBuffer); 
	if(hKey) 
		CryptDestroyKey(hKey); 
	if(hXchgKey) 
		CryptDestroyKey(hXchgKey); 
	if(hHash) 
		CryptDestroyHash(hHash); 
	if(hCryptProv) 
		CryptReleaseContext(hCryptProv,0); 
	return	(TRUE); 
} 
 
BOOL	CryptDecryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPasswords,int suanfa) 
{ 
	FILE*	hSource; 
	FILE*	hDestination; 
 
	HCRYPTPROV	hCryptProv; 
	HCRYPTKEY	hKey; 
	HCRYPTKEY	hXchgKey; 
	HCRYPTHASH	hHash; 
 
	PBYTE	pbKeyBlob; 
	DWORD	dwKeyBlobLen; 
 
	PBYTE	pbBuffer; 
	DWORD	dwBlockLen; 
 
	DWORD	dwBufferLen; 
	DWORD	dwCount; 
	hSource=fopen(szSource,"rb");	 
	hDestination=fopen(szDestination,"wb"); 
	//if(suanfa==MYRSA) 
	//{ 
		CryptAcquireContext(&hCryptProv,NULL,NULL,PROV_RSA_FULL,0); 
		CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash); 
	//} 
	//else 
	//{ 
	//	CryptAcquireContext(&hCryptProv,NULL,NULL,PROV_RSA_FULL,0); 
	//	CryptCreateHash(hCryptProv,CALG_CYLINK_MEK,0,0,&hHash); 
	//} 
	//CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash); 
	//------------------------ 
	CryptHashData(hHash,(BYTE *)szPasswords,strlen(szPasswords),0); 
	 
	if(suanfa==MYRSA) 
		CryptDeriveKey(hCryptProv,ENCRYPT_ALGORITHM,hHash,0,&hKey); 
	else 
		CryptDeriveKey(hCryptProv,CALG_RC4,hHash,0,&hKey); 
	 
	CryptDestroyHash(hHash); 
	hHash=0; 
 
	//------------------------ 
	dwBlockLen=1000-1000%ENCRYPT_BLOCK_SIZE; 
	if(ENCRYPT_BLOCK_SIZE>1) 
		dwBufferLen=dwBlockLen+ENCRYPT_BLOCK_SIZE; 
	else 
		dwBufferLen=dwBlockLen; 
 
	pbBuffer=(BYTE * )malloc(dwBufferLen); 
	do 
	{ 
		dwCount=fread(pbBuffer,1,dwBlockLen,hSource); 
		if(ferror(hSource)) 
			HandleError("Opem file error!"); 
		if(!CryptDecrypt(hKey,0,feof(hSource),0,pbBuffer,&dwCount)) 
			HandleError("CryptEncrypt	error!"); 
		fwrite(pbBuffer,1,dwCount,hDestination); 
		if(ferror(hDestination)) 
			HandleError("Error on write to destination!"); 
	}while(!feof(hSource)); 
	fclose(hSource); 
	fclose(hDestination); 
	if(pbBuffer) 
		free(pbBuffer); 
	if(hKey) 
		CryptDestroyKey(hKey); 
	if(hXchgKey) 
		CryptDestroyKey(hXchgKey); 
	if(hHash) 
		CryptDestroyHash(hHash); 
	if(hCryptProv) 
		CryptReleaseContext(hCryptProv,0); 
	return	(TRUE); 
} 
void	HandleError(char *s) 
{ 
	//AfxMessageBox(s); 
} 
//marvin 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCECREITYDlg dialog 
 
CCECREITYDlg::CCECREITYDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CCECREITYDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCECREITYDlg) 
	m_miyao = _T(""); 
	m_username = _T(""); 
	m_password = _T(""); 
	m_sourcefile = _T(""); 
	m_destinationfile = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CCECREITYDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCECREITYDlg) 
	DDX_Control(pDX, IDC_PASSWORDEDIT, m_Cpassword); 
	DDX_Control(pDX, IDC_USEREDIT, m_Cuseredit); 
	DDX_Control(pDX, IDC_CEREDIT, m_Cceredit); 
	DDX_Control(pDX, IDC_DESTINATIONEDIT, m_Cdestinationfile); 
	DDX_Control(pDX, IDC_SOURCEEDIT, m_Csourcefile); 
	DDX_Text(pDX, IDC_CEREDIT, m_miyao); 
	DDV_MaxChars(pDX, m_miyao, 8); 
	DDX_Text(pDX, IDC_USEREDIT, m_username); 
	DDX_Text(pDX, IDC_PASSWORDEDIT, m_password); 
	DDX_Text(pDX, IDC_SOURCEEDIT, m_sourcefile); 
	DDX_Text(pDX, IDC_DESTINATIONEDIT, m_destinationfile); 
	DDV_MaxChars(pDX, m_destinationfile, 50); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CCECREITYDlg, CDialog) 
	//{{AFX_MSG_MAP(CCECREITYDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_DESRADIO,OnDesSelect) 
	ON_BN_CLICKED(IDC_RSARADIO,OnRsaSelect) 
	ON_BN_CLICKED(IDC_LOOKBUTTON1, OnLookbutton1) 
	ON_BN_CLICKED(IDC_LOOKBUTTON2, OnLookbutton2) 
	ON_BN_CLICKED(IDC_JIAMBUTTON, OnJiambutton) 
	ON_BN_CLICKED(IDC_JIEMIBUTTON, OnJiemibutton) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCECREITYDlg message handlers 
 
BOOL CCECREITYDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	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); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	((CButton *)GetDlgItem(IDC_DESRADIO))->SetCheck(BST_CHECKED); 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CCECREITYDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CCECREITYDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		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; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CCECREITYDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CCECREITYDlg::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	 
	CDialog::OnCancel(); 
} 
 
void CCECREITYDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
	 
	CDialog::OnOK(); 
} 
 
void CCECREITYDlg::OnLookbutton1()  
{ 
	CFileDialog dlg(TRUE); 
	if(dlg.DoModal()==IDOK) 
		this->m_sourcefile=dlg.GetPathName(); 
		this->m_Csourcefile.SetWindowText(m_sourcefile); 
} 
 
void CCECREITYDlg::OnLookbutton2()  
{ 
	CFileDialog dlg(FALSE); 
	if(dlg.DoModal()==IDOK) 
		this->m_destinationfile=dlg.GetPathName(); 
		this->m_Cdestinationfile.SetWindowText(m_destinationfile); 
	 
} 
void CCECREITYDlg::OnRsaSelect() 
{ 
	this->m_Cceredit.EnableWindow(false); 
	this->m_Cuseredit.EnableWindow(true); 
	this->m_Cpassword.EnableWindow(true); 
} 
void CCECREITYDlg::OnDesSelect() 
{ 
	this->m_Cceredit.EnableWindow(true); 
	this->m_Cuseredit.EnableWindow(false); 
	this->m_Cpassword.EnableWindow(false); 
} 
 
void CCECREITYDlg::OnJiambutton()  
{ 
	// TODO: Add your control notification handler code here 
	UINT	nLes=GetCheckedRadioButton(IDC_DESRADIO,IDC_RSARADIO); 
	char szSource[255]; 
	char szDestination[255]; 
	char szPassword[255]; 
	char szMiyao[255]; 
	UpdateData(TRUE); 
	memcpy(szSource,m_sourcefile,m_sourcefile.GetLength()+1); 
	memcpy(szDestination,m_destinationfile,m_destinationfile.GetLength()+1); 
	memcpy(szPassword,m_password,m_password.GetLength()+1); 
	memcpy(szMiyao,m_miyao,m_miyao.GetLength()+1); 
	if(m_sourcefile==""||m_destinationfile=="") 
	{ 
		AfxMessageBox("Input the file name!"); 
	} 
	else 
	{ 
		switch(nLes) 
		{ 
		case	IDC_DESRADIO:CryptEncryptFile(szSource,szDestination,szMiyao,MYDES); 
							 break; 
 
		case	IDC_RSARADIO: 
							 CryptEncryptFile(szSource,szDestination,szPassword,MYRSA); 
							 break; 
							  
		} 
	} 
} 
 
void CCECREITYDlg::OnJiemibutton()  
{ 
	// TODO: Add your control notification handler code here 
	UINT	nLes=GetCheckedRadioButton(IDC_DESRADIO,IDC_RSARADIO); 
	char szSource[255]; 
	char szDestination[255]; 
	char szPassword[255]; 
	char szMiyao[255]; 
	UpdateData(TRUE); 
	memcpy(szSource,m_sourcefile,m_sourcefile.GetLength()+1); 
	memcpy(szDestination,m_destinationfile,m_destinationfile.GetLength()+1); 
	memcpy(szPassword,m_password,m_password.GetLength()+1); 
	memcpy(szMiyao,m_miyao,m_miyao.GetLength()+1); 
	if(m_sourcefile==""||m_destinationfile=="") 
	{ 
		AfxMessageBox("Input the file name!"); 
	} 
	else 
	{ 
		switch(nLes) 
		{ 
		case	IDC_DESRADIO:CryptDecryptFile(szSource,szDestination,szMiyao,MYDES); 
							 break; 
 
		case	IDC_RSARADIO: 
							 CryptDecryptFile(szSource,szDestination,szPassword,MYRSA); 
							 break; 
							  
		} 
	} 
}