Chinaunix首页 | 论坛 | 博客
  • 博客访问: 64195
  • 博文数量: 31
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 15
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-29 12:33
文章分类

全部博文(31)

文章存档

2017年(1)

2015年(1)

2014年(2)

2013年(2)

2011年(1)

2010年(6)

2009年(18)

我的朋友

分类: 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();
}

阅读(782) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~