RSA算法的图形化dialog代码

来源:互联网 发布:iphone用电脑网络上网 编辑:程序博客网 时间:2024/06/05 04:15
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int pf_c(int m,int k);
int pf(int m1,int n1);
int gcd(int f);
int r;
int h;
int a,b,c,d,d1,a1,b1,c1,L,m;
bool test_prime(int m);


BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);
        HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);
HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);
    }


    return FALSE;
}


BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)
{
    return TRUE;
}


void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{int a,b,c,d,d1,a1,b1,c1;
    switch(id)
    {


         case IDC_BUTTON1:
TCHAR str1[16384];
TCHAR str2[16384];
GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
a1=atoi(str1);
b1=atoi(str2);
if(!test_prime(a1)){
MessageBox(hwnd,TEXT("第一个数不是素数"),TEXT("错误"),MB_OK|MB_ICONERROR);
return;
}
if(!test_prime(b1)){
MessageBox(hwnd,TEXT("第二个数不是素数"),TEXT("错误"),MB_OK|MB_ICONERROR);
return;
}
r=a1*b1;
c=(a1-1)*(b1-1);
c1=gcd(c);
TCHAR str3[16384];
wsprintf(str3,"%i",c1);
SetDlgItemText(hwnd,IDC_EDIT3,str3);

TCHAR str4[16384];
GetDlgItemText(hwnd,IDC_EDIT4,str4,sizeof(str4));
b=atoi(str4);
m=pf_c(b,c1);
TCHAR str5[16384];
        wsprintf(str5,"%i",m);
SetDlgItemText(hwnd,IDC_EDIT5,str5);
break;


case IDC_BUTTON2: 
 

GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));
GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));
a1=atoi(str1);
b1=atoi(str2);
if(!test_prime(a1)){
MessageBox(hwnd,TEXT("第一个数不是素数"),TEXT("错误"),MB_OK|MB_ICONERROR);
return;
}
if(!test_prime(b1)){
MessageBox(hwnd,TEXT("第二个数不是素数"),TEXT("错误"),MB_OK|MB_ICONERROR);
return;
}
r=a1*b1;
c=(a1-1)*(b1-1);
c1=gcd(c);


wsprintf(str3,"%i",c1);
SetDlgItemText(hwnd,IDC_EDIT3,str3);

TCHAR str6[16384];
GetDlgItemText(hwnd,IDC_EDIT7,str6,sizeof(str6));
int d=atoi(str6);
d1=pf(c,c1);
L=pf_c(d,d1);
TCHAR str7[16384];
        wsprintf(str7,"%i",d1);
SetDlgItemText(hwnd,IDC_EDIT9,str7);
TCHAR str8[16384];
        wsprintf(str8,"%i",L);
SetDlgItemText(hwnd,IDC_EDIT8,str8);
break;



    }
getchar();
}
bool test_prime(int m) {
if (m <= 1) {
   return false;
}
else if (m == 2) {
   return true;
}
else {
   for(int i=2; i<=sqrt(m); i++) {
    if((m % i) == 0) {
     return false;
     break;
    }
   }
   return true;
}
}


int pf_c(int m,int k)
{
 int a,i1,a1,b[50],c1,c;
 c=0;c1=1;i1=0;
 do{
  a=k/2;
  a1=k%2;
  b[i1]=a1;
  k=a;
  i1++;
 }while(a>0);
 i1--;
 for(int i=i1;i>=0;i--)
 {
  c=2*c;
  c1=(c1*c1)%r;
  if(b[i]==1)
  {
   c=c+1;
   c1=(c1*m)%r;
  }
 }
 return c1;
}
int pf(int m1,int n1)
{
 int x1=1,x2=0,x3;
    int y1=0,y2=1,y3;
 x3=m1;
 y3=n1;
 int d;
 for(int i=0; ;i++)
 {
    int q=x3/y3;
    int t1=x1-q*y1;
    int t2=x2-q*y2;
    int t3=x3-q*y3;
    x1=y1;
    x2=y2;
    x3=y3;
    y1=t1;
    y2=t2;
    y3=t3;
    if(y3==1)
    {
     if(y2<0) d=m1+y2;
     else d=y2;
     break;
    }
 }
 return d;
}
int gcd(int f)
{   
 int x1=1,x2=0,x3;
    int y1=0,y2=1,y3;
 for(int i1=2;i1<f;i1++)
 {
  x3=f;
  y3=i1;
     int q=x3/y3;
     int t1=x1-q*y1;
     int t2=x2-q*y2;
     int t3=x3-q*y3;
     x1=y1;
     x2=y2;
     x3=y3;
     y1=t1;
     y2=t2;
     y3=t3;
  if(y3==1)
  {
   return i1;
   break;
  }
 }
}
void Main_OnClose(HWND hwnd)
{
    EndDialog(hwnd, 0);
}
0 0