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

📄 rsadlg.cpp

📁 而SDES是简单的des算法版本
💻 CPP
字号:
// RSAdlg.cpp : implementation file
//

#include "stdafx.h"
#include "sdesandrsa.h"
#include "RSAdlg.h"
#include"stdlib.h"
#include"time.h"
#include"math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// RSAdlg dialog


RSAdlg::RSAdlg(CWnd* pParent /*=NULL*/)
	: CDialog(RSAdlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(RSAdlg)
	m_p1 = 0;
	m_p2 = 0;
	m_gong = 0;
	m_si = 0;
	m_n = 0;
	m_ming = 0;
	m_mi = 0;
	m_key = 0;
	m_n2 = 0;
	//}}AFX_DATA_INIT
}


void RSAdlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(RSAdlg)
	DDX_Text(pDX, IDC_EDIT5, m_p1);
	DDX_Text(pDX, IDC_EDIT6, m_p2);
	DDX_Text(pDX, IDC_EDIT7, m_gong);
	DDX_Text(pDX, IDC_EDIT8, m_si);
	DDX_Text(pDX, IDC_EDIT10, m_n);
	DDX_Text(pDX, IDC_EDIT2, m_ming);
	DDX_Text(pDX, IDC_EDIT4, m_mi);
	DDX_Text(pDX, IDC_EDIT3, m_key);
	DDX_Text(pDX, IDC_EDIT9, m_n2);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(RSAdlg, CDialog)
	//{{AFX_MSG_MAP(RSAdlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// RSAdlg message handlers
int prime(long int x)
{
	int t=(int)sqrt(x)+1;
	int i;
	for(i=2;i<t;i++)
	{
		if(x%i==0)return 0;
	}
	return 1;
}
int niyuan(int b, int m)
 {
    int t0,r0,t1,r1,t2,r2,q;  
    t0=0,r0=m,t1=1,r1=b;
    while(r1!=0)
    {
        q=r0/r1;
        r2=r1;
        r1=r0-q*r1; 
        r0=r2; 
        t2=t1;      
        t1=t0-q*t1;
        t0=t2;
    }
    return (t0+m)%m;
 }
int gcd(int a,int b) 
{ 
	int temp; 
	if (a<b) 
	{ 
		temp=a;
		a=b;
		b=temp; 
	} 
	if (a%b==0) 
	{
		return b; 
	}
	else gcd(b,a%b); 
} 
void creat_miyao(int p,int q,int &e,int &d)
{
	int fn=(p-1)*(q-1);

	for(e=2;e<fn;e++)
		if (gcd(e,fn)==1)break;
	d=niyuan(e,fn);

}

int pow_mod(int x,int n,int m)
{
    long unsigned int pw = 1;
    while (n>0) 
	{
        if ((n%2)==1)
        pw *= x;
        x = x*x % m;
        n /= 2;
    }
    return pw%m;
}
void RSAdlg::OnButton1() 
{
	int p,q,e,d,n;
	srand(time(0));
    while(!prime(q)||q<=1||q==2)
	{
		q=rand()%100;
	}
    while(!prime(p)||p<=1||q==2)
	{
		p=rand()%100;
	}
	creat_miyao(p,q,e,d);
	 n=p*q;
	 m_p1=p;
	 m_p2=q;
	 m_gong=e;
	 m_si=d;
	 m_n=n;
	 UpdateData(FALSE);	
}

void RSAdlg::OnButton2() 
{
	UpdateData();	
	m_mi=NULL;
	if(m_ming>m_n2)
			MessageBox("请输入小于n的明文o(∩_∩)o...");
	else
	m_mi=pow_mod(m_ming,m_key,m_n2);
	UpdateData(FALSE);	

}

void RSAdlg::OnButton3() 
{
	UpdateData();	
	if(m_mi>m_n2)
			MessageBox("请输入小于n的密文o(∩_∩)o...");
	m_ming=NULL;
	m_ming=pow_mod(m_mi,m_key,m_n2);
	UpdateData(FALSE);	
}

void RSAdlg::OnButton4() 
{
	m_ming=NULL;
    m_mi=NULL;
	m_n2=NULL;
	m_key=NULL;
	UpdateData(FALSE);	
	
}

void RSAdlg::OnButton5() 
{
	CDialog::OnCancel();		
}

⌨️ 快捷键说明

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