資源描述:
《C++實(shí)現(xiàn)RSA加解密算法》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫(kù)。
1、/*************************************************************************//*Filename:RSA.c*//*Description:*//*publicKey:pqr*//*privateKey:mn*//*************************************************************************//*包含頭文件*/#include#include/*類型定義*/typ
2、edeflongintli_ELEMTYPE;typedefinti_ELEMTYPE;typedefcharc_ELEMTYPE;/*宏定義*/#defineTEXT_MAX_NUM20#defineRSA_ENCODE_TEXT"xidianuniversity"/*RSA編解碼函數(shù)*//*輸入輸出說(shuō)明:r=a^bmodc編碼1.a-------原始加密明文數(shù)據(jù)2.b-------加密指數(shù)(p-1)(q-1)3.c-------p*q4.r-------加密后密文數(shù)據(jù)解碼1.a-------原始解密密文數(shù)據(jù)2.b-------解密指數(shù)d
3、3.c-------p*q4.r-------解密后明文數(shù)據(jù)*/li_ELEMTYPERsa_un_enCode(li_ELEMTYPEa,li_ELEMTYPEb,li_ELEMTYPEc){li_ELEMTYPEr=1;b=b+1;while(b!=1){r=r*a;r=r%c;b--;}returnr;}/*main主函數(shù)*/intmain(intargc,char**argv){li_ELEMTYPEp,q,e,d,n,t;i_ELEMTYPEi=0;i_ELEMTYPEacSecret_Text[TEXT_MAX_NUM];c_E
4、LEMTYPEacPublic_Text[TEXT_MAX_NUM];memset(acSecret_Text,0,sizeof(acSecret_Text));memset(acPublic_Text,0,sizeof(acPublic_Text));memcpy(acPublic_Text,RSA_ENCODE_TEXT,sizeof(acPublic_Text));p=53;q=79;e=77777;printf("Info:p=%ldq=%de=%d",p,q,e);n=p*q;printf("Then(p*q)=%3d
5、",n);t=(p-1)*(q-1);printf("Thet((p-1)*(q-1))=%ld",t);printf("Caculatingd>>…………");d=1;while(((e*d)%t)!=1){d++;}printf("Thed=%ld",d);printf("--------------Encoding--------------");printf("TheacSecret_Textis:");for(i=0;i6、){acSecret_Text[i]=Rsa_un_enCode(acPublic_Text[i],e,n);printf("%d",acSecret_Text[i]);}printf("");printf("--------------Decoding--------------");printf("TheacPublic_Textis:");for(i=0;i7、(acSecret_Text[i],d,n);printf("%c",acPublic_Text[i]);}printf("");return0;}/*************************源文件結(jié)束(+回車)*****************************/