信息安全技术RSA加密算法文档格式.docx
《信息安全技术RSA加密算法文档格式.docx》由会员分享,可在线阅读,更多相关《信息安全技术RSA加密算法文档格式.docx(27页珍藏版)》请在冰豆网上搜索。
a2[i])
return1;
if(a1[i]<
return0;
}
voidmov(inta[MAX],int*b)
{
intj;
for(j=0;
j<
MAX;
j++)
b[j]=a[j];
return;
//大数相乘(向左移)
voidmul(inta1[MAX],inta2[MAX],int*c)
inti,j,y,x,z,w,l1,l2;
l1=a1[MAX-1];
l2=a2[MAX-1];
if(a1[MAX-2]=='
-'
&
a2[MAX-2]=='
)
c[MAX-2]=0;
elseif(a1[MAX-2]=='
c[MAX-2]='
;
elseif(a2[MAX-2]=='
for(i=0;
l1;
l2;
x=a1[i]*a2[j];
y=x/10;
z=x%10;
w=i+j;
c[w]=c[w]+z;
c[w+1]=c[w+1]+y+c[w]/10;
c[w]=c[w]%10;
w=l1+l2;
if(c[w-1]==0)w=w-1;
c[MAX-1]=w;
return;
}
//大数相加,注意进位
voidadd(inta1[MAX],inta2[MAX],int*c)
inti,l1,l2;
intlen,temp[MAX];
intk=0;
if((a1[MAX-2]=='
)&
(a2[MAX-2]=='
))
c[MAX-2]='
mov(a1,temp);
temp[MAX-2]=0;
sub(a2,temp,c);
mov(a2,temp);
temp[98]=0;
sub(a1,temp,c);
if(l1<
l2)len=l1;
elselen=l2;
len;
c[i]=(a1[i]+a2[i]+k)%10;
k=(a1[i]+a2[i]+k)/10;
if(l1>
len)
for(i=len;
c[i]=(a1[i]+k)%10;
k=(a1[i]+k)/10;
if(k!
=0)
c[l1]=k;
len=l1+1;
elselen=l1;
else
c[i]=(a2[i]+k)%10;
k=(a2[i]+k)/10;
c[l2]=k;
len=l2+1;
c[99]=len;
//大数相减,注意借位
voidsub(inta1[MAX],inta2[MAX],int*c)
intlen,t1[MAX],t2[MAX];
if((a1[MAX-2]=='
)&
(a2[MAX-2]=='
mov(a1,t1);
mov(a2,t2);
t1[MAX-2]=0;
t2[MAX-2]=0;
sub(t2,t1,c);
elseif(a2[MAX-2]=='
mov(a2,t2);
t2[MAX-2]=0;
add(a1,t2,c);
t2[MAX-2]='
if(cmp(a1,a2)==1)
len=l2;
if((a1[i]-k-a2[i])<
0)
c[i]=(a1[i]-a2[i]-k+10)%10;
k=1;
else
c[i]=(a1[i]-a2[i]-k)%10;
k=0;
if((a1[i]-k)<
c[i]=(a1[i]-k+10)%10;
c[i]=(a1[i]-k)%10;
if(c[l1-1]==0)//使得数组C中的前面所以0字符不显示了,如1000-20=0980--->
显示为980了
len=l1-1;
i=2;
while(c[l1-i]==0)//111456-111450=00006,消除0后变成了6
len=l1-i;
i++;
else
len=l1;
if(cmp(a1,a2)==(-1))
len=l1;
if((a2[i]-k-a1[i])<
c[i]=(a2[i]-a1[i]-k+10)%10;
c[i]=(a2[i]-a1[i]-k)%10;
if((a2[i]-k)<
c[i]=(a2[i]-k+10)%10;
c[i]=(a2[i]-k)%10;
if(c[l2-1]==0)
len=l2-1;
while(c[l1-i]==0)
elseif(cmp(a1,a2)==0)
len=1;
c[len-1]=0;
c[MAX-1]=len;
//取模数
voidmod(inta[MAX],intb[MAX],int*c)//c=amodb,注意:
经检验知道此处A和C的数组都改变了
{intd[MAX];
mov(a,d);
while(cmp(d,b)!
=(-1))//c=a-b-b-b-b-b.......until(c<
b)
sub(d,b,c);
mov(c,d);
//c复制给a
return;
//大数相除(向右移)
//试商法,调用以后w为amodb,C为adivb
voiddivt(intt[MAX],intb[MAX],int*c,int*w)
inta1,b1,i,j,m;
//w用于暂时保存数据
intd[MAX],e[MAX],f[MAX],g[MAX],a[MAX];
mov(t,a);
e[i]=0;
d[i]=0;
i++)g[i]=0;
a1=a[MAX-1];
b1=b[MAX-1];
if(cmp(a,b)==(-1))
c[0]=0;
c[MAX-1]=1;
mov(t,w);
elseif(cmp(a,b)==0)
c[0]=1;
w[0]=0;
w[MAX-1]=1;
m=(a1-b1);
for(i=m;
i--)//例如341245/3=341245-300000*1--->
41245-30000*1--->
11245-3000*3--->
2245-300*7--->
145-30*4=25--->
25-3*8=1
for(j=0;
d[j]=0;
d[i]=1;
d[MAX-1]=i+1;
mov(b,g);
mul(g,d,e);
while(cmp(a,e)!
=(-1))
c[i]++;
sub(a,e,f);
mov(f,a);
//f复制给g
for(j=i;
j++)//高位清零
e[j]=0;
mov(a,w);
if(c[m]==0)c[MAX-1]=m;
elsec[MAX-1]=m+1;
//解决了m=a*bmodn
voidmulmod(inta[MAX],intb[MAX],intn[MAX],int*m)
intc[MAX],d[MAX];
d[i]=c[i]=0;
mul(a,b,c);
divt(c,n,d,m);
m[MAX-1];
printf("
m[m[MAX-1]-i-1]);
\nmlengthis:
%d\n"
m[MAX-1]);
//解决了m=a^pmodn的函数问题。
voidexpmod(inta[MAX],intp[MAX],intn[MAX],int*m)
intt[MAX],l[MAX],temp[MAX];
//t放入2,l放入1
intw[MAX],s[MAX],c[MAX],b[MAX],i;
MAX-1;
b[i]=l[i]=t[i]=w[i]=0;
t[0]=2;
t[MAX-1]=1;
l[0]=1;
l[MAX-1]=1;
mov(l,temp);
mov(a,m);
mov(p,b);
while(cmp(b,l)!
w[i]=c[i]=0;
divt(b,t,w,c);
//c=pmod2w=p/2
mov(w,b);
//p=p/2
if(cmp(c,l)==0)//余数c==1
w[i]=0;
mul(temp,m,w);
mov(w,temp);
divt(temp,n,w,c);
//c为余c=temp%n,w为商w=temp/n
mov(c,temp);
s[i]=0;
mul(m,m,s);
//s=a*a
c[i]=0;
divt(s,n,w,c);
//w=s/n;
c=smodn
mov(c,m);
mul(m,temp,s);
//余数s给m
m[MAX-2]=a[MAX-2];
//为后面的汉字显示需要,用第99位做为标记
//k=temp*k%n
intis_prime_san(intp[MAX])
inti,a[MAX],t[MAX],s[MAX],o[MAX];
s[i]=o[i]=a[i]=t[i]=0;
t[0]=1;
t[MAX-1]=1;
a[0]=2;
//{2,3,5,7}
a[MAX-1]=1;
sub(p,t,s);
expmod(a,s,p,o);
if(cmp(o,t)!
=0)
a[0]=3;
i++)o[i]=0;
a[0]=5;
{
a[0]=7;
if(cmp(o,t)!
return1;
//检测两个大数之间是否互质
intcoprime(inte[MAX],ints[MAX])
inta[MAX],b[MAX],c[MAX],d[MAX],o[MAX],l[MAX];
l[i]=o[i]=c[i]=d[i]=0;
o[0]=0;
o[MAX-1]=1;
mov(e,b);
mov(s,a);
do
if(cmp(b,l)==0)
divt(a,b,d,c);
mov(b,a);
//b--->
a
mov(c,b);
//c--->
b
}while(cmp(c,o)!
=0);
//printf("
Theyarenotcoprime!
\n"
return0;
//产生随机素数p和q
voidprime_random(int*p,int*q)
inti,k;
time_tt;
p[0]=1;
q[0]=3;
//p[19]=1;
//q[18]=2;
p[MAX-1]=10;
q[MAX-1]=11;
t=time(NULL);
srand((unsignedlong)t);
for(i=1;
p[MAX-1]-1;
k=rand()%10;
p[i]=k;
while(k==0)
k=rand()%10;
p[p[MAX-1]-1]=k;
}while((is_prime_san(p))!
=1);
素数p为:
"
p[MAX-1];
p[p[MAX-1]-i-1]);
do
q[MAX-1];
q[i]=k;
}while((is_prime_san(q))!
素数q为:
q[q[MAX-1]-i-1]);
//产生与(p-1)*(q-1)互素的随机数
voiderand(inte[MAX],intm[MAX])
e[MAX-1]=5;
随机产生一个与(p-1)*(q-1)互素的e:
"
e[MAX-1]-1;
e[i]=k;
while((k=rand()%10)==0)
e[e[MAX-1]-1]=k;
}while(coprime(e,m)!
e[MAX-1];
e[e[MAX-1]-i-1]);
//根据上面的p、q和e计算密钥d
voidrsad(inte[MAX],intg[MAX],int*d)
intr[MAX],n1[MAX],n2[MAX],k[MAX],w[MAX];
inti,t[MAX],b1[MAX],b2[MAX],temp[MAX];
mov(g,n1);
mov(e,n2);
k[i]=w[i]=r[i]=temp[i]=b1[i]=b2[i]=t[i]=0;
b1[MAX-1]=0;
b1[0]=0;
//b1=0;
b2[MAX-1]=1;
b2[0]=1;
//b2=1;
while
(1)
k[i]=w[i]=0;
divt(n1,n2,k,w);
//k=n1/n2;
temp[i]=0;
mul(k,n2,temp);
//temp=k*n2;
r[i]=0;
sub(n1,temp,r);
if((r[MAX-1]==1)&
(r[0]==0))//r=0
break;
else
mov(n2,n1);
//n1=n2;
mov(r,n2);
//n2=r;
mov(b2,t);
//t=b2;
mul(k,b2,temp);
//b2=b1-k*b2;
b2[i]=0;
sub(b1,temp,b2);
mov(t,b1);
t[i]=0;
add(b2,g,t);
temp[i]=d[i]=0;
divt(t,g,temp,d);
由以上的(p-1)*(q-1)和e计算得出的d:
d[MAX-1];
d[d[MAX-1]-i-1]);
//求解密密钥d的函数(根据欧几里得算法)
unsignedlongrsa(unsignedlongp,unsignedlongq,unsignedlonge)//求解密密钥d的函数(根据欧几里得算法)
unsignedlongg,k,r,n1,n2,t;
unsignedlongb1=0,b2=1;
g=(p-1)*(q-1);
n1=g;
n2=e;
{k=n1/n2;
r=n1-k*n2;
if(r!
n1=n2;
n2=r;
t=b2;
b2=b1-k*b2;
b1=t;
return(g+b2)%g;
//加密和解密
voidprintbig(structslink*h)
structslink*p;
p=(structslink*)malloc(LEN);
p=h;
if(h!
=NULL)
do
p->
bignum[MAX-1];
p->
bignum[p->
bignum[MAX-1]-i-1]);
p=p->
next;
while(p!
=NULL);
structslink*input(void)//输入明文并且返回头指针,没有加密时候转化的数字
structslink*head;
structslink*p1,*p2;
inti,n,c,temp;
charch;
n=0;
p1=p2=(structslink*)malloc(LEN);
head=NULL;
\n请输入你所要加密的内容:
\n"
while((ch=getchar())!
='
\n'
i=0;
c=ch;
if(c<
c=abs(c);
//把负数取正并且做一个标记
p1->
bignum[MAX-2]='
0'
1'
while(c/10!
temp=c%10;
c=c/10;
bignum[i]=temp;
p1->
bignum[i]=c;
bignum[MAX-1]=i+1;
n=n+1;
if(n==1)
head=p1;
elsep2->
next=p1;
p2=p1;
p1=(structslink*)malloc(LEN);