首页 技术 正文
技术 2022年11月16日
0 收藏 351 点赞 2,590 浏览 2272 个字

RSA是一种非对称加密算法,在公开密钥和电子商业中RSA被广泛使用。它是基于一个很简单的数论事实,两个素数相乘很容易,对两素数乘积因式分解很困难。原理就不再阐述了,我谈谈算法的编程实现过程。

一、RSA加密和解密过程是基于以下形式,其中明文为M,密文为C,公匙PU={e, n},密匙PR={d, n}。

1、准备工作,选择两个大素数p和q,计算p和q的乘积n,计算p-1和q-1的乘积,选择一个与p-1和q-1乘积互质的数e,计算出d

RSA加密算法c++简单实现

2、加密过程

RSA加密算法c++简单实现

3、解密过程

RSA加密算法c++简单实现

程序没有生成大素数,只是列出1000以内的素数,随机取两个素数p和q,利用欧德里德扩展算法计算出e和d,用反复平方法求数的幂

二、程序流程图

RSA加密算法c++简单实现

三、程序源码

 #include <iostream>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std; int Plaintext[];//明文
long long Ciphertext[];//密文
int n, e = , d; //二进制转换
int BianaryTransform(int num, int bin_num[])
{ int i = , mod = ; //转换为二进制,逆向暂存temp[]数组中
while(num != )
{
mod = num%;
bin_num[i] = mod;
num = num/;
i++;
} //返回二进制数的位数
return i;
} //反复平方求幂
long long Modular_Exonentiation(long long a, int b, int n)
{
int c = , bin_num[];
long long d = ;
int k = BianaryTransform(b, bin_num)-; for(int i = k; i >= ; i--)
{
c = *c;
d = (d*d)%n;
if(bin_num[i] == )
{
c = c + ;
d = (d*a)%n;
}
}
return d;
} //生成1000以内素数
int ProducePrimeNumber(int prime[])
{
int c = , vis[];
memset(vis, , sizeof(vis));
for(int i = ; i <= ; i++)if(!vis[i])
{
prime[c++] = i;
for(int j = i*i; j <= ; j+=i)
vis[j] = ;
} return c;
} //欧几里得扩展算法
int Exgcd(int m,int n,int &x)
{
int x1,y1,x0,y0, y;
x0=; y0=;
x1=; y1=;
x=; y=;
int r=m%n;
int q=(m-r)/n;
while(r)
{
x=x0-q*x1; y=y0-q*y1;
x0=x1; y0=y1;
x1=x; y1=y;
m=n; n=r; r=m%n;
q=(m-r)/n;
}
return n;
} //RSA初始化
void RSA_Initialize()
{
//取出1000内素数保存在prime[]数组中
int prime[];
int count_Prime = ProducePrimeNumber(prime); //随机取两个素数p,q
srand((unsigned)time(NULL));
int ranNum1 = rand()%count_Prime;
int ranNum2 = rand()%count_Prime;
int p = prime[ranNum1], q = prime[ranNum2]; n = p*q; int On = (p-)*(q-); //用欧几里德扩展算法求e,d
for(int j = ; j < On; j+=)
{
int gcd = Exgcd(j, On, d);
if( gcd == && d > )
{
e = j;
break;
} } } //RSA加密
void RSA_Encrypt()
{
cout<<"Public Key (e, n) : e = "<<e<<" n = "<<n<<'\n';
cout<<"Private Key (d, n) : d = "<<d<<" n = "<<n<<'\n'<<'\n'; int i = ;
for(i = ; i < ; i++)
Ciphertext[i] = Modular_Exonentiation(Plaintext[i], e, n); cout<<"Use the public key (e, n) to encrypt:"<<'\n';
for(i = ; i < ; i++)
cout<<Ciphertext[i]<<" ";
cout<<'\n'<<'\n';
} //RSA解密
void RSA_Decrypt()
{
int i = ;
for(i = ; i < ; i++)
Ciphertext[i] = Modular_Exonentiation(Ciphertext[i], d, n); cout<<"Use private key (d, n) to decrypt:"<<'\n';
for(i = ; i < ; i++)
cout<<Ciphertext[i]<<" ";
cout<<'\n'<<'\n';
} //算法初始化
void Initialize()
{
int i;
srand((unsigned)time(NULL));
for(i = ; i < ; i++)
Plaintext[i] = rand()%; cout<<"Generate 100 random numbers:"<<'\n';
for(i = ; i < ; i++)
cout<<Plaintext[i]<<" ";
cout<<'\n'<<'\n';
} int main()
{
Initialize(); while(!e)
RSA_Initialize(); RSA_Encrypt(); RSA_Decrypt(); return ;
}

四、运行结果

RSA加密算法c++简单实现

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902