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

📄 rsadlg.cpp

📁 在VC环境下实现对文件和明文的加密解密
💻 CPP
字号:
//UpdateData(true);		用于将屏幕上控件中的数据交换到变量中。 
//UpdateData(false);	用于将数据在屏幕中对应控件中显示出来

#include "stdafx.h"
#include "RSA.h"
#include "RSADlg.h"
#include"fstream"
#include"string"
using namespace std;

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

/////////////////////////////////////////////////////////////////////////////
// CRSADlg dialog

CRSADlg::CRSADlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRSADlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRSADlg)
	m_N = _T("");
	m_E = _T("");
	m_D = _T("");
	m_IN = _T("");
	m_OUT = _T("");
	ready=0;
	m_Len=0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRSADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRSADlg)
	DDX_Text(pDX, IDC_N, m_N);
	DDX_Text(pDX, IDC_E, m_E);
	DDX_Text(pDX, IDC_D, m_D);
	DDX_Text(pDX, IDC_INPUT, m_IN);
	DDX_Text(pDX, IDC_OUTPUT, m_OUT);
	DDX_CBIndex(pDX, IDC_Len, m_Len);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRSADlg, CDialog)
	//{{AFX_MSG_MAP(CRSADlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_JIEMI, OnJiemi)
	ON_BN_CLICKED(IDC_JIAMI, OnJiami)
	ON_BN_CLICKED(IDC_GETNED, OnGetned)
	ON_BN_CLICKED(IDC_PUTNED, OnPutned)
	ON_BN_CLICKED(IDC_FILE, OnFile)
	ON_BN_CLICKED(IDC_FILE2, OnFile2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRSADlg message handlers

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

void CRSADlg::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 CRSADlg::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 CRSADlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//以上是做界面时,系统自动生成


//************************************************************
void CRSADlg::OnJiami()                           //加密
{
	if(ready==0)                                  //ready是编辑框的初值
	{
		m_OUT=_T("请先输入N、D、E的值");               //输出提示
        UpdateData(FALSE);                        //更新编辑框中的数据成员
		return;
	}
	UpdateData(TRUE);                              //从编辑框中读出数据到数据成员中
    if(m_IN.GetLength()>256)
	{
		m_OUT=_T("N,E最大为256位十六进制数");
		UpdateData(FALSE);
		return;
	}
	for(int i=0;i<m_IN.GetLength();i++)
	{
		if((m_IN[i]<'0')||((m_IN[i]>'9')&&(m_IN[i]<'A'))||((m_IN[i]>'F')&&(m_IN[i]<'a'))||
		   (m_IN[i]>'f'))
		{
			m_OUT=_T("加密数据应为为十六进制数");
			UpdateData(FALSE);
			return;
		}
	}
	M.Get(m_IN);                         //将待加密的数据M读入                                   
		if(m_IN.GetLength()<1)                                  //ready是编辑框的初值
	{
		m_OUT=_T("请先输入明文");                 //输出提示
        UpdateData(FALSE);                        //更新编辑框中的数据成员
		return;
	}
	if(M.Cmp(N)>=0)
	{
		m_OUT=_T("加密数据应小于N");                  
        UpdateData(FALSE);
		return;
	}
	C.Mov(M.RsaTrans(E,N));              //加密算法:C=M^Emod n
	C.Put(m_OUT);
	UpdateData(FALSE);
}

void CRSADlg::OnJiemi()                                //解密
{
	
	if(ready==0||(M.m_nLength==1&&M.m_ulValue[0]==0))      //判断明文是否为空和是否加密
	{
		m_OUT=_T("请先进行加密");
        UpdateData(FALSE);
		return;
	}
	C.Get(m_OUT);
	M.Mov(C.RsaTrans(D,N));                           //解密算法:M=C^Dmod N
	M.Put(m_OUT);
	UpdateData(FALSE);
}


void CRSADlg::OnGetned()                            //获取N,D,E
{
	UpdateData(TRUE);
	int len=2;
	for(int i=0;i<m_Len;i++)
	{  
		len*=2;
	}
	P.Mov(0);                                      //调用大数库的函数,进行有关运算
	Q.Mov(0);
	N.Mov(0);
	E.Mov(0);
	P.GetPrime(len);                               //随机产生P,Q
	Q.GetPrime(len);
	N.Mov(P.Mul(Q));                               //计算机N值:N=P*Q
	N.Put(m_N);                                    //输出N
	P.m_ulValue[0]--;                              //P,Q分别减1
	Q.m_ulValue[0]--;
	N1.Mov(P.Mul(Q));                              //N1=(p-1)*(q-1)
	D.GetPrime(1);                                 //随机产生D
	D.Put(m_D);
	E.Mov(D.Euc(N1));                              //计算E的值
	E.Put(m_E);
	ready=1;
	UpdateData(FALSE);                             //更新编辑框
}

void CRSADlg::OnPutned()
{
	UpdateData(TRUE);
	ready=0;
	if(m_N.GetLength()>256)
	{
		m_OUT=_T("N不能大于256位");
		UpdateData(FALSE);
		return;
	}
	if(m_E.GetLength()>256)
	{
		m_OUT=_T("E不能大于256位");
		UpdateData(FALSE);
		return;
	}
    if(m_D.GetLength()>10)
	{
		m_OUT=_T("D不能大于10位");
		UpdateData(FALSE);
		return;
	}
	for(int i=0;i<m_N.GetLength();i++)
	{
		if((m_N[i]<'0')||
		   ((m_N[i]>'9')&&(m_N[i]<'A'))|| ((m_N[i]>'F')&&(m_N[i]<'a'))||(m_N[i]>'f'))
		{
			m_OUT=_T("N必须为数据应为为十六进制数");
			UpdateData(FALSE);
			return;
		}
	}
	for(i=0;i<m_E.GetLength();i++)
	{
		if((m_E[i]<'0')||
		   ((m_E[i]>'9')&&(m_E[i]<'A'))||((m_E[i]>'F')&&(m_E[i]<'a'))||(m_E[i]>'f'))
		{
			m_OUT=_T("E必须为数据应为为十六进制数");
			UpdateData(FALSE);
			return;
		}
	}
	for(i=0;i<m_D.GetLength();i++)
	{
		if((m_D[i]<'0')||
		   ((m_D[i]>'9')&&(m_D[i]<'A'))||((m_D[i]>'F')&&(m_D[i]<'a'))||(m_D[i]>'f'))
		{
			m_OUT=_T("D必须为数据应为为十六进制数");
			UpdateData(FALSE);
			return;
		}
	}
	N.Get(m_N);               //将输入的m_N,m_D,m_E,分别赋给N,D,E
	D.Get(m_D);
	E.Get(m_E);
	if((N.Cmp(E)<=0)||(N.Cmp(D)<=0))               //比较N,D,E的大小
	{
		m_OUT=_T("N不能小于D、E");                  
		UpdateData(FALSE);
		return;
	}
	ready=1;
}

void CRSADlg::OnFile()
{
	ofstream out("secretfile.txt",ios::binary);             //以二进制方式打开文件
	if(!out)
		return;
	string s;
	int len=N.m_nLength-1;
	int N_len=N.m_nLength;
	int length=len;
	len=len*4;                                        //16进制一位为二进制4位
	N1=P.Sub(1).Mul(Q.Sub(1));                   //重载运算符"-"和"*",原形为N1=(P-1)(Q-1)
	ifstream fin("file1.txt",ios::binary); //将待加密文件中的数据
	char ch;                                    //加密后写入加密文件中
	while(fin.get(ch))
		s+=ch;
	unsigned long num=0;
	unsigned long value=0;
	int k=0;
	int j=0;
	int count=0;
	for(int i=0;i<s.length();i++)
	{
		count++;
		num=num<<8;                                 //左移8位        
		num+=s[i];
		k++;
		if(k==4)
		{
			M.m_ulValue[j++]=num;
			num=0;
			k=0;
			if(count>=len)
			{
				count=0;
				j=0;
				C=M.RsaTrans(E,N);
				for(int m=0;m<C.m_nLength;m++)
					out.write((char *)&C.m_ulValue[m],sizeof(unsigned long));
				if(C.m_nLength<N.m_nLength)
				{
					value=0;
					for(int n=0;n<N.m_nLength-C.m_nLength;n++)
						out.write((char *)&value,sizeof(unsigned long));
				}
				C.Mov(0);
				M.Mov(0);
				continue;
			}
			if(i<s.length()-1)
			{
				M.m_nLength++;
				
			}
		}
	}

	if(num!=0)
	{
		M.m_ulValue[M.m_nLength-1]=num;
	}
	if(M.m_nLength>1||(M.m_nLength==1&&M.m_ulValue[0]!=0))
	{
		C=M.RsaTrans(E,N);
		for(i=0;i<C.m_nLength;i++)
		{
			out.write((char *)&C.m_ulValue[i],sizeof(unsigned long));
			
		}
		if(C.m_nLength<N.m_nLength)
		{
			value=0;
			for(int n=0;n<N.m_nLength-C.m_nLength;n++)
			out.write((char *)&value,sizeof(unsigned long));
		}
	}

	out.flush();
	out.close();
	m_OUT=_T("文件加密成功");
	UpdateData(FALSE);
}

void CRSADlg::OnFile2() 
{	
	int count=0;
	int k=0;
	int i=0;
	int j=0;
	int num=0;

	ifstream in("secretfile.txt",ios::binary);
	if(!in)
		return;

	ofstream out1("file2.txt",ios::binary);
	if(!out1)
		return;
	unsigned long value=0;
	C.Mov(0);
	if(!in.read((char *)&num,sizeof(unsigned long)))             //取出加密文件开始解密       
		return;
	while(1)
	{
		C.m_ulValue[k]=num;
		k++;
		if(k==N.m_nLength)
		{
			for(i=0;i<35;i++)
			{
				if(C.m_ulValue[34-i]!=0)
				{
					C.m_nLength=35-i;
					break;
				}
			}
			M=C.RsaTrans(D,N);
			for(i=0;i<M.m_nLength;i++)
			{
				value=M.m_ulValue[i];
				for(j=0;j<4;j++)
				{
					char c=value>>((3-j)*8);
					if(c!=0){
						out1<<c;
					}
				}
			}
			k=0;
			C.Mov(0);

		}
		if(!in.read((char *)&num,sizeof(unsigned long)))
			break;
	}
	out1.flush();                           //清除缓冲
	out1.close();
	m_OUT=_T("文件解密成功");
	UpdateData(FALSE);
}

⌨️ 快捷键说明

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