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

📄 aescodedlg.cpp

📁 实现对字符串和整篇文章的AES加密和解密操作
💻 CPP
字号:
// AesCodeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AesCode.h"
#include "AesCodeDlg.h"
#include "HexStrToByte.h"
#include "ByteToHex.h"
#include "Aes.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()

/////////////////////////////////////////////////////////////////////////////
// CAesCodeDlg dialog

CAesCodeDlg::CAesCodeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAesCodeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAesCodeDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAesCodeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAesCodeDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAesCodeDlg, CDialog)
	//{{AFX_MSG_MAP(CAesCodeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BAesEn, OnBAesEn)
	ON_BN_CLICKED(IDC_BAesDe, OnBAesDe)
	ON_BN_CLICKED(IDC_BFile, OnBFile)
	ON_BN_CLICKED(IDC_BFileEn, OnBFileEn)
	ON_BN_CLICKED(IDC_BFileDe, OnBFileDe)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAesCodeDlg message handlers

BOOL CAesCodeDlg::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
	SetDlgItemText(IDC_EAesEn,"FEF4128550E8E742F1C31F03DE0C3802");
	EnDe_filename="";
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAesCodeDlg::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 CAesCodeDlg::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 CAesCodeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//Aes字符串加密
void CAesCodeDlg::OnBAesEn() 
{
	// TODO: Add your control notification handler code here
	CString str1;
	CString str2 = "",str3;
	unsigned char inBuff[17],ouBuff[17];
	memset(inBuff,0,17);
	memset(ouBuff,0,17);

    CHexStrToByte ToByte;
	GetDlgItemText(IDC_EAesEn,str1);
	ToByte.SetHexStrToByte(str1,inBuff);

    unsigned char m_Key[24] = {0x4E,0x7E,0x6D,0x85,0xC4,0x0D,0xF5,0xA8,0x68,0x58,0x7C,0x3A,
		                       0xAF,0x0A,0xB8,0xEC,0xD1,0x9A,0x60,0xF2,0xE5,0x8F,0x64,0x57};

	Aes aes(24,(unsigned char*)m_Key);
		
	if(strlen((char*)inBuff)>16)
	MessageBox("本例只能加密16字节的字符串,大于截断!");

	aes.Cipher(inBuff,ouBuff); 

	for(int i=0;i<16;i++)
	{
		str3.Format("%02X",ouBuff[i]);   
		str2 += str3;
	}
	SetDlgItemText(IDC_EAesEn,str2);
}
////////////////////////////////////////////////////////////////////////////////////////////////

//Aes字符串解密
void CAesCodeDlg::OnBAesDe() 
{
	// TODO: Add your control notification handler code here
	CString str1,str2;
	CByteToHex ToHex;
	unsigned char inBuff[33],ouBuff[17];  
										  
	memset(inBuff,0,33);
	memset(ouBuff,0,17);
    
	unsigned char m_Key[24] = {0x4E,0x7E,0x6D,0x85,0xC4,0x0D,0xF5,0xA8,0x68,0x58,0x7C,0x3A,
		                       0xAF,0x0A,0xB8,0xEC,0xD1,0x9A,0x60,0xF2,0xE5,0x8F,0x64,0x57};
	
	Aes aes(24,(unsigned char*)m_Key);

	GetDlgItemText(IDC_EAesEn,(char*)inBuff,33);

	unsigned char temp[17];

	for(int j=0;j<16;j++)
	{
		temp[j]=char2num(inBuff[2*j])*16+char2num(inBuff[2*j+1]);
	}
    
	aes.InvCipher(temp,ouBuff);

	str2 = ToHex.BytedToHexed((char *)ouBuff,16);

	SetDlgItemText(IDC_EAesDe,str2);
}

////////////////////////////////////////////////////////////////////////////////////////////////
//字符ASCII码值到字符字面值的转换 如 '0'转换成0, 'a'转换成10
int CAesCodeDlg::char2num(char ch)
{
	if(ch>='0'&&ch<='9')return ch-'0';
	else if(ch>='A'&&ch<='F')return ch-'A'+10;
	return -1;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//文件选择框
void CAesCodeDlg::OnBFile() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fdlg(1, NULL, NULL, OFN_HIDEREADONLY, "All Files(*.*)|*.*||");
	if(IDOK != fdlg.DoModal())
	return;
	EnDe_filename = fdlg.GetPathName();	
	SetDlgItemText(IDC_EFile, EnDe_filename);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//Aes文件加密
void CAesCodeDlg::OnBFileEn() 
{
	// TODO: Add your control notification handler code here
	CHexStrToByte ToByte;
    CString str="";		
	if(EnDe_filename == "")
	return;
	FILE* finput;
	FILE* foutput;
	finput = fopen((LPCTSTR)EnDe_filename, "rb");
	if(!finput)
	{
		MessageBox("文件打开错误!", "出错", MB_OK);
		return;
	}
	fseek(finput, 0, SEEK_END);
	long lFileLen = ftell(finput); 
	fseek(finput, 0, SEEK_SET);
	long blocknum = lFileLen/16;
	long leftnum = lFileLen%16;
	EnDe_filename += ".en";
	foutput = fopen((LPCTSTR)EnDe_filename, "wb");
	if(!foutput)
	{
		MessageBox("文件打开错误!","出错", MB_OK);
		fclose(finput);
		return;
	}
	unsigned char inBuff[17], ouBuff[17];
	unsigned char m_Key[24] = {0x4E,0x7E,0x6D,0x85,0xC4,0x0D,0xF5,0xA8,0x68,0x58,0x7C,0x3A,
		                       0xAF,0x0A,0xB8,0xEC,0xD1,0x9A,0x60,0xF2,0xE5,0x8F,0x64,0x57};
	
	Aes aes(24,(unsigned char*)m_Key);

	for(long i=0;i<blocknum;i++)
	{
		memset(inBuff, 0, 16);
		memset(ouBuff, 0, 16);
        fread(inBuff,1,16,finput);
		aes.Cipher(inBuff,ouBuff);
	    fwrite(ouBuff,1,16,foutput);
		
	}
	if(leftnum)
	{
		memset(inBuff, 0, 16);
		memset(ouBuff, 0, 16);
		fread(inBuff,1,leftnum,finput);
		aes.Cipher(inBuff,ouBuff);
	    fwrite(ouBuff,1,16,foutput);
	}

	fclose(finput);
	fclose(foutput);
	MessageBox("加密成功!");
	SetDlgItemText(IDC_EFile,EnDe_filename);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//Aes文件解密
void CAesCodeDlg::OnBFileDe() 
{
	// TODO: Add your control notification handler code here
	CByteToHex ToHex;
	CString str = "";
	if(EnDe_filename == "")
	return;
	FILE* finput;
	FILE* foutput;
	finput=fopen((LPCTSTR)EnDe_filename,"rb");
	if(!finput)
	{
		MessageBox("文件打开错误!","出错",MB_OK);
		return;
	}
	fseek(finput, 0, SEEK_END);
	long lFileLen = ftell(finput); 
	fseek(finput, 0, SEEK_SET);
	long blocknum = lFileLen/16;
	long leftnum = lFileLen%16;
	EnDe_filename+=".de";
	foutput=fopen((LPCTSTR)EnDe_filename,"wb");
	if(!foutput)
	{
		MessageBox("文件打开错误!","出错",MB_OK);
		fclose(finput);
		return;
	}
	unsigned char inBuff[17],ouBuff[17];
	unsigned char m_Key[24] = {0x4E,0x7E,0x6D,0x85,0xC4,0x0D,0xF5,0xA8,0x68,0x58,0x7C,0x3A,
		                       0xAF,0x0A,0xB8,0xEC,0xD1,0x9A,0x60,0xF2,0xE5,0x8F,0x64,0x57};
	
	Aes aes(24,(unsigned char*)m_Key);
	for(long i=0;i<blocknum;i++)
	{
		memset(inBuff,0,16);
		memset(ouBuff,0,16);
		fread(inBuff,1,16,finput);  
		aes.InvCipher(inBuff,ouBuff);
	    fwrite(ouBuff,1,16,foutput);
	}

	if(leftnum)
	{
		MessageBox("文件可能已损坏或非经aes加密过!");
	}
	fclose(finput);
	fclose(foutput);
	MessageBox("解密成功!");
}

⌨️ 快捷键说明

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