标题:[求助]部分RSA加密程序有问题
取消只看楼主
happyyu
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2005-4-10
 问题点数:0 回复次数:0 
[求助]部分RSA加密程序有问题

#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
unsigned long cd,ce;
unsigned long high_exp(unsigned long x,unsigned long y,unsigned long n)/*高次幂求摹*/
{
unsigned long e,f;
f=x%n;
while(y!=1)
{
e=x*f;
f=e%n;
y--;

}
return(f);
}
unsigned long pr_num()/*产生素数*/
{
unsigned long c,a,b,i;

while(c!=0)
{ c=0;
a=random(10);
if(a==0) continue;
for(i=2;i<=sqrt(a);i++)
{
b=a%i;
if(b==0)
c++ ;
}
}

}
unsigned long gcd(unsigned long a,unsigned long d)/*欧几里德算法*/
{
unsigned long r;
while(r>0)
{
r=a%d;
a=d;
d=r;
}
if(a==1) return 1;
else return 0;
}
unsigned long widen_gcd(unsigned long x,unsigned long y,unsigned long*a1,unsigned long*a2,unsigned long*b1,unsigned long*b2)/*扩展欧几里德算法*/
{
unsigned long a,b,q,r;

q=x/y ;
r=x%y ;
a=*a2-q*(*a1);
b=*b2-q*(*b1);
if(0==r)
{
return y ;
}

*a2=*a1 ;
*b2=*b1 ;
*a1=a ;
*b1=b ;
return widen_gcd(y,r,a1,a2,b1,b2);

}
unsigned long prod_ek_dk()/*产生密钥*/
{
unsigned long de,e,c,d,p,q,n,t,b1,b2,a2;
e=0;a2=1;b1=1;b2=0;
p=pr_num();

q=pr_num();
printf("p=%ld,q=%ld",p,q);
n=p*q;
/*printf("\n n=%ld",n);*/
t=(p-1)*(q-1);
printf("\n t=%ld",t);
do
{
d=random(t);
c=gcd(t,d);
}while(d==1||!c);
printf("\n d=%ld",d);

de=widen_gcd(d,t,&e,&a2,&b1,&b2);

if(de<0)
{
de=de+t;

}
printf("\n e=%ld",de);
cd=d;
ce=de;
return(n);
}

void main()
{
unsigned long f,c,n,d,e,ww,qq;
randomize();
n=prod_ek_dk();
d=cd;
e=ce;
printf("\n n=%ld",n);
printf("\n d=%ld e=%ld",d,e);
ww=5;
c=high_exp(ww,d,n);
printf("\n c=%ld",c);
qq=high_exp(c,e,n);
printf("\n qq=%ld",qq);
getch();
}

不报错,但没有结果

搜索更多相关主题的帖子: RSA 
2006-12-05 15:24



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-107421-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.693142 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved