RSA加解密实验报告文档格式.docx
《RSA加解密实验报告文档格式.docx》由会员分享,可在线阅读,更多相关《RSA加解密实验报告文档格式.docx(18页珍藏版)》请在冰豆网上搜索。
双机加解密:
单机加解密:
程序代码及注释:
头文件:
#pragmaonce
classRSA
{
public:
RSA();
~RSA();
staticCStringEncrpt(CStringplaintext,intkey);
staticCStringDecrypt(CStringciphertext,intkey);
CStringencrpt(CStringplaintext,intkey);
CStringdecrypt(CStringciphertext,intkey);
intprime(intm);
intgcd(intx,inty);
intExtendedEuclidean(inta,intb,int*r);
intpower(inta);
intmod(intf,intg,inth);
};
主程序:
#include"
stdafx.h"
RSA.h"
RSA:
:
RSA()
}
~RSA()
intRSA:
prime(intm)//判断是否为素数
inti;
for(i=2;
i<
m/2;
i++)
{
if(m%i==0)
return0;
}
return1;
gcd(intx,inty)//判断是否互素
intt;
while(y)
t=x;
x=y;
y=t%y;
returnx;
ExtendedEuclidean(inta,intb,int*r)//根据扩展欧几里得算法求乘法逆元
intx1,x2,x3,y1,y2,y3,t1,t2,t3,c;
x1=y2=1;
x2=y1=0;
if(a>
=b)
x3=a;
y3=b;
else
x3=b;
y3=a;
while
(1)
c=x3/y3;
t1=x1-c*y1;
t2=x2-c*y2;
x1=y1;
x2=y2;
t3=x3-c*y3;
x3=y3;
y1=t1;
y2=t2;
y3=t3;
if(y3==1)
{
*r=y2;
return1;
;
}
power(inta)//2的a次幂
intb=1,i;
for(i=1;
i<
=a;
b=b*2;
returnb;
mod(intf,intg,inth)//加解密
ints=1;
=g;
s=s*f;
s=s%h;
returns;
CStringRSA:
encrpt(CStringplaintext,intkey)
CStringciphertext;
inti,j,b,a,sum,k;
intt,temp,l=0,ch=1,f,l1;
intp,q,n,y,e,d=0;
intnum[200];
charplain[100];
intplain1[100],cipher[100],al[10000],bl[10000];
//printf("
请输入第一个大素数p:
"
);
//输入两个大素数p,q
//scanf("
%d"
&
p);
p=519817;
if(prime(p)==1)
break;
else
printf("
p不是素数,请重新输入:
//printf("
请输入第二个大素数q:
q);
q=521887;
if(prime(q)==1)
q不是素数,请重新输入:
n=p*q;
y=(p-1)*(q-1);
n=%dy=%d\n"
n,y);
请输入一个整数e:
//输入与y互素的整数e
e);
e=key;
if(gcd(e,y)==1)
//printf("
e不符合条件,请重新输入:
AfxMessageBox(L"
e不符合条件,请重新输入"
ExtendedEuclidean(e,y,&
d);
//计算d
d=%d\n"
d);
公钥:
(%d,%d)\n"
e,n);
私钥:
d,n);
while(ch<
n)//明文分块
ch=ch*2;
l++;
l=l-1;
//每块长度
%d\n"
l);
请选择:
1.加密;
2.解密;
\n"
//scanf("
b);
b=1;
if(b==1)//加密
//printf("
请输入明文:
//scanf("
%s"
plain);
intii;
for(ii=0;
ii<
plaintext.GetLength();
ii++)
plain[ii]=plaintext[ii];
plain[ii]='
\0'
t=strlen(plain);
/*明文信息数字化*/
for(i=0;
t;
i++)//转换为二进制
j=6;
temp=plain[i];
while(temp!
=0)
{
al[i*8+j+1]=temp%2;
temp=temp/2;
j--;
}
al[i*8]=0;
if(t*8%l==0)//转为十进制
f=t*8/l;
for(i=0;
f;
sum=0;
for(j=0;
j<
l;
j++)
{
sum=sum+al[j+i*f]*power(l-1-j);
}
num[i]=sum;
//printf("
%d"
num[i]);
CStringtmp;
tmp.Format(_T("
),num[i]);
ciphertext+=tmp;
//printf("
f=t*8/l+1;
l1=t*8%l;
f-1;
sum=0;
for(j=0;
l1;
sum=sum+al[j+i*l]*power(l1-1-j);
num[f-1]=sum;
num[f-1]);
CStringtmp;
tmp.Format(_T("
),num[f-1]);
ciphertext+=tmp;
i++)//对明文加密
cipher[i]=mod(num[i],e,n);
cipher[i]);
returnciphertext;
decrypt(CStringciphertext,intkey)
CStringplaintext;
e不符合条件,请重新输入"
b=2;
if(b==2)//解密
//printf("
请输入密文的个数:
a);
a=ciphertext.GetLength();
a;
请输入第%d个密文数字:
i+1);
//scanf("
cipher[i]);
cipher[i]=ciphertext[i];
num[i]=mod(cipher[i],d,n);
/*数字转换为明文*/
i++)//转化为二进制
j=0;
temp=num[i];
bl[j]=temp%2;
j++;
while(j<
l)
bl[j]=0;
for(k=0;
k<
k++)
al[k+l*i]=bl[l-1-k];
//printf("
al[k+l*i]);
//printf("
f=a*l/8;
l1=a*l%8;
f*8;
bl[i]=al[i];
if(l1!
for(i=(a-1)*l;
a*l-l1;
bl[i]=al[i+l1];
i++)//转化为十进制
8;
sum=sum+bl[j+i*8]*power(7-j);
plain1[i]=sum;
%c"
plain1[i]);
plaintext+=tmp;
returnplaintext;
Encrpt(CStringplaintext,intkey)
RSAr;
CStringciphertext=r.encrpt(plaintext,key);
Decrypt(CStringciphertext,intkey)
CStringplaintext=r.decrypt(ciphertext,key);
六、结束语
在这次实验课中,我们以分组的方式完成了对自定义数据的单机加密以及多机加密,在整个上机实践过程中,我承担了RSA加密的编程任务,经过这一阶段的程序编辑,我对于这种加密方式有了一个新的理解与认识,虽然在整体的程序设计中还存在着一定不足(比如大素数的选择并没有根据提供的数据进行计算,而是从网上提供的数中随便选择了一个作为固定的大素数,安全性大大降低),然而我相信,随着对现代加密技术的理解逐渐加深,我对这个加密方式的运用也会逐渐熟悉。
七、参考文献
《经典密码学与现代密码学》
Richard
Spillman(美)
清华大学出版社
《c++
Primer中文版》
Stanley.B.Moo(美)
电子工业出版社
《密码学原理与实践(第三版)》
(加)斯廷森(Stinson,D.R.)
电子工业出版社