您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > elgamalC语言
#includeiostream.h#includestdlib.h#includetime.hinta,b;intprime_number_p();intrandom(int);intm_develop(int);/*---------------------------------*/intprime(intp){inti,j;for(i=2;ip;i++){j=p%i;if(j==0)return0;}return1;}//ÅжÏÊÇ·ñΪËØÊý,ÈôΪËØÊýÔò·µ»Ø1/*------------------------------------*/intprime_number_max(intn,intm){intp;for(p=m;p=n;p--){if(prime(p)==1)returnp;}}//Çó³ön,mÖ®¼äµÄ×î´óËØÊý/*--------------------------------*/intprime_number_p(){intp,m,n;coutΪһ×éÓû§²úÉúÒ»¸ö¹«Ô¿P(pΪËØÊý)endl;coutÇëÊäÈëpËùÔÚ·¶Î§n£¬m(ϵͳ½«Ñ¡Ôñ×î´óµÄËØÊýP)endl;coutn=;cinn;coutm=;cinm;p=prime_number_max(n,m);if(p==0){coutn,mÖÐÎÞËØÊý;p=prime_number_p();}returnp;}//pµÄ²úÉú/*-------------------------------*/intRandom_number_g(intp){intg;srand((int)time(NULL));g=rand()%p;returng;}//gµÄ²úÉúintPKI_y(intp,intg,intx){inty;inti;y=1;for(i=0;ix;i++){y*=g;y%=p;}returny;}//¹«Ô¿yµÄ²úÉúintgcd(inta,intb){ints[50],q[50];if(ab){s[0]=a;s[1]=b;}else{s[0]=a;s[1]=b;}for(inti=1;i100;i++){s[i+1]=s[i-1]%s[i];q[i]=s[i-1]/s[i];if(s[i+1]==0)break;}returns[i];}//Çóa£¬bµÄ×î´ó¹«Ô¼Êý/*----------------------------------*/intm_develop(intp){intm;cinm;if(mp){coutÎÞ·¨¶ÔÊäÈëµÄm½øÐÐÇ©Ãû,ÇëÖØÐÂÊäÈëendl;m=m_develop(p);}returnm;}intprime_k(intp){intk,i;k=rand()%p;i=gcd(k,p-1);if(i!=1){k=prime_k(p);}returnk;}//Ëæ¼´ÊýkµÄ²úÉú/*-----------------------------------*/voidelg_sign_develop(intg,intp,intx,intm){intk;inti=1;k=prime_k(p);//²úÉúÒ»¸ökµÄËæ»úÊý,kÓëp-1»¥ÖÊ//a=(g^k)%p;for(i=0;ik;i++){a*=g;a%=p;}//while(m!=(a*x+k*i)%(p-1))//{i++;}b=i;for(i=0;ip-1;i++){b=i;if((x*a+k*b)%(p-1)==m)break;}k=0;//¶ªÆúËæ¼´ÊýKcoutmµÄÊý×ÖÇ©ÃûÊÇ£¨a,b£©endl;}//²úÉúÇ©Ãû/*¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª*/voidelg_sign_test(inty,intp,intm,intg,charname){inti,c1,d1,j;c1=d1=j=1;//i=((y^a)*(a^b))%p;for(i=0;ia;i++){c1*=y;c1%=p;}for(i=0;ib;i++){d1*=a;d1%=p;}d1*=c1;d1%=p;//j=(g^m)%p;for(i=0;im;i++){j*=g;j%=p;}if(d1==j){coutÊÇÓû§nameÇ©ÃûµÄ¡£endl;}else{cout²»ÊÇÓû§nameÇ©ÃûµÄendl;}}//Ñé֤ǩÃû/*¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª¡ª*/main(){intp,g,x,y,m,i;a=b=1;charname;p=prime_number_p();//²úÉú´óËØÊýpg=Random_number_g(p);//²úÉúËæ¼´Êýgcout´ËÓû§×éµÄ¹«¹²²ÎÊýΪp=pg=gendl;coutÊäÈëÓû§Ãûendl;cinname;x=p+1;while(x=p){coutÊäÈëÓû§ÃÜÂëendl;cinx;if(x=p)coutÃÜÂë¸ñʽ´íÎó;endl;}y=PKI_y(p,g,x);//¹«Ô¿yµÄ²úÉúcoutÓû§name¹«Ô¿yÊÇyendl;coutÊäÈëҪǩÃûµÄ×Ö·ûmendl;m=m_develop(p);elg_sign_develop(g,p,x,m);elg_sign_test(y,p,m,g,name);jj:cout½áÊø³ÌÐòÇëÊäÈë0endl;cini;if(i==0)exit(0);elsegotojj;}
本文标题:elgamalC语言
链接地址:https://www.777doc.com/doc-5680687 .html