分类: C/C++
2009-04-28 08:25:14
/*由于c语言中的长整型的数字范围不是很大,所以只用于小数据的密钥生成和加解密
#include
int Isinterprime(long x,long fn)
{
long i=1;
for(;i
if(x%(i+1)==0&&fn%(i+1)==0) {i=0; break;}
}
if(i==0) return 0;
else return 1;
}
void Founde(long fn)
{
long i=1;
for(;i
if(Isinterprime(i,fn))
printf("%d\t",i);
}
}
void Foundd(long e,long fn)
{
long d,de;
de=e;
for(d=1;d
de=de%fn;
if(de==1) printf("%d",d);
}
}
long crypt(long x,long y,long z)
{
long i,xx;
xx=x;
for(i=1;i
x=x*xx;
x=x%z;
}
return x ;
}
void main()
{
long p,q,n,fn,e,d,nn,plaint=0,cryptext=0,decryptext=0,exit;
printf(" welcome for using the public key software powed by lei\n\n");
printf("please input the prime number p:");
scanf("%d",&p);
printf("please input the prime number q:");
scanf("%d",&q);
n=p*q;
fn=(p-1)*(q-1);
clrscr();
printf("the available e are followed ,please choice one:\n"); nn=fn;
Founde(nn);
printf("\ninput e:");
scanf("%d",&e);
clrscr();
printf("the available d are followed ,please choice one:\n");
Foundd(e,fn);
printf("\ninput d:");
scanf("%d",&d);
clrscr();
printf("the public key is: {%d,",e); printf("%d}\n",n);
printf("the private key is: {%d,",d); printf("%d}\n",n);
printf("please input plaintext:");
scanf("%d",&plaint);
cryptext=crypt(plaint,e,n);
clrscr();
printf("the encryptext is: %d\n",cryptext);
printf("waiting...\n");
decryptext=crypt(cryptext,d,n);
printf("the decryptext is: %d\n",decryptext);
printf("press any key to exit!");
exit=getch();
}