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

📄 pubdecrypdlg.cpp

📁 rsa加密算法的c++实现,此程序实现利用公钥解密
💻 CPP
字号:
// pubdecrypDlg.cpp : implementation file
//

#include "stdafx.h"
#include "pubdecryp.h"
#include "pubdecrypDlg.h"
#include <string.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()

/////////////////////////////////////////////////////////////////////////////
// CPubdecrypDlg dialog

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

void CPubdecrypDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPubdecrypDlg)
	DDX_Text(pDX, IDC_EDIT1, m_pubkeypath);
	DDX_Text(pDX, IDC_EDIT3, m_sigpath);
	DDX_Text(pDX, IDC_EDIT4, m_messpath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPubdecrypDlg, CDialog)
	//{{AFX_MSG_MAP(CPubdecrypDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_LoadPubKey, OnLoadPubKey)
	ON_BN_CLICKED(IDC_LoadSigText2, OnLoadSigText2)
	ON_BN_CLICKED(IDC_LoadSigText3, OnLoadSigText3)
	ON_BN_CLICKED(IDC_Verify, OnVerify)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPubdecrypDlg message handlers

BOOL CPubdecrypDlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CPubdecrypDlg::OnLoadPubKey() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CFileDialog fileDlg(TRUE);
 	fileDlg.m_ofn.lpstrTitle="导入公钥";
 	fileDlg.m_ofn.lpstrFilter="Pub Files(*.txt)\0*.txt\0\0\0";
 	fileDlg.m_ofn.lpstrDefExt="txt";
 	if(IDOK==fileDlg.DoModal())
 	{
		m_pubkeypath = fileDlg.GetPathName();
	}

	UpdateData(false);	
}

void CPubdecrypDlg::OnLoadSigText2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CFileDialog fileDlg(TRUE);
 	fileDlg.m_ofn.lpstrTitle="导入签名文本";
 	fileDlg.m_ofn.lpstrFilter="Txt Files(*.txt)\0*.txt\0\0\0";
 	fileDlg.m_ofn.lpstrDefExt="txt";
	if( fileDlg.DoModal() == IDOK )
	{
		m_sigpath = fileDlg.GetPathName();		
	}
	UpdateData(false);
}

void CPubdecrypDlg::OnLoadSigText3() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CFileDialog fileDlg(false);
 	fileDlg.m_ofn.lpstrTitle="保存消息";
 	fileDlg.m_ofn.lpstrFilter="Txt Files(*.txt)\0*.txt\0\0\0";
 	fileDlg.m_ofn.lpstrDefExt="txt";
	if( fileDlg.DoModal() == IDOK )
	{
		m_messpath = fileDlg.GetPathName();		
	}
	UpdateData(false);
}

void CPubdecrypDlg::OnVerify() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_pubkeypath.IsEmpty() || m_sigpath.IsEmpty() || m_messpath.IsEmpty() )
	{
		MessageBox("请输入路径");
		return;
	}

	//导入公钥
	fstream fout2 (m_pubkeypath, ios::in | ios::binary);
 	char* pBuf = new char[6];
	int pos = 0;
	memset(pBuf, 0, 6);
	while(!fout2.eof())
		{
			fout2.read(pBuf,5);
			if(memcmp(pBuf, "PUBK|", 5) == 0)
				{
					MessageBox("success");
					pos = fout2.tellg();
					break;
				}
			fout2.seekg(-4, ios::cur);
		}
		if(fout2.eof())
		{
			MessageBox("公钥格式错误");
			return ;
		}

		char* pubklen1 = new char[4];
		char* pubklen2 = new char[4];
		memset(pubklen1, 0, 4);
		memset(pubklen2, 0, 4);
		int count1 = 0;
		int count2 = 0;
		fout2.read(pubklen1, 3);
		fout2.read(pubklen2, 3);
		count1 = atoi(pubklen1);
		count2 = atoi(pubklen2);
		char* pubkp = new char [count1+1];
		char* pubkm = new char [count2+1];
		memset(pubkp, 0, count1);
		memset(pubkm, 0, count2);
		fout2.read(pubkp, count1);
		fout2.read(pubkm, count2);
		LINT p = LINT(pubkp, 16);
		LINT mod = LINT(pubkm, 16);
		PKEYSTRUCT key;
		key.mod = mod;
		key.pubexp = p;
		key.bitlen_mod = ld (key.mod);
		key.bytelen_mod = key.bitlen_mod >> 3;
		if ((key.bitlen_mod % 8) > 0)
		{
		++key.bytelen_mod;
		}
		RSApub pubkey(key);
fout2.close ();
		delete []pubklen1;
		delete []pubklen2;
		delete []pubkp;
		delete []pubkm;
 //		

//	fout2 >> pubkey;
 	fout2.close();


	//验证模块
		fstream fin1 (m_sigpath , ios::in | ios::binary);
		fstream fout(m_messpath, ios::out| ios::binary);
		int tmeleng = 0;
		char* block = new char[256];
		memset(block, 0, 256);
		CString pBuf1("");
		fin1.read(pBuf1.GetBuffer(4),4);
		while(!fin1.eof())
		{
			if(memcmp(pBuf1, "SIG|", 4) == 0)
				{
					break;
				}
			fin1.seekg(-4, ios::cur);
			fin1.read(pBuf1.GetBuffer(4),4);
		}
		pBuf1.ReleaseBuffer(4);
		if(fin1.eof())
		{
			MessageBox("签名格式错误");
			return ;
		}
		UCHAR* sigtext = new UCHAR[200];
		char* bytecount = new char[4];
		memset(bytecount, 0, 4);
		int count = 0;
		while( !fin1.eof() )
		{
			fin1.read(bytecount, 3);
			count = atoi(bytecount);
			fin1.read(block, count) ;
			if(block[0]==0 && block[1] == 0)
				break;
			LINT ciphertext = LINT(block, 16);
			sigtext = pubkey.verify(&tmeleng, ciphertext);
			fout.write(sigtext, strlen((char*)sigtext));
			memset(block, 0, 256);
	//		memset(sigtext,0,tmeleng);
		}	
	//	memset(sigtext,0,200);
	//	delete []sigtext;
		fin1.close();
		fout.close();
		MessageBox("解密成功");
  UpdateData(false);
}

⌨️ 快捷键说明

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