RSA加密算法

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

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

#include<iostream>
#include
<string>
#include
<ctime>
#include
"math.h"
usingnamespacestd;

boolisPrime(long);//thenumberisprimeandreturntrue,elsereturnfalse;
longgcd(longa,longb);//findanumberwhichthetwonumberaandbdivideexactly.
longpublicKey(longp,longq);//usingp,qtocalculatethepublickeye.
longprivateKey(longe,longQn);//usinge,Qntocalculatetheprivatekeyd.
longgenerKey(constlongp,constlongq,long*pubKey,long*priKey);//getthepublickeyandprivatekey.Andreturnn=p*q.
//char*encode(char*,longn,longpubKey);//encodetheText
//char*decode(char*,longn,longpriKey);//decodetheTextwhichhasbeenencoded.
longencode(long,longn,longpubKey);//encodetheText
longdecode(long,longn,longpriKey);//decodetheTextwhichhasbeenencoded.
intmain()
...{

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;
}


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

returntrue;
}

returnfalse;
}


longgcd(longa,longb)
...{
intx=a%b;
if(x==0)
returnb;
else
returngcd(b,x);
}


longpublicKey(constlongp,constlongq,constlongQn)
...{

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;
}


longprivateKey(constlonge,constlongQn)
...{
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;
}


longgenerKey(constlongp,constlongq,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;
}
*/

longencode(longnum,longn,longpubKey)
...{
longtmp=1;
for(inti=0;i<pubKey;i++)
...{
tmp
*=num;
tmp
%=n;
}

returntmp;
}

longdecode(longnum,longn,longpriKey)
...{
longtmp=1;
for(inti=0;i<priKey;i++)
...{
tmp
*=num;
tmp
%=n;
}

returntmp;
}
【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值