您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 电子商务 > 基于VC的RSA加密与解密实现技术
基于VC的RSA加密与解密实现技术一、RSA基本原理对明文分组M和密文分组C,加密与解密过程如下:C=POW(M,e)modnM=POW(C,d)modn=POW(POW(M,e),d)modn=POW(M,e*d)其中POW是指数函数,mod是求余数函数。其中收发双方均已知n,发送放已知e,只有接受方已知d,因此公钥加密算法的公钥为KU={e,n},私钥为KR={d,n}。该算法要能用做公钥加密,必须满足下列条件:1.可以找到e,d和n,使得对所有Mn,POW(M,e*d)=Mmodn.2.对所有Mn,计算POW(M,e)和POW(C,d)是比较容易的。3.由e和n确定d是不可行的下面介绍RSA算法,该算法中到的下列的元素:两个素数p,q(保密的,选定的)n=p*q(公开的,计算得出的)e,gcd(x(n),e)=1;1ex(n)(公开的,选定的)d≡POW(e,-1)modx(n)(保密的,计算得出的)这里,私钥为{d,n},公钥为{e,n}。假定用户A已公布了其公钥,用户B要发送消息M给A,那么用户B计算C=POW(M,e)modn,并发送C,在接受端,用户A计算M=POW(C,d)modn以解出消息M。二、开发平台及语言1、开发平台:MicrosoftVisualStudioc++6.02.语言:c++三、应用程序界面设计及变量的连接和方法的名称对象IDCaption连接变量或方法成组框IDC_STATIC产生密钥过程无编辑框IDC_PRIME_EDIT1无m_PrimeEdit1/long编辑框IDC_PRIME_EDIT2无m_PrimeEdit2/long编辑框IDC_PUBN_EDIT无m_PubNEdit/long编辑框IDC_PUBX_EDIT无m_PubXEdit/long编辑框IDC_PUBKEY_EDI无m_PubKeyEdit/long编辑框IDC_PRIKEY_EDIT无m_PriKeyEdit/CEdit命令按钮IDC_CALCULATE_BUTTON计算OnCalculateButton()命令按钮IDC_PUBKEY_BUTTON公有密钥OnPubkeyButton()命令按钮IDC_PRIKEY_BUTTON私有密钥OnPrikeyButton()命令按钮IDC_HIDE_PRIKEY_BUTTON隐藏私钥OnHidePrikeyButton()命令按钮IDC_SHOW_PRIKEY_BUTTON显示私钥OnShowPrikeyButton()命令按钮IDC_STATIC加密和解密过程无编辑框IDC_RESOURCE_EDIT无m_ResourceEdit/CEditOnChangeResourceEdit()编辑框IDC_CRYPTOGRAPH_EDIT无m_CryptographEdit/CEdit编辑框IDC_OPEN_EDIT无m_OpenEdit/CEdit编辑框IDC_LINE无m_Line/CEdit编辑框IDC_CHAR无m_Char/CEdit命令按钮IDC_ENCRYPT_BUTTON数字加密OnEncryptButton()命令按钮IDC_OPEN_BUTTON数字解密OnOpenButton()命令按钮IDC_CLEAR_BUTTONClearOnClearButton()命令按钮IDC_EXIT_BUTTONExitOnExitButton()命令按钮IDC_ENCRYPT_TEXT_BUTTON文本加密OnEncryptTextButton()命令按钮IDC_OPEN_TEXTBUTTON文本解密OnOpenTextbutton()菜单ID_CALCULATE_MENU计算OnCalculateMenu()菜单ID_PUBKEY_MENU公有密钥OnPubkeyMenu()菜单ID_PRIKEY_MENU私有密钥OnPrikeyMenu()菜单ID_HIDE_MENU隐藏私钥OnHideMenu()菜单ID_SHOW_MENU显示私钥OnShowMenu()菜单ID_CLEAR_MENU清除OnClearMenu()菜单ID_EXIT_MENU退出OnExitMenu()菜单ID_ENCRYPTNUM_MENU数字加密OnEncryptnumMenu()菜单ID_OPENNUM_MENU数字解密OnOpennumMenu()菜单ID_ENCRYPTTEXT_MENU文本加密OnEncrypttextMenu()菜单ID_OPENTEXT_MENU文本解密OnOpentextMenu()菜单ID_ABOUT_MENU关于OnAboutMenu()四、RSADlg.h声明//RSADlg.h:headerfile#if!defined(AFX_RSADLG_H__D491C4A6_0DE9_4430_BE31_2150D1C5E35F__INCLUDED_)#defineAFX_RSADLG_H__D491C4A6_0DE9_4430_BE31_2150D1C5E35F__INCLUDED_#if_MSC_VER1000#pragmaonce#endif//_MSC_VER1000#includemath.h///////////////////////////////////////////////////////////////////CRSADlgdialogclassCRSADlg:publicCDialog{//Constructionpublic:charCalculateModOfChar(doublenum1,doublenum2);charCalculateModOfChar(constcharcChar,doublenum,long*ps);doublecalculatemod(doublenum1,doublenum2);intprimeNumber(longm);CRSADlg(CWnd*pParent=NULL);//standardconstructor//DialogData//{{AFX_DATA(CRSADlg)enum{IDD=IDD_RSA_DIALOG};CEditm_OpenEdit;CEditm_CryptographEdit;CEditm_PriKeyEdit;CEditm_ResourceEdit;CEditm_Line;CEditm_Char;longm_PubNEdit;longm_PubXEdit;longm_PrimeEdit1;longm_PrimeEdit2;longm_PubKeyEdit;//}}AFX_DATA//ClassWizardgeneratedvirtualfunctionoverrides//{{AFX_VIRTUAL(CRSADlg)protected:virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport//}}AFX_VIRTUAL//Implementationprotected:HICONm_hIcon;//Generatedmessagemapfunctions//{{AFX_MSG(CRSADlg)virtualBOOLOnInitDialog();afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);afx_msgvoidOnPaint();afx_msgHCURSOROnQueryDragIcon();afx_msgvoidOnCalculateButton();afx_msgvoidOnPubkeyButton();afx_msgvoidOnPrikeyButton();afx_msgvoidOnChangeResourceEdit();afx_msgvoidOnHidePrikeyButton();afx_msgvoidOnShowPrikeyButton();afx_msgvoidOnClearButton();afx_msgvoidOnExitButton();afx_msgvoidOnEncryptButton();afx_msgvoidOnOpenButton();afx_msgvoidOnEncryptTextButton();afx_msgvoidOnOpenTextbutton();afx_msgvoidOnCalculateMenu();afx_msgvoidOnPubkeyMenu();afx_msgvoidOnPrikeyMenu();afx_msgvoidOnHideMenu();afx_msgvoidOnShowMenu();afx_msgvoidOnClearMenu();afx_msgvoidOnExitMenu();afx_msgvoidOnEncryptnumMenu();afx_msgvoidOnOpennumMenu();afx_msgvoidOnEncrypttextMenu();afx_msgvoidOnOpentextMenu();afx_msgvoidOnAboutMenu();//}}AFX_MSGDECLARE_MESSAGE_MAP()private:longlArrayOfCount[300];//文本加密和解密过程中记数功能};//{{AFX_INSERT_LOCATION}}//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.#endif//!defined(AFX_RSADLG_H__D491C4A6_0DE9_4430_BE31_2150D1C5E35F__INCLUDED_)五、映射消息的应用程序代码1、voidCRSADlg::OnCalculateButton()//计算按钮处理函数{UpdateData(TRUE);intflag1,flag2;flag1=primeNumber(m_PrimeEdit1);flag2=primeNumber(m_PrimeEdit2);if(flag1==1)m_PrimeEdit1=m_PrimeEdit1;elsem_PrimeEdit1=0;if(flag2==1)m_PrimeEdit2=m_PrimeEdit2;elsem_PrimeEdit2=0;m_PubNEdit=m_PrimeEdit1*m_PrimeEdit2;m_PubXEdit=(m_PrimeEdit1-1)*(m_PrimeEdit2-1);UpdateData(FALSE);}2、intCRSADlg::primeNumber(longm)//判断是否为素数{intflag=1;longtemp=(long)sqrt(m);for(inti=2;i=temp;i++){if(m%i==0){flag=0;break;}}if(flag==0)return0;elsereturn1;}3、voidCRSADlg::OnPubkeyButton()//公有密钥按钮处理函数{UpdateData(TRUE);if(primeNumber(m_PubKeyEdit)==0||m_PubXEdit%m_PubKeyEdit==0||m_PubKeyEditm_PubXEdit)m_PubKeyEdit=0;UpdateData(FALSE);}4、voidCRSADlg::OnPrikeyButton(){UpdateData(TRUE);doubleprivatenum;charcBuffer[32];for(doublei=1;;i++){doubletemp=fmod((i*m_PubXEdit+1),m_PubKeyEdit);if(temp==0.0&&(i*m_PubXEdit+1)/m_PubKeyEditm_PubXEd
本文标题:基于VC的RSA加密与解密实现技术
链接地址:https://www.777doc.com/doc-2572647 .html