密码学课设实验——RSA算法c++实现

一、实验目的

掌握并实现RSA算法。

 

  • 实验内容

利用C\C++实现RSA算法的加、解密运算。

具体包括:

1) 利用扩展的Euclid计算 a mod n 的乘法逆元;

2) Miller-Rabin素性测试算法对一个给定的大数进行测试;

3) 实现 的运算,并计算 ;

4) 利用Euler定理手工计算 ,并与3)计算的结果对比;

5) 实现RSA算法。并对"I LOVE NANJING UNIVERSITY OF AERONAUTICS AND ASTRONAUTICS"加解密。

 

  • 实验步骤
  1. 计算乘法逆元:

按照书p94上的模n求逆算法写函数。

  1. Miller-Rabin素性测试:

按照书上p110上的Miller-Rabin素性测试算法写函数。

  1. 求大数幂乘:

按照书上p95上的模n的大数幂乘的快速算法写函数。

  1. RSA算法的实现

按照书上p96上的RSA算法写函数。

由于没想到大数如何实现及使用,p,q只使用了1000以内的素数,可能会出现错误。

首先计算n=p*q,利用前面实现的扩展欧几里得算法求出e,d,然后按照模运算实现加密与解密,因为使用了小素数,所以直接利用字符的ascii码进行加解密,而没有利用编码.

 

代码如下:

#include"pch.h"
#include "iostream"
using namespace std;
#include <cmath>
#include <cstring>
#include <ctime>
#include <cstdlib>

typedef long long int ll;
ll Power_mul(ll a, ll b, ll c, ll n);//幂乘

//***************************扩展欧几里得计算乘法逆元***************************
//记gcd(a,b)表示非负整数a,b的最大公因数,那么:gcd(a,b)=gcd(b,a%b)
ll gcd(ll a, ll b)
{
	if (a < b)
	{
		ll temp = b;
		b = a;
		a = temp;
	}
	if (a%b == 0)
		return b;
	else
		return gcd(b, a%b);
}

//gcd(n,u)=an+bu;
ll ExtendEculid(ll n1, ll n2, ll b1, ll b2)
{
	ll q, r;
	q = n1 / n2;
	r = n1 - q*n2;
	if (r != 0)
	{
		ll temp = b1 - q*b2;
		ExtendEculid(n2, r, b2, temp);
	}
	if (n2 == 1)
		return b2;
}

//调用过程
void Inverse_element()
{
	cout << "u * v == 1(mod n)\n";
	cout << "";
	ll u, n, t;
	cout << "请输入u:";
	cin >> u;
	cout << "请输入n:";
	cin >> n;
	if (gcd(n, u) != 1)
		cout << "两个数不互素,没有乘法逆元!" << endl << endl;
	else
	{
		t = ExtendEculid(n, u, 0, 1);
		if (t < 0)
			t += n;
		cout << u << "mod" << n << "的逆元为" << t << endl;
	}
}


//***************************Miller-Rabin素性测试算法***************************
bool Miller_Rabin(ll n)
{
	ll r = 0;
	ll z = 0;
	ll m = 0;
	ll s = 0;
	m = n - 1;
	if (n < 2)
		return false;
	else if (n == 2)
		return true;

	while (m % 2 == 0) {
		m /= 2;
		s = s + 1;
	}
	srand(time(NULL));
	ll b = rand() % (n - 2) + 2;
	/*for(int i = 0;i < m - 1;i++)
		z1 = b*b;
	z1 = ((z1 % n) + n)% n;*/
	z = Power_mul(b, m, 1, n);
	if (z == 1 || z == n - 1)
		return true;
	else
	{
		while (1) {
			if (r == (s - 1))
				return false;
			else
			{
				r = r + 1;
				z = Power_mul(z, 2, 1, n);
				if (z == (n - 1))
					return true;
			}
		}
	}
}

void MR()
{
	ll N;
	bool result;
	cout << "请输入N:";
	cin >> N;
	result = Miller_Rabin(N);
	if (result)
		cout << N << "是素数" << endl;
	else
		cout << "不是素数" << endl;
}

//***************************模n的大数幂乘的快速算法***************************
ll Power_mul(ll a,ll b,ll c,ll n)
{
	//初始a=x,b=r,c=1
	while (b != 0)
	{
		while (b % 2 == 0)
		{
			b /= 2;
			a = (a * a) % n;
		}
		b -= 1;
		c = (c * a) % n;
	}
	return	c;
}

void PM()
{
	cout << "x^r mod n" << endl;
	ll x, r, n;
	cout << "请输入x:";
	cin >> x;
	cout << "请输入r:";
	cin >> r;
	cout << "请输入n:";
	cin >> n;
	ll out = Power_mul(x, r, 1, n);
	cout << x << "^" << r << " mod " << n << " = " << out << endl;
	
}


//***************************RSA加密***************************
int Plaintext[100];//明文
long long Ciphertext[100];//密文
int Plaintext_C[100];
int n, e = 0, 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;
}

//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 = 2; j < On; j += 1331)
	{
		d = ExtendEculid(On, j, 0, 1);
		if (gcd(On,j) == 1 & d>0)
		{
			e = j;
			break;
		}
    }
}

//RSA加密
void RSA_Encrypt()
{
	cout << endl;
	cout << "公钥(e, n) : e = " << e << " n = " << n << "\t";
	cout << "私钥(d, n) : d = " << d << " n = " << n << endl;

	int i = 0;
	for (i = 0; i < 100; i++)
		Ciphertext[i] = Power_mul(Plaintext[i], e, 1, n);

	cout << endl;
	cout << "用公钥(e, n)加密出的密文如下:" ;
	for (i = 0; i < 100; i++)
		cout << Ciphertext[i] << " ";
	cout << endl;
}

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

	cout << endl;
	cout << "用私钥(d, n)解密出的明文如下:" ;
	for (i = 0; i < 100; i++)
		printf("%c", Plaintext_C[i]);
	cout << endl;
}


//明文读入,因为取的素数比较小,所以直接用了ascii码来写
void Initialize()
{
	char ch[] = "I LOVE NANJING UNIVERSITY OF AERONAUTICS AND ASTRONAUTICS";
	int counter=0;
	while (ch[counter] != '\0')
	{
		counter++;
	}
	//cout << "counter:" << counter << endl;
	int i,j=0;
	for (i = 0; i < 100; i++)
	{
		if (j != counter)
		{
			Plaintext[i] = ch[j];
			j++;
		}
		else
		{
			Plaintext[i] = 32;//空格
		}

	}
	cout << "明文如下:";
	for (i = 0; i < 100; i++)
	{
		printf("%c", Plaintext[i]);
	}
	cout << endl;
}

int RSA_main()
{
	Initialize();
	while (!e)
		RSA_Initialize();
	RSA_Encrypt();
	RSA_Decrypt();
	return 0;
}



//***************************main函数***************************
int main()
{
	while (true)
	{
		cout << "菜单选项" << endl;
		cout << "1.求乘法逆元\t2.Miller-Rabin素性测试\t3.求大数幂乘\t4.RSA" << endl;
		int choice;
		cout << "请输入你的选择:";
		cin >> choice;
		switch (choice)
		{
		case 1:Inverse_element();
			break;
		case 2:MR();
			break;

		case 3:PM();
			break;
		case 4:RSA_main();
			break;
		default:break;
		}
		cout << endl;
	}
	return 0;
}

 

实验结果:

  • 7
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值