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

📄 passdlg.cpp

📁 利用RSA加密算法对输入的原文进行加密
💻 CPP
字号:
// passDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RSA.h"
#include "passDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// passDlg dialog


passDlg::passDlg(CWnd* pParent /*=NULL*/)
	: CDialog(passDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(passDlg)
	m_in = _T("");
	//}}AFX_DATA_INIT
}


void passDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(passDlg)
	DDX_Text(pDX, IDC_IN, m_in);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(passDlg, CDialog)
	//{{AFX_MSG_MAP(passDlg)
	ON_BN_CLICKED(IDCANCLE, OnCancle)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// passDlg message handlers

BOOL passDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_out="";
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}



void passDlg::OnOK() 
{
	UpdateData(true);
    length=m_in.GetLength();
//	m_out="";
	m_out=MRSA(m_in);
//	m_in=m_out;
	UpdateData(false);
	//RSA(m_in);
	AfxMessageBox(m_out);
}

void passDlg::OnCancle() 
{
	// TODO: Add your control notification handler code here
    m_out=CRSA();


	AfxMessageBox(m_out);
}





CString passDlg::MRSA(CString m_str)
{
	CString temp_str;  ///it is a indirect variable
	CString ss,sss;
	temp_str=m_str;
	char str[300];
 //   figure[600]={0};/////the array is used to store the 
	unsigned __int64 temp;/////it is a indirect variable 

	for(int i=0;i<length;i++)
	{
		 str[i]=m_in.GetAt(i);
	}
	for( i=0;i<length;i++)
	{
		
		char c;
		c=temp_str.GetAt(i);
		ss.Format("%d",c);/////change to int
		figure[i]=atoi(ss);////////
		if(figure[i]>26)
		{
			figure[i]=figure[i]-26;////////
		}
	}
	if(length%2==0)       //the "length" is the string's length
	{
	    num=length/2;///////num is
	}
	else
	{
		num=length/2+1;
	}
    for( i=0;i<num;i++)
	{
		int j=2*i;
		figure[i]=figure[j]*100+figure[j+1];
	    temp=Encrypt(figure[i],i);
		figure[i]=temp;
    	sss.Format("%d",0);
		if(temp<10)
		{
			for(int k=0;k<3;k++)
				m_out+=sss;
		}

		if(temp<100 && temp>10)
		{ 
			for(int k=0;k<2;k++)
            	m_out+=sss;
		}
		if(temp<1000 && temp>100)
			m_out+=sss;
		ss.Format("%d",temp);
		m_out+=ss;

	}

	return m_out;

}

CString passDlg::CRSA()
{  
	CString ss,sss;
    CString out;
	for(int i=0;i<num;i++)
	{
		unsigned __int64 temp=Decrypt(figure[i]);

		unsigned __int64 atemp=temp/100;
		unsigned __int64 btemp=temp-atemp*100;
		atemp+=26;
		btemp+=26;
		ss.Format("%c",atemp);
		out+=ss;
		sss.Format("%c",btemp);
		out+=sss;
	}
    m_out=out;
    return (m_out);

}


unsigned __int64 passDlg::Encrypt(unsigned __int64 figure,int t)
{ 
	P=971;
	Q=137;
	N=P*Q;
    Eul=(P-1)*(Q-1);
	E=71;
	unsigned __int64 e=E;
	unsigned __int64 z=1;///////////////// the cipher 
	unsigned __int64 x=0,y[100];   ////its are used to get a figure's binary number
	unsigned __int64 f;
//	for(int s=0;s<num;s++)
//	{ 
		f=figure;
//	}
	int i;  //a variable
	int l=0;
	while(x!=1) ////the cycle can get a binary number.for example 13 can be converted to 1101
	{              //the binary is remained in a array y[]
		x=e/2;
	    y[l]=e%2;
		l++;
		e=x;		
	}
	y[l]=x;
	int j;
	unsigned __int64 h[100];///it contains the 
	int k=l;
	for(j=0;j<=l;j++)////because the binary in y[] is not the right sort ,so that i use the follow
	{                //method to get the right sort.
		h[j]=y[k];
		k--;
	}
	k++;
	for(i=j-1;i>=0;i--)////it is the main method of the RSA 
	{
		if(h[k]==1)
		{
			z=Getz(z,f,N);
		//	z=z*z*f;
		//	z=z%N;
		}
		else
		{	
			z=z*z;
			z=z%N;
		}
		k++;
	}
	return(z);

}
unsigned __int64 passDlg::Decrypt(unsigned __int64 cipher)
{
	 
	unsigned __int64 D;
	D=Eujilide(E,Eul);
	unsigned __int64 z=1;///////////////// the cipher 
	unsigned __int64 x=0,y[100];   ////its are used to get a figure's binary number
	int l=0;
	int i;
	unsigned __int64 c;
	c=cipher;
	while(x!=1) ////the cycle can get a binary number.for example 13 can be converted to 1101
	{              //the binary is remained in a array y[]
		x=D/2;
	    y[l]=D%2;
		l++;
		D=x;		
	}
	y[l]=x;
	int j;
	unsigned __int64 h[100];///it contains the 
	int k=l;
	for(j=0;j<=l;j++)////because the binary in y[] is not the right sort ,so that i use the follow
	{                //method to get the right sort.
		h[j]=y[k];
		k--;
	}
	k++;
	for(i=j-1;i>=0;i--)////it is the main method of the RSA 
	{
		if(h[k]==1)
		{
			z=Getz(z,c,N);
		//	z=z*z*f;
		//	z=z%N;
		}
		else
		{	
			z=z*z;
			z=z%N;
		}
		k++;
	}
	return(z);

}




unsigned __int64  passDlg::Eujilide(unsigned __int64 a,unsigned __int64 b)
{
	unsigned __int64 temp;
	unsigned __int64 m=a,n=b;
	if(m<n)
	{
		temp=m;
		m=n;
		n=temp;
	}
	unsigned __int64 s=1;
	unsigned __int64 u=0;
	unsigned __int64 t=0;
	unsigned __int64 v=1;
	unsigned __int64 r=1;
	unsigned __int64 q=0;
	while(r!=0)
	{
		r=m % n;
	    q=m/n;
		if(r!=0)
		{
			m=n;
			n=r;
			unsigned __int64 w=u;
			u=s-q*u;
			s=w;
			w=v;
			v=t-q*v;
			t=w;
		}
		else
			break;
	}
    unsigned __int64 g=n;
    return(v);
}





unsigned __int64 passDlg::Getz(unsigned __int64 z,unsigned __int64 f,unsigned __int64 N)
{
	unsigned __int64 R;//
	z=z*z;
//	X=z/N;
	R=z%N;
	z=f*R;
	z=z%N;
	return(z);
   
}


⌨️ 快捷键说明

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