求RSA加密解密算法,c++源代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求RSA加密解密算法,c++源代码相关的知识,希望对你有一定的参考价值。

知道公钥e和密钥d,如何进行加密解密编码

//下面程序由520huiqin编写,已在VC++ 6.0下编译通过

#include <iostream.h>
#include <math.h>
#include <stdio.h>

typedef int Elemtype;
Elemtype p,q,e;
Elemtype fn;
Elemtype m,c;
int flag = 0;
typedef void (*Msghandler) (void);
struct MsgMap
char ch;
Msghandler handler;
;
/* 公钥 */
struct PU
Elemtype e;
Elemtype n;
pu;
/* 私钥 */
struct PR
Elemtype d;
Elemtype n;
pr;
/* 判定一个数是否为素数 */
bool test_prime(Elemtype m)
if (m <= 1)
return false;

else if (m == 2)
return true;

else
for(int i=2; i<=sqrt(m); i++)
if((m % i) == 0)
return false;
break;


return true;


/* 将十进制数据转化为二进制数组 */
void switch_to_bit(Elemtype b, Elemtype bin[32])
int n = 0;
while( b > 0)
bin[n] = b % 2;
n++;
b /= 2;


/* 候选菜单,主界面 */
void Init()
cout<<"*********************************************"<<endl;
cout<<"*** Welcome to use RSA encoder ***"<<endl;
cout<<"*** a.about ***"<<endl;
cout<<"*** e.encrypt ***"<<endl;
cout<<"*** d.decrypt ***"<<endl;
cout<<"*** s.setkey ***"<<endl;
cout<<"*** q.quit ***"<<endl;
cout<<"**********************************by*Terry***"<<endl;
cout<<"press a key:"<<endl;

/* 将两个数排序,大的在前面*/
void order(Elemtype &in1, Elemtype &in2)
Elemtype a = ( in1 > in2 ? in1 : in2);
Elemtype b = ( in1 < in2 ? in1 : in2);
in1 = a;
in2 = b;

/* 求最大公约数 */
Elemtype gcd(Elemtype a, Elemtype b)
order(a,b);
int r;
if(b == 0)
return a;

else
while(true)
r = a % b;
a = b;
b = r;
if (b == 0)
return a;
break;





/* 用扩展的欧几里得算法求乘法逆元 */
Elemtype extend_euclid(Elemtype m, Elemtype bin)
order(m,bin);
Elemtype a[3],b[3],t[3];
a[0] = 1, a[1] = 0, a[2] = m;
b[0] = 0, b[1] = 1, b[2] = bin;
if (b[2] == 0)
return a[2] = gcd(m, bin);

if (b[2] ==1)
return b[2] = gcd(m, bin);

while(true)
if (b[2] ==1)
return b[1];
break;

int q = a[2] / b[2];
for(int i=0; i<3; i++)
t[i] = a[i] - q * b[i];
a[i] = b[i];
b[i] = t[i];



/* 快速模幂算法 */
Elemtype modular_multiplication(Elemtype a, Elemtype b, Elemtype n)
Elemtype f = 1;
Elemtype bin[32];
switch_to_bit(b,bin);
for(int i=31; i>=0; i--)
f = (f * f) % n;
if(bin[i] == 1)
f = (f * a) % n;


return f;

/* 产生密钥 */
void produce_key()
cout<<"input two primes p and q:";
cin>>p>>q;
while (!(test_prime(p)&&test_prime(q)))
cout<<"wrong input,please make sure two number are both primes!"<<endl;
cout<<"input two primes p and q:";
cin>>p>>q;
;
pr.n = p * q;
pu.n = p * q;
fn = (p - 1) * (q - 1);
cout<<"fn = "<<fn<<endl;
cout<<"input e :";
cin>>e;
while((gcd(fn,e)!=1))
cout<<"e is error,try again!";
cout<<"input e :";
cin>>e;

pr.d = (extend_euclid(fn,e) + fn) % fn;
pu.e = e;
flag = 1;
cout<<"PR.d: "<<pr.d<<" PR.n: "<<pr.n<<endl;
cout<<"PU.e: "<<pu.e<<" PU.n: "<<pu.n<<endl;

/* 加密 */
void encrypt()
if(flag == 0)
cout<<"setkey first:"<<endl;
produce_key();

cout<<"input m:";
cin>>m;
c = modular_multiplication(m,pu.e,pu.n);
cout<<"c is:"<<c<<endl;

/* 解密 */
void decrypt()
if(flag == 0)
cout<<"setkey first:"<<endl;
produce_key();

cout<<"input c:";
cin>>c;
m = modular_multiplication(c,pr.d,pr.n);
cout<<"m is:"<<m<<endl;

/* 版权信息 */
void about()
cout<<"*********************************************"<<endl;
cout<<"*** by Terry ***"<<endl;
cout<<"*** copyright 2010,All rights reserved by ***"<<endl;
cout<<"*** Terry,technology supported by weizuo !***"<<endl;
cout<<"*** If you have any question, please mail ***"<<endl;
cout<<"*** to 18679376@qq.com ! ***"<<endl;
cout<<"*** Computer of science and engineering ***"<<endl;
cout<<"*** XiDian University 2010-4-29 ***"<<endl;
cout<<"*********************************************"<<endl;
cout<<endl<<endl;
Init();

/* 消息映射 */
MsgMap Messagemap[] =
'a',about,
's',produce_key,
'd',decrypt,
'e',encrypt,
'q',NULL
;
/* 主函数,提供循环 */
void main()
Init();
char d;
while((d = getchar())!='q')
int i = 0;
while(Messagemap[i].ch)
if(Messagemap[i].ch == d)
Messagemap[i].handler();
break;

i++;




//欢迎分享,盗窃可耻

参考资料:http://hi.baidu.com/520huiqin/blog/item/f6bf271bef19be76dab4bd9b.html

参考技术A #include<iostream.h>
#include<stdio.h>
#include<math.h>
int pf_c(int m,int k);
int pf(int m1,int n1);
int gcd(int f);
int r;
int h;
void main()
int a,b,c,d,d1,a1,b1,c1;
cout<<"请输入你选择的2个大素数!"<<endl;
cin>>a1;
cin>>b1;
r=a1*b1;
c=(a1-1)*(b1-1);
c1=gcd(c);
cout<<"公开钥为:"<<c1<<endl;
cout<<"请选择你要的操作:1.加密 2.解密"<<endl;
cin>>a;
switch(a)
case 1: cout<<"请输入明文:"<<endl;
cin>>b;
cout<<"密文为:"<<pf_c(b,c1)<<endl;
break;
case 2: cout<<"请输入密文:"<<endl;
cin>>d;
d1=pf(c,c1);
cout<<"私密钥为:"<<d1<<endl;
cout<<"明文为:"<<pf_c(d,d1)<<endl;
break;

getchar();

int pf_c(int m,int k)

int a,i1,a1,b[50],c1,c;
c=0;c1=1;i1=0;
do
a=k/2;
a1=k%2;
b[i1]=a1;
k=a;
i1++;
while(a>0);
i1--;
for(int i=i1;i>=0;i--)

c=2*c;
c1=(c1*c1)%r;
if(b[i]==1)

c=c+1;
c1=(c1*m)%r;


return c1;

int pf(int m1,int n1)

int x1=1,x2=0,x3;
int y1=0,y2=1,y3;
x3=m1;
y3=n1;
int d;
for(int i=0; ;i++)

int q=x3/y3;
int t1=x1-q*y1;
int t2=x2-q*y2;
int t3=x3-q*y3;
x1=y1;
x2=y2;
x3=y3;
y1=t1;
y2=t2;
y3=t3;
if(y3==1)

if(y2<0) d=m1+y2;
else d=y2;
break;


return d;

int gcd(int f)

int x1=1,x2=0,x3;
int y1=0,y2=1,y3;
for(int i1=2;i1<f;i1++)

x3=f;
y3=i1;
int q=x3/y3;
int t1=x1-q*y1;
int t2=x2-q*y2;
int t3=x3-q*y3;
x1=y1;
x2=y2;
x3=y3;
y1=t1;
y2=t2;
y3=t3;
if(y3==1)

return i1;
break;


参考技术B RSA算法表述
假定用户A欲发送消息m给用户B,则RSA算法的加/解密过程为:
1) 首先用户B产生两个大素数p和q(p、q是保密的)。
2) 用户B计算n=pq和ø(n)=(p-1)(q-1)(ø(n)是保密的)。
3) 用户B选择一个随机数e(0<e< ø(n)),使得(e,ø(n))=1,即e和ø(n)互素(除了1无其他公约数)。
4) 用户B通过计算得出d,使得d*e mod ø(n)=1(即在与n互素的数中选取与ø(n)互素的数,d是用户B自留且保密的,用作解密密钥)。
5) 用户B将n及e作为公钥公开。
6) 用户A通过公开渠道查到n和e。
7) 对m施行加密变换,即EB(B是下标)(m)=me(m的e次方) mod n =c。
8) 用户B收到密文c后,施行解密变换:
DB(B是下标)(c)=cd(c的d次方) mod n=( me(m的e次方) mod n)d mod n =med(m的ed次方) mod n =m mod n 。

最近学了这个,从书上抄下来的。。
参考技术C 问百度、

php rsa加密 已有明文和公钥 只需加密

代码:
$encrypted = '';
$key = openssl_pkey_get_public($pubkey);
var_dump($key);
openssl_public_encrypt('a',$encrypted,$key);
echo $encrypted;

失败提示:
Warning: openssl_public_encrypt(): Don't know how to get public key from this private key in E:\WWW\php_20170720\api\bdy\api.php on line 131

Warning: openssl_public_encrypt(): key parameter is not a valid public key in E:\WWW\php_20170720\api\bdy\api.php on line 131

参考技术A 你的公钥有问题,不是有效的公钥。本回答被提问者采纳

以上是关于求RSA加密解密算法,c++源代码的主要内容,如果未能解决你的问题,请参考以下文章

RSA 加密算法在C++中的实现 面向初学者(附代码)

求正确的RSA加密解密算法C语言的,多谢。

RSA算法的C++实现

给出p、q、e、M,设计一个RSA算法,求公钥,私钥,并且利用RSA算法加密和解密?

php rsa加密 已有明文和公钥 只需加密

RSA加密算法已知公钥对,求d