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

📄 url2md5dlg.cpp

📁 一个加密程序
💻 CPP
字号:
// Url2Md5Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Url2Md5.h"
#include "Url2Md5Dlg.h"
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include "md5.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CUrl2Md5Dlg dialog

CUrl2Md5Dlg::CUrl2Md5Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUrl2Md5Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUrl2Md5Dlg)
	m_nMethod = -1;
	m_cSourceFile = _T("");
	m_cObjectFile = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CUrl2Md5Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUrl2Md5Dlg)
	DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
	DDX_Radio(pDX, IDC_ADD, m_nMethod);
	DDX_Text(pDX, IDC_SFILE, m_cSourceFile);
	DDX_Text(pDX, IDC_DFILE, m_cObjectFile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUrl2Md5Dlg, CDialog)
	//{{AFX_MSG_MAP(CUrl2Md5Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_SOURCE, OnSource)
	ON_BN_CLICKED(IDC_OBJECT, OnObject)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUrl2Md5Dlg message handlers

BOOL CUrl2Md5Dlg::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
	m_nMethod=0;
	((CButton *)GetDlgItem(IDC_ADD))->SetCheck(1);
	//////////////////////////////////////////////added by yanxia
	char FileName[MAX_PATH];
	char buf[1000] = {0};
	CStdioFile file;
	GetSystemDirectory(FileName,MAX_PATH);
	strcat(FileName,"\\classify.txt");	
	file.Open(FileName,CFile::modeRead);
	char Att[10]={0};
	int i=0;
	this->m_ListCtrl.InsertColumn(0, "属性",  LVCFMT_LEFT,100);
	this->m_ListCtrl.InsertColumn(1, "类目",  LVCFMT_LEFT,100);
	this->m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | 
		                               LVS_EX_GRIDLINES | 
									   LVS_EX_HEADERDRAGDROP);
	while(file.ReadString( buf,200 ) != NULL)
	{
	    buf[strlen(buf)-1] = 0;
		strncpy(Att,buf,9);
		char *pbuf = buf+9;
		this->m_ListCtrl.InsertItem(i,Att);
		this->m_ListCtrl.SetItemText(i,1,pbuf);
		i++;
	}


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CUrl2Md5Dlg::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 CUrl2Md5Dlg::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 CUrl2Md5Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CUrl2Md5Dlg::OnAdd() 
{
	// TODO: Add your control notification handler code here
	m_nMethod=0;
}

void CUrl2Md5Dlg::OnDel() 
{
	// TODO: Add your control notification handler code here
	m_nMethod=1;
}

void CUrl2Md5Dlg::OnSource() 
{
	// TODO: Add your control notification handler code here
	char szFilter[]="Text Files(*.txt)||";
	CFileDialog OpenFileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);
    if(IDOK==OpenFileDlg.DoModal())
	{
		m_cSourceFile=OpenFileDlg.GetPathName();
        UpdateData(FALSE);
        //AfxMessageBox(m_cSourceFile);
	}
    
}

void CUrl2Md5Dlg::OnObject() 
{
	// TODO: Add your control notification handler code here
	char szFilter[]="Text Files(*.txt)||";
	CFileDialog OpenFileDlg(FALSE,"txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter,NULL);
    if(IDOK==OpenFileDlg.DoModal()){
		m_cObjectFile=OpenFileDlg.GetPathName();
        UpdateData(FALSE);
        //AfxMessageBox(m_cSourceFile);
	}
}

void CUrl2Md5Dlg::OnOK() 
{
 	UpdateData(TRUE);

	if(m_cSourceFile.IsEmpty()||m_cObjectFile.IsEmpty()){
		AfxMessageBox("请先选择源文件和目标文件!", MB_ICONSTOP);
		return;
	}
	if(strAtt.IsEmpty())
	{
		AfxMessageBox("请选择分类!", MB_ICONSTOP);
		return;
	}
	GetDlgItem(IDC_STATUS)->SetWindowText(_T("状态:  正在转换,请等待..."));
	if(MakeMiddleFile()){
		GetDlgItem(IDC_STATUS)->SetWindowText(_T("状态:  转换失败!"));
	}else{
		GetDlgItem(IDC_STATUS)->SetWindowText(_T("状态:  转换成功!"));
	}
    
	 
}

/*

函数名称:Compare
函数功能:比较Md5大小
输入参数:unsigned char * str1;
输入参数:unsigned char * str2;
输出参数:无
返回值  :>0大于,=0等于,<0小于

*/
static int  Compare(unsigned char *str1,unsigned char *str2)
{   
	int i;
    for(i=0;i<16;i++)
        if(str1[i]>str2[i]) return 1;
		else if(str1[i]<str2[i]) return -1;
	return 0;
}
/*

函数名称:swap
函数功能:Md5交换
输入参数:unsigned char * char_array;指向md5
输入参数:int i;
输入参数:int j;
输出参数:无
返回值  :无

*/
static void swap(unsigned char * char_array, int i, int j)
{
    
   char temp[17];
    
   memcpy(temp,char_array+16*i,16);
   memcpy(char_array+16*i,char_array+16*j,16);
   memcpy(char_array+16*j,temp,16);

}
/*

函数名称:q_sort
函数功能:对Md5进行快速排序
输入参数:unsigned char * char_array;指向md5
输入参数:int left;
输入参数:int right;
输出参数:无
返回值  :无

*/
static void q_sort(unsigned char * char_array, int left, int right)
{
	 int current, last;
     if (left >= right)
        return;
     swap(char_array, left, (left+right)/2);
 
     last = left;
     for (current = left + 1; current <= right; current++)
	 {
          //if(Compare(char_array+16*current,char_array+16*left)<0)
            //  swap(char_array, ++last, current); 
		 if(Compare(char_array+16*current,char_array+16*left)<0 )
			 swap(char_array, ++last, current);  //modify by lqy
  
	 }

     swap(char_array, left, last);		//确定每个元素的位置,left偏移的元素即为last处元素by lqy
     q_sort(char_array, left, last-1);
     q_sort(char_array, last+1, right);
}
/*

函数名称:ConvertMd5
函数功能:将16字节Md5转换成32字节形式
输入参数:char * szMd5;16字节Md5
输出参数:char * szString;转换后的32字节字符串
返回值  :无

*/
static void ConvertMd5(char *szMd5,char *sz32Md5)
{
	int i; 
	for(i=0;i<16;i++){ 
		sz32Md5[2*i]=(int)(szMd5[i]&0xf0)/16;
		sz32Md5[2*i]+=(sz32Md5[2*i]<10)?'0':'A'-10;
		sz32Md5[2*i+1]=szMd5[i]&0x0f;
		sz32Md5[2*i+1]+=(sz32Md5[2*i+1]<10)?'0':'A'-10;
	}

}

static void	stringToken(char *linebuf,char *buf1)
{
	int	bufPosition;
	int	tokenPosition;

       /*getting rid of ' ' and '\t' characters before "linebuf"*/ 

	for (bufPosition =0;(linebuf[bufPosition] == ' '||linebuf[bufPosition]== '\t')
		&&bufPosition<strlen(linebuf);bufPosition++){}

       /*get a line value according to '\n' character*/

	for (tokenPosition = 0;linebuf[bufPosition]!='\n' && bufPosition<strlen(linebuf);bufPosition++)
	{
		if(linebuf[bufPosition]!=' ')
		{   
			buf1[tokenPosition] = linebuf[bufPosition];
		    tokenPosition++;
		}
	}
	buf1[tokenPosition]=0;
}

/*
函数名称:MakeMiddleFile
函数功能:生成中间文件
输入参数:无
输出参数:无
返回值  :0 成功,其他失败
*/

int CUrl2Md5Dlg::MakeMiddleFile()
{
	FILE * sfp=NULL,*dfp=NULL;
	char szURL[100],szURLtmp[100],szRecord[200];  //szURLtmp is added by lfc
    char sz32Md5[33];

	//unsigned char szMd5[16];  

	unsigned char szMd5[17];		//modify by lqy

	unsigned char *pMd5=NULL,*pOldMd5=NULL;
    int nNum=0; 
	int i=0;
	int repeatflag=0;   //added by lfc
	time_t  CurrentTime;
	CTime BeginTime,EndTime;
	
	if((sfp=fopen((LPCTSTR)m_cSourceFile,"r"))==NULL)
	{
		return -1;
	}

	if((dfp=fopen((LPCTSTR)m_cObjectFile,"w+"))==NULL)
	{
		return -2;
	}
    
   GetDlgItem(IDC_TIME)->SetWindowText(_T("时间: "));
   time(&CurrentTime);  // Get the current time from the operating system.
                       
   BeginTime=(CurrentTime); 
  
	while(fgets(szURLtmp,sizeof(szURLtmp),sfp)!=NULL){

        stringToken(szURLtmp,szURL); //added by lfc

		if(szURL[0]==0)
			continue;

		LowerCase(szURL);//转换成小写字母

		//生产MD5值
		memset(szMd5, 0, 17);   //add by lqy
		ProducePropertyValue(szMd5, (unsigned char *)szURL, strlen(szURL));
        
		pOldMd5 = pMd5;

		//The following is added by lfc on 2002-07-31
		repeatflag=0;   
		for(i=0;i<nNum;i++)
		{
			//是否重复,开始查找指针pMd5,重复去掉此url add by lqy

			if(memcmp(szMd5,pMd5+i*16,16)==0)  
			{
				repeatflag=1;
				break;
			}
		}
		if(repeatflag==0)   //addition is end by lfc
		{
			//changes the size of an allocated memory block by lqy

			if((pMd5=(unsigned char*)realloc(pMd5,16*(nNum+1)))==NULL)
			{
			    if(pOldMd5)	free(pOldMd5);
			    fclose(sfp);
		        fclose(dfp);
			    return -3; 
			}
		    memcpy((void *)(pMd5+16*nNum),(void *)szMd5,16);
		    nNum++;
		}		       
	}
	
	//对16字节的主md5值进行快速排序

    q_sort(pMd5,0,nNum-1);

	//生成目标文件
	i=0;
	while(i<nNum)
	{
		memcpy(szMd5,pMd5+i*16,16);
		
		//将16字节md5值转换为32字节	
		ConvertMd5((char *)szMd5,sz32Md5);
		
		//属性值(001001001) 语言中类(001) 运营方式(001)
		strAtt =strAtt + "001001";
		
		strncpy(szRecord,strAtt,16);
		
		memcpy(szRecord+15,sz32Md5,32); 
		
		//辅MD5值全部为0
		sprintf(szRecord+47,"%035d",0);

		if(m_nMethod)								//del  the record
			memcpy((void*)(szRecord+82),"000",3);
		else										//add	the record
			memcpy((void*)(szRecord+82),"001",3);

		fwrite((void*)szRecord,85,1,dfp);	
		
		if(i<nNum-1)
			fprintf(dfp,"\n");
		i++;
	}

	if(pMd5)
		free(pMd5);	

	if(ferror(sfp))
	{
		fclose(sfp);
		fclose(dfp);
		return -3;
	}
	 
    time(&CurrentTime);  // Get the current time from the operating system.
                      
    EndTime=(CurrentTime);
	m_cUseTime=EndTime-BeginTime;
	 
    CString UsedTime =m_cUseTime.Format("时间: %D天-%H小时-%M分-%S秒");
	GetDlgItem(IDC_TIME)->SetWindowText(UsedTime);
	
	fclose(sfp);
	fclose(dfp);
	
	return 0;
}
/*

函数名称:LowerCase
函数功能:将字符串转换成小写
输入参数:char * szString;要转换的字符串
输出参数:char * szString;转换后的字符串
返回值  :无

*/

void CUrl2Md5Dlg::LowerCase(char * szString)
{
	for(unsigned int i=0;i<strlen(szString);i++){
		szString[i]=tolower(szString[i]);
	}
}

void CUrl2Md5Dlg::OnCancel() 
{	
	CDialog::OnCancel();
}

void CUrl2Md5Dlg::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	
	POSITION pos = m_ListCtrl.GetFirstSelectedItemPosition();
	while (pos)
   {
      int nItem = m_ListCtrl.GetNextSelectedItem(pos);
	  strAtt = m_ListCtrl.GetItemText(nItem, 0);
   }

	*pResult = 0;
}

⌨️ 快捷键说明

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