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

📄 cyptdlg.cpp

📁 可以实现RIJNDEAL和IDEA 2种加密方法的完整程序,还可以看到加密的所用时间的情况
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CyptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Cypt.h"
#include "CyptDlg.h"

#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define TURN 100000
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CCyptDlg dialog




CCyptDlg::CCyptDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCyptDlg::IDD, pParent)
	, m_strPlain(_T(""))
	, m_strCipher(_T(""))
	, m_strPsw(_T(""))
	, m_strPlain2(_T(""))
	, m_dwTime2(0)
	, m_dwCycle2(0)
	, m_dwSpeed2(0)
	, m_dwTime(0)
	, m_dwCycle(0)
	, m_dwSpeed(0)
	, m_comboValue(_T(""))
	, m_comboTesttypeValue(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCyptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_PLAIN, m_strPlain);
	DDX_Text(pDX, IDC_EDIT_CIPHER, m_strCipher);
	DDX_Control(pDX, IDC_COMBO_ALGRI, m_comboAlgi);
	DDX_Text(pDX, IDC_EDIT_PSW, m_strPsw);
	DDX_Text(pDX, IDC_EDIT_PLAIN2, m_strPlain2);
	DDX_Text(pDX, IDC_EDIT_TIME2, m_dwTime2);
	DDX_Text(pDX, IDC_EDIT_CYCLE2, m_dwCycle2);
	DDX_Text(pDX, IDC_EDIT_SPEED2, m_dwSpeed2);
	DDX_Text(pDX, IDC_EDIT_TIME, m_dwTime);
	DDX_Text(pDX, IDC_EDIT_CYCLE, m_dwCycle);
	DDX_Text(pDX, IDC_EDIT_SPEED, m_dwSpeed);
	DDX_CBString(pDX, IDC_COMBO_ALGRI, m_comboValue);
	DDX_Control(pDX, IDC_COMBO_TESTTYPE, m_comboTesttype);
	DDX_CBString(pDX, IDC_COMBO_TESTTYPE, m_comboTesttypeValue);
}

BEGIN_MESSAGE_MAP(CCyptDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, &CCyptDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_DECRYPT, &CCyptDlg::OnBnClickedButtonDecrypt)
	ON_CBN_SELCHANGE(IDC_COMBO_ALGRI, &CCyptDlg::OnCbnSelchangeComboAlgri)
	ON_CBN_SELCHANGE(IDC_COMBO_TESTTYPE, &CCyptDlg::OnCbnSelchangeComboTesttype)
END_MESSAGE_MAP()


// CCyptDlg message handlers

BOOL CCyptDlg::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

	
	m_comboTesttype.AddString( _T("固定钥钥加密") );
	m_comboTesttype.AddString( _T("变钥加密") );

	m_comboTesttypeValue = _T("固定钥钥加密");

	m_nSeltype = 0; 

	m_comboAlgi.AddString( _T("Rijndael") );
	m_comboAlgi.AddString( _T("IDEA") );
	m_comboValue = _T("Rijndael");

	m_nSel = 0;

	UpdateData(0);

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

void CCyptDlg::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 CCyptDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCyptDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CCyptDlg::OnBnClickedOk()
{
	UpdateData();
	m_strCipher.Empty();
	if( m_nSel == 0 )
	{
		CString strKey( m_strPsw );
		int i,len = m_strPsw.GetLength();
		
		if( len > 16 )
		{
			len = 16;
			strKey = m_strPsw.Left(16);
		}
		WideCharToMultiByte(	CP_ACP,
								WC_COMPOSITECHECK,
								strKey.GetBuffer(len),
								len,
								(LPSTR)m_aesKey,
								len,
								NULL,
								NULL
							);
		for( i=len;i<16;i++)
			m_aesKey[i]=i;
		m_aes.Init( m_aesKey );
		AESDeEncrypt( m_strPlain, 1 );
	}
	else
	{
		CString strKey( m_strPsw );
		int i,len = m_strPsw.GetLength();
		BYTE key[9];
		if( len > 8 )
		{
			len = 8;
			strKey = m_strPsw.Left(8);
		}
		WideCharToMultiByte(	CP_ACP,
								WC_COMPOSITECHECK,
								strKey.GetBuffer(len),
								len,
								(LPSTR)key,
								len,
								NULL,
								NULL
							);
		for( i=len;i<8;i++)
			key[i]=i;
		
		for( i=0;i<8;i++)
			m_ideaKey[i+1] = key[i];
		m_idea.Init( m_ideaKey );
		IDEADeEncrypt( m_strPlain, 1);
	}
//	OnOK();
}

bool CCyptDlg::IDEADeEncrypt(CString strText, bool bEn)
{
	if( strText.IsEmpty() ) return 0;
	int i,j, dwSize, len = strText.GetLength();
	if( bEn )
	{
		if( len%4 != 0 ) dwSize = len + 4-len%4;
		else dwSize = len;
		unsigned *pBuf = new unsigned[ dwSize ];
		unsigned *sRes = new unsigned[ dwSize ];

		for(i=0;i<len;i++)
		{
			pBuf[ i] = strText.GetAt(i);
		}

		if( len%4 != 0 )
		{
			for(i=1;i<=4-len%4;i++)
			{
				pBuf[ dwSize - i ] = 0;
			}
		}

		/*************************************************/
		double dCpuHz=1.8;
		long HighStart,LowStart,HighEnd,LowEnd;
		unsigned long numhigh,numlow;
		
		//获取代码运行开始时cpu内部计数器的值
		__asm              
		{
			RDTSC 
				mov HighStart, edx
				mov LowStart, eax
		}
		/*************************************************/

		if( m_nSeltype == 0 )
		{
			for( j=0;j<TURN;j++)
			{
				for(i=0;i<dwSize/4;i++)
				{
					m_idea.Encrypt( pBuf + i*4, sRes + i*4 );
				}
			}
		}
		else
		{
			for( j=0;j<TURN;j++)
			{
				m_idea.Init( m_ideaKey );
				for(i=0;i<dwSize/4;i++)
				{
					m_idea.Encrypt( pBuf + i*4, sRes + i*4 );
				}
			}
		}
		/*************************************************************/
		__asm
		{
			RDTSC
				mov HighEnd, edx
				Mov LowEnd,  eax
				;获取两次计数器值得差
				sub eax,  LowStart
				cmp    eax,  0       ; 如果低32的差为负则求反,因为第二次取得永远比第一次的大
				jg     L1
				neg     eax 
				jmp     L2
				L1: mov numlow,  eax
				L2: sbb edx,  HighStart
			mov numhigh, edx
		}
		m_dwCycle2 = numhigh * pow( 2.0,32 ) + numlow;				m_dwCycle2 /= TURN;
		m_dwTime2 = m_dwCycle2/dCpuHz/1000000000;					m_dwTime2 /=  TURN;
		m_dwSpeed2 = dwSize/(m_dwCycle2/dCpuHz/1000000000)/1024;
		/***************************************************************/

		delete []pBuf;
	
		CString str;
		for(i=0;i<dwSize;i++)
		{
			str.Format( _T("%x "), sRes[i] );
			if( str.GetLength() == 4 )
				str = _T("0") + str;
			else if( str.GetLength() == 3 )
				str = _T("00") + str;
			else if( str.GetLength() == 2 )
				str = _T("000") + str;
			m_strCipher += str;
		}
		delete []sRes;
	}
	else
	{
		CString str;
		int j,turn = len/5;
		unsigned *pBuf = new unsigned[ turn ];
		unsigned *sRes = new unsigned[ turn ];

⌨️ 快捷键说明

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