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

📄 md5dlg.cpp

📁 具有MD5和SHA1单向(哈希)函数算法加解密功能的小程序.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MD5Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "MD5.h"
#include "MD5Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMD5Dlg dialog

CMD5Dlg::CMD5Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMD5Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMD5Dlg)
	m_msg1 = _T("");
	m_msg2 = _T("");
	m_msg3 = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMD5Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMD5Dlg)
	DDX_Control(pDX, IDC_EDIT4, m_msg4);
	DDX_Control(pDX, IDC_BUTTON2, m_shajm);
	DDX_Control(pDX, IDC_BUTTON1, m_explain);
	DDX_Control(pDX, IDOK, m_jiami);
	DDX_Text(pDX, IDC_EDIT1, m_msg1);
	DDX_Text(pDX, IDC_EDIT2, m_msg2);
	DDX_Text(pDX, IDC_EDIT3, m_msg3);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMD5Dlg, CDialog)
	//{{AFX_MSG_MAP(CMD5Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnSHA)
	ON_BN_CLICKED(IDC_BUTTON2, OnDelare)
	ON_BN_CLICKED(IDC_BUTTON3, OnHELP)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMD5Dlg message handlers

BOOL CMD5Dlg::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_msg4.SetWindowText("  请使用“MD5/SHA算法说明”按钮进行切换,每按一次即显示MD5的算法描述,再按一次则显示SHA的算法描述。依次循环显示");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMD5Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CMD5Dlg::OnDestroy()
{
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}

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

UINT4 squ(int i)//求2的i次方
{
	UINT4 m=1;
	for(int k=1;k<=i;k++)
		m*=2;
	return m;
}

void CMD5Dlg::MINGWENERJINZHI()
{
        UpdateData(TRUE);
 		FILE *fp;
 		char ch;
 		static int i=0,j,str[8];
////////建立一个名为“明文处理后二进制表示.txt”保存好输入的明文二进制信息
 		fp=fopen("MD5明文处理后二进制表示.txt","w");//建立文件“明文二进制处理后表示.txt”
 		int m=m_msg1.GetLength();
 		for(i=0;i<m;i++)
 		{
			ch=m_msg1.GetAt(i);
 			for(j=0;j<8;j++)//把每个字符分别转换为二进制数
 			{
 				str[j]=ch%2;
 				ch=ch/2;
			}
 			for(j=7;j>=0;j--)
 				fprintf(fp,"%d",str[j]);//把每组八位二进制数依次存入文件中
 		}
////////填充原信息长度使其满足512的倍数余448
	    int n_binary=m_msg1.GetLength()*8;//字符个数计算
	    int temp=n_binary;
		int n=n_binary%512;//对512取余看其是否是512的倍数再余448
	    if(n==448)//如果刚好余448则追加64位表示原始信息长度的数据
	     ;
	    else if(n<448)//如果不满足n=448则对其实行追加至448,追加规则为追加的第一位为1其于为0直至n为448
		{
		   int i=1;
		   int k=448-n;
	       fseek(fp,0,SEEK_END);
	       fprintf(fp,"%d",1);
	       while(i<=k-1)//信息的填充
		   {
		      i++;
		      fseek(fp,0,SEEK_END);
	          fprintf(fp,"%d",0);
		   }
		}
		else if(n>448)
		{
		   int i=1;
		   int k=512-n;
	       fseek(fp,0,SEEK_END);
	       fprintf(fp,"%d",1);
	       while(i<=k+447)//信息的填充
		   {
		      i++;
		      fseek(fp,0,SEEK_END);
	          fprintf(fp,"%d",0);
		   }
		}
////////追加最后64位信息长度
 		j=63;
		int len_letter[64]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//保存原信息的64位长度 
	    while(temp>=1)//生成需追加的64位数
		{ 
          len_letter[j]=temp%2; 
          temp/=2; 
          j--; 
		}
		for(j=7;j>=0;j--)
	     for(i=0;i<=7;i++)//追加64位表示原始信息长度的数据
		{
			fseek(fp,0,SEEK_END);
			fprintf(fp,"%d",len_letter[j*8+i]);
		}

	     fclose(fp);
	
}


UINT4 CMD5Dlg::char_to_long(int l,int t)//将32位数组转换为4个字的长整型
{
	int i=0;
    UINT4 m[4]={0x0};
	FILE *fp;
	fp=fopen("MD5明文处理后二进制表示.txt","r+");
	fseek(fp,t*512+l*32,0);//找到第t次512位循环的第l个子数组的开始位置
	char buffer[33];
	if(!feof(fp))
		fread(buffer,sizeof(char),32,fp);
	int temp[4][8];
	for(int j=1;j<=4;j++)
      for(i=0;i<8;i++)
		  temp[j-1][i]=buffer[32-j*8+i];
    for(j=0;j<=3;j++)
	for(i=0;i<=7;i++)
	{
		if(temp[j][i]=='1')
			m[j]+=squ(7-i);
	}
	UINT4 mt=m[0]*squ(24)+m[1]*squ(16)+m[2]*squ(8)+m[3];
	fclose(fp);
    return mt;
}


void CMD5Dlg::OnOK() 
{
    UpdateData(TRUE);
	 if(m_msg1.IsEmpty())
	{
     m_msg1=' ';AfxMessageBox("无输入时默认明文信息为空!");
	} 
	MINGWENERJINZHI();	
	Init();
	UINT4 x[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	int i;	
    UINT4 q;
	if(count_file()%512==0)
		q=count_file()/512-1;//q个512位执行tp次 	
 	else
		q=count_file()/512;
	for(UINT4 k=0;k<=q;k++){ 
	UINT4 a=state[0],b=state[1],c=state[2],d=state[3];

/////////产生16个子串   
	for(i=0;i<=15;i++)
	x[i]=char_to_long(i,k);
	
/////////进入四轮主循环
	/*Round 1*/
  FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */

⌨️ 快捷键说明

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