RSA加密算法

RSA 算法:
其中加密encode和解密decode中的效率可以提高的log(n)

但是这个算法只是原型,只能对数字加密,对字符串加密时要将转换成AScII码的东东进行拆分才可以。要按位加密。当q,p过大时,将有溢出。改进的可以自己写一个大数类。但没有时间了,暂时就做成这个样子吧。

#include < iostream >
#include
< string >
#include
< ctime >
#include
" math.h "
using namespace std;

bool isPrime( long ); // thenumberisprimeandreturntrue,elsereturnfalse;
long gcd( long a, long b); // findanumberwhichthetwonumberaandbdivideexactly.
long publicKey( long p, long q); // usingp,qtocalculatethepublickeye.
long privateKey( long e, long Qn); // usinge,Qntocalculatetheprivatekeyd.
long generKey( const long p, const long q, long * pubKey, long * priKey); // getthepublickeyandprivatekey.Andreturnn=p*q.
// char*encode(char*,longn,longpubKey); // encodetheText
// char*decode(char*,longn,longpriKey); // decodetheTextwhichhasbeenencoded.
long encode( long , long n, long pubKey); // encodetheText
long decode( long , long n, long priKey); // decodetheTextwhichhasbeenencoded.
int main()
... {

longp,q;
longpubKey;
longpriKey;
boolpass=true;
while(pass)
...{
cout
<<"inputtwoprimenumbers!andthetwonumbersarebiggerthantwo!!"<<endl;
cin
>>p>>q;
//srand(time(0));
//q=rand();
//p=rand();
if(isPrime(p)&&isPrime(q))
pass
=false;
}

cout
<<"p="<<p<<"q="<<q<<endl;
longn=generKey(p,q,&pubKey,&priKey);
cout
<<"n:"<<n<<endl;
cout
<<"inputthenumberforencoding!"<<endl;
longnum;
cin
>>num;
//cout<<decode(encode(txt,n,pubKey),n,priKey);
longenText=encode(num,n,pubKey);
cout
<<"encodetheText:"<<enText<<endl;
cout
<<"decodetheText:"<<decode(enText,n,priKey)<<endl;
return0;
}


bool isPrime( long a)
... {
if(a>2)
...{
intb=2,c=sqrt(a);
for(;b<c;b++)
...{
if(a%b==0)
returnfalse;
}

returntrue;
}

returnfalse;
}


long gcd( long a, long b)
... {
intx=a%b;
if(x==0)
returnb;
else
returngcd(b,x);
}


long publicKey( const long p, const long q, const long Qn)
... {

srand(time(
0));
longe=rand();
if(e>=Qn)
e
=3;
while(gcd(e,Qn)!=1)
...{

e
++;
if(e>=Qn)
...{
e
=3;
}

}

cout
<<"e:"<<e<<endl;
returne;
}


long privateKey( const long e, const long Qn)
... {
srand(time(
0));
longd=rand();
if(d>=Qn)
d
=3;
while(e*d%Qn!=1||e==d)
...{
d
++;
if(d>=Qn)
...{
d
=3;
}

}

cout
<<"d:"<<d<<endl;
returnd;
}


long generKey( const long p, const long q, long * pubKey, long * priKey)
... {
longn=p*q;
longQn=(p-1)*(q-1);
*pubKey=publicKey(p,q,Qn);
*priKey=privateKey(*pubKey,Qn);
returnn;
}




/**/ /*

char*encode(char*txt,longn,longpubKey)
{
intlength=strlen(txt);
char*text=newchar[length];
strcpy(text,txt);
for(inti=0;i<length;i++)
{

text[i]=(int)pow(text[i],pubKey)%n;
}
returntext;
}

char*decode(char*txt,longn,longpriKey)
{
intlength=strlen(txt);
char*text=newchar[length];
strcpy(text,txt);
for(inti=0;i<length;i++)
{
text[i]=(int)pow(text[i],priKey)%n;
}
returntext;
}
*/

long encode( long num, long n, long pubKey)
... {
longtmp=1;
for(inti=0;i<pubKey;i++)
...{
tmp
*=num;
tmp
%=n;
}

returntmp;
}

long decode( long num, long n, long priKey)
... {
longtmp=1;
for(inti=0;i<priKey;i++)
...{
tmp
*=num;
tmp
%=n;
}

returntmp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值