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

点击打开原文

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

1

2、加密过程

2

3、解密过程

3

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

二、程序流程图

          5

#include <iostream>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;


int Plaintext[100];//明文
long long Ciphertext[100];//密文
int n, e = 0, d;

//二进制转换
int BianaryTransform(int num, int bin_num[])
{

	int i = 0,  mod = 0;

	//转换为二进制,逆向暂存temp[]数组中
	while(num != 0)
	{
		mod = num%2;
		bin_num[i] = mod;
		num = num/2;
		i++;
	}

	//返回二进制数的位数
	return i;
}

//反复平方求幂
long long Modular_Exonentiation(long long a, int b, int n)
{
	int c = 0, bin_num[1000];
	long long d = 1;
	int k = BianaryTransform(b, bin_num)-1;

	for(int i = k; i >= 0; i--)
	{
		c = 2*c;
		d = (d*d)%n;
		if(bin_num[i] == 1)
		{
			c = c + 1;
			d = (d*a)%n;
		}
	}
	return d;
}

//生成1000以内素数
int ProducePrimeNumber(int prime[])
{
	int c = 0, vis[1001];
	memset(vis, 0, sizeof(vis));
	for(int i = 2; i <= 1000; i++)if(!vis[i])
	{
		prime[c++] = i;
		for(int j = i*i; j <= 1000; j+=i)
			vis[j] = 1;
	}

	return c;
}


//欧几里得扩展算法
int Exgcd(int m,int n,int &x)
{
	int x1,y1,x0,y0, y;
	x0=1; y0=0;
	x1=0; y1=1;
	x=0; y=1;
	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[5000];
	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-1)*(q-1);


	//用欧几里德扩展算法求e,d
	for(int j = 3; j < On; j+=1331)
	{
		int gcd = Exgcd(j, On, d);
		if( gcd == 1 && d > 0)
		{
			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 = 0;
	for(i = 0; i < 100; i++)
		Ciphertext[i] = Modular_Exonentiation(Plaintext[i], e, n);

	cout<<"Use the public key (e, n) to encrypt:"<<'\n';
	for(i = 0; i < 100; i++)
		cout<<Ciphertext[i]<<" ";
	cout<<'\n'<<'\n';
}

//RSA解密
void RSA_Decrypt()
{
	int i = 0;
	for(i = 0; i < 100; i++)
		Ciphertext[i] = Modular_Exonentiation(Ciphertext[i], d, n);

	cout<<"Use private key (d, n) to decrypt:"<<'\n';
	for(i = 0; i < 100; i++)
		cout<<Ciphertext[i]<<" ";
	cout<<'\n'<<'\n';
}


//算法初始化
void Initialize()
{
	int i;
	srand((unsigned)time(NULL));
	for(i = 0; i < 100; i++)
		Plaintext[i] = rand()%1000;

	cout<<"Generate 100 random numbers:"<<'\n';
	for(i = 0; i < 100; i++)
		cout<<Plaintext[i]<<" ";
	cout<<'\n'<<'\n';
}

int main()
{
	Initialize();

	while(!e)
		RSA_Initialize();

	RSA_Encrypt();

	RSA_Decrypt();

	return 0;
}

运行结果:

RSA加密算法C语言实现需要用到大数运算库,可以使用GMP库来实现。以下是一个简单RSA加密算法C语言实现的代码示例: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> #include <gmp.h> void generate_key(mpz_t p, mpz_t q, mpz_t n, mpz_t e, mpz_t d) { // Generate random prime numbers p and q gmp_randstate_t state; gmp_randinit_default(state); gmp_randseed_ui(state, time(NULL)); mpz_t bit_size; mpz_init(bit_size); mpz_set_ui(bit_size, 1024); // Set key size to 1024 bits mpz_t one; mpz_init(one); mpz_set_ui(one, 1); mpz_urandomb(p, state, bit_size); mpz_urandomb(q, state, bit_size); while (!mpz_probable_prime_p(p, state)) { mpz_add(p, p, one); } while (!mpz_probable_prime_p(q, state)) { mpz_add(q, q, one); } // Compute n mpz_mul(n, p, q); // Compute phi(n) mpz_t phi_n; mpz_init(phi_n); mpz_t p_minus_one; mpz_init(p_minus_one); mpz_sub_ui(p_minus_one, p, 1); mpz_t q_minus_one; mpz_init(q_minus_one); mpz_sub_ui(q_minus_one, q, 1); mpz_mul(phi_n, p_minus_one, q_minus_one); // Choose e mpz_t gcd; mpz_init(gcd); do { mpz_urandomm(e, state, phi_n); mpz_gcd(gcd, e, phi_n); } while (mpz_cmp_ui(gcd, 1) != 0); // Compute d mpz_invert(d, e, phi_n); gmp_randclear(state); mpz_clear(bit_size); mpz_clear(one); mpz_clear(phi_n); mpz_clear(p_minus_one); mpz_clear(q_minus_one); mpz_clear(gcd); } void rsa_encrypt(mpz_t m, mpz_t n, mpz_t e, mpz_t c) { mpz_powm(c, m, e, n); } void rsa_decrypt(mpz_t c, mpz_t n, mpz_t d, mpz_t m) { mpz_powm(m, c, d, n); } int main(void) { // Generate key mpz_t p, q, n, e, d; mpz_init(p); mpz_init(q); mpz_init(n); mpz_init(e); mpz_init(d); generate_key(p, q, n, e, d); // Encrypt a message char message[] = "Hello, world!"; mpz_t m, c; mpz_init(m); mpz_import(m, strlen(message), 1, sizeof(char), 0, 0, message); mpz_init(c); rsa_encrypt(m, n, e, c); gmp_printf("Encrypted message: %Zd\n", c); // Decrypt the message mpz_t m2; mpz_init(m2); rsa_decrypt(c, n, d, m2); size_t size; char* message2 = (char*)mpz_export(NULL, &size, 1, sizeof(char), 0, 0, m2); message2[size] = '\0'; // Add null terminator printf("Decrypted message: %s\n", message2); // Clean up mpz_clear(p); mpz_clear(q); mpz_clear(n); mpz_clear(e); mpz_clear(d); mpz_clear(m); mpz_clear(c); mpz_clear(m2); free(message2); return 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值