首页 > 代码库 > 界面实现rsa

界面实现rsa

今天去答辩,老师叫我回去做一个界面。我想问,老师,不是说话第一类的不做界面么现在又要做界面是什么回事?

╮(╯▽╰)╭  算了八  还是好好的敲代码八 下面就是修改过后的。  用SDK写的界面。

 

#include <windows.h>#include "resource.h"#include<tchar.h>#include <stdio.h>#include <math.h>#include <windowsx.h>int length = 0; char str2[20]={0};static long int e ,n ,m,c,d;HINSTANCE hins ;TCHAR cD[20][30];int flag = 0,str3[20]={0};BOOL CALLBACK DlgMain(HWND hwnd,UINT message,WPARAM  wParam,    LPARAM  lParam);bool JudgeP (int prime)             //判断是不是素数{    int i=0;    long double temp=0 ;    if (prime == 0||prime == 2)        return false;    else    {    temp =sqrt((float)prime);    for (i=2;i<=temp;i++)    {        if (prime%i == 0)                return false;       //输入的不是素数            }    return true ;                    //输入的数是素数    } }int gcd(int a , int b){    return b==0?a:gcd(b,a%b);}bool  CheckE ( int f_n ,int e)        //判断e是否符合要求  1<e <f_n且e 与f_n互素{    int k ;    while (e > f_n || e<= 1)                        //确定e的范围    {        return false;    }    k =gcd(f_n,e) ;       if ( k !=1)           return false;       return  true ;}long int  CalculateD(long int  f_n ,long  int e)     //辗转相除{    int i , j = 1 , t1 , t2= 1 , k = 0  , m ;    int arry_1[30] = {f_n,e} ,arry_2[30] ={0} ;    printf(" the Calculation:\n") ;    for (i=1 ;arry_1[i] != 1; i ++)                //用数组存放好辗转相除过程中的数据    {        arry_1[i+1] =arry_1[i-1]%arry_1[i] ;        arry_2 [i-1] = arry_1[i-1]/arry_1[i] ;        wsprintf(cD[i],L" %d = %d*%d+%d\n",arry_1[i-1],arry_1[i],arry_2[i-1],arry_1[i+1] ) ;     }    flag = i;    t1 = 1 ;    t2 = -arry_2[i-2] ;    while((i-2)!=0 )    {        m = t1 ;        t1 = t2  ;        if (j%2 != 0)            t2 = (abs(m)+arry_2[i-3]*abs(t2));        else            t2 = -(abs(m)+arry_2[i-3]*abs(t2));        i -- ;           j++ ;    }        while(t2 < 0)    //保证D是大于0正数    {        t2 = f_n + t2 ;    }    k = t2 ;    while(t2 = t2/f_n)            {        k = t2%f_n      ;     }    printf("D = %d\n",k);        return k ;}int  * Dec2Bin(int e ) //转换为二进制 {  int i = 0 ;  int str1[20]; while(e) {  str1[i] = e%2 ;  e = e/2 ;   i++ ; } i -- ; for (length = 0 ; i>=0 ; length ++)          //这里赋值的。Ok {  str2[length] = str1[i] +0;   //这里是二进制的  只有10 str3[length] = str1[i];  i -- ; }  str2[length]=\0; return str3 ; }int  Encrypt(int m , int e , int n)  //平方乘法 {     int  c =1  , i ;     int *p =  Dec2Bin(e) ;         for (i = 0  ; i < length;i ++)     {         c = c %n;         c =c*c%n ;          if (*(p+i) == 1)         { c =c%n;          c =c*m%n;          }     }     return c ; }int WINAPI WinMain(    HINSTANCE hInstance,HINSTANCE hPrevInst, LPSTR lpszCmdLine,    int    nCmdShow){    DialogBox(hInstance,(LPCWSTR)IDD_DIALOG1,NULL,DlgMain);    return 0 ;}BOOL CALLBACK DlgEncrpty(HWND hwnd,UINT message,WPARAM  wParam,    LPARAM  lParam){    TCHAR mesg[20] ={L""};    TCHAR buff[512] ={L""};//最好不要用L 用TEXT    switch (message)    {    case WM_INITDIALOG:        wsprintf(mesg,L"(%d,%d)",e ,n );          SetDlgItemText(hwnd,IDC_EDIT1,mesg);        SetDlgItemTextA(hwnd,IDC_EDIT2,str2);        SetDlgItemInt(hwnd,IDC_EDIT3,c,TRUE);        break;    case WM_DESTROY:        ShowWindow(hwnd,SW_HIDE);        break;    case WM_CLOSE:        ShowWindow(hwnd,SW_HIDE);        break;    }    return 0 ;}BOOL CALLBACK DlgDecrption(HWND hwnd,UINT message,WPARAM  wParam,    LPARAM  lParam){    HWND hlist =GetDlgItem(hwnd,IDC_LIST1);    TCHAR mesg[20] ={L""};    TCHAR buff[512] ={L""};//最好不要用L 用TEXT    switch (message)    {    case WM_INITDIALOG:        wsprintf(mesg,L"(%d,%d)",e ,n );          SetDlgItemText(hwnd,IDC_EDIT1,mesg);        SetDlgItemTextA(hwnd,IDC_EDIT2,str2);        SetDlgItemInt(hwnd,IDC_EDIT3,m,TRUE);        for (int i=0;i< flag;i++)        {            ListBox_AddString(hlist,cD[i]);        }        break;    case WM_DESTROY:        ShowWindow(hwnd,SW_HIDE);        break;    case WM_CLOSE:        ShowWindow(hwnd,SW_HIDE);        break;    }    return 0 ;}BOOL CALLBACK DlgMain(HWND hwnd,UINT message,WPARAM  wParam,    LPARAM  lParam){    long int p,q ,f_n;    int choice = 0;    BOOL flag ;            switch(message)        {                                case  WM_COMMAND:                switch (LOWORD(wParam))                {                case IDOK:                    p = GetDlgItemInt(hwnd,IDC_EDIT1,&flag,TRUE);                    if (!JudgeP(p))                    MessageBox(hwnd,L"p不是素数",L"警告",MB_OK);                    else                    {                    q =GetDlgItemInt(hwnd,IDC_EDIT2,&flag,TRUE);                                        if (!JudgeP(q))                                        {                    MessageBox(hwnd,L"q不是素数",L"警告",MB_OK);                                        }                                        else                                        {                                                    f_n =(p-1)*(q-1);                    e =GetDlgItemInt(hwnd,IDC_EDIT3,&flag,TRUE);                    if (!CheckE(f_n,e))                        MessageBox(hwnd,L"请注意e的范围 而且e要与(p-1)*(q-1)互素",L"错误",MB_OK);                    else                    {                    choice =IsDlgButtonChecked(hwnd,IDC_RADIO3);                    choice = IsDlgButtonChecked(hwnd,IDC_RADIO4);                    n = p*q ;                    switch (choice)                    {                    case 0:                           //加密                        m =GetDlgItemInt(hwnd,IDC_EDIT4,&flag,TRUE);                        c =Encrypt(m,e,n);                        DialogBox(hins,(LPCWSTR)IDD_DIALOG2,NULL,DlgEncrpty);                        break;                    case 1:                          //解密                        c = GetDlgItemInt(hwnd,IDC_EDIT5,&flag,TRUE);                        d=CalculateD(f_n,e);                        m=Encrypt(c,d,n);                        DialogBox(hins,(LPCWSTR)IDD_DIALOG3,NULL,DlgDecrption);                        break;                    }                    }}                                                            }                    break;                }                break;            case WM_DESTROY:            PostQuitMessage(0);                    //调用PostQuitMessage发出WM_QUIT消息            break;            case WM_CLOSE:                DestroyWindow(hwnd);                break;        }    return 0;}

在这个遇到的那题是  如何一个int型的数组放到edit空间输出。表示脑袋太笨,很久才做出来。噗。。

实现主界面:

技术分享

输出加密后的界面:

技术分享

输出界面的界面:

技术分享

界面实现rsa