hust 密码学课设 rsa参数d

hust密码学课设 rsa参数d,

在这里插入图片描述

#include <stdio.h>
#include <gmpxx.h>
#include<time.h>   
#include<stdlib.h>
#pragma warning (disable:4996)


mpz_class modmod(mpz_class a, mpz_class m, mpz_class n)
{
	mpz_class  result = 1;

	while (m != 0) {

		if ((m & 1) == 1) {
			result = (result * a) % n;
		}
		a = (a * a) % n;
		m = m>>1;
	}
	return result;
}
//求最大公因数
mpz_class maxgcd(mpz_class a, mpz_class b)
{

	while (1)
	{
		while (a > b)
			a = a - b;
		if (a < b)
		{
			b = b - a;
			continue;
		}
		else
			return a;
	}
}

//求参数d
mpz_class rsad(mpz_class e, mpz_class m)
{
	mpz_class x1, x2, x3, y1, y2, y3, t1, t2, t3, q;
	x1 = y2 = 1;
	x2 = y1 = 0;
	x3 = (e >= m) ? e : m;
	y3 = (e >= m) ? m : e;
	while (1)
	{
		if (y3 == 0)
			return 0;
		if (y3 == 1)
			return y2;
		q = x3 / y3;
		t1 = x1 - q * y1;
		t2 = x2 - q * y2;
		t3 = x3 - q * y3;
		x1 = y1;
		x2 = y2;
		x3 = y3;
		y1 = t1;
		y2 = t2;
		y3 = t3;
	}
}

//素性检测
int pjudge(mpz_class n)
{
	mpz_class a, b, m, r;
	m = n - 1;
	int i, k = 0;
	while ((m % 2) != 0) {
		m >>= 2;
		k++;

	}
	//随机数生成
	gmp_randstate_t rstate;
	gmp_randinit_default(rstate);
	gmp_randseed_ui(rstate, time(NULL));
	mpz_class n1 = n - 1;
	mpz_urandomm(a.get_mpz_t(), rstate, n1.get_mpz_t());
	a = a + 1;
	b = modmod(a, m, n);//a的m次方模n
	if ((b % n) == 1)
		return 1;
	for (i = 0; i < k; i++) {
		if ((b % n) == ((-1) % n))
			return 1;
		else
			b = (b * b) % n;
	}
	return 0;
}


int main()
{
	int n, i;
	mpz_class e, p, q, m, d;
	scanf("%d", &n);
	for (i = 0; i < n; i++)//问题的个数
	{

		gmp_scanf("%Zd %Zd %Zd", e.get_mpz_t(), p.get_mpz_t(), q.get_mpz_t());
		m = (p - 1) * (q - 1);

		if (e<65536)
		{
			printf("ERROR\n");
			continue;
		}//判断e不能太小

		//判断是否素数
		if (pjudge(p)==0)
		{
			printf("ERROR\n");
			continue;
		}
		if (pjudge(q)==0)
		{
			printf("ERROR\n");
			continue;
		}
		//pq间隔不能太小
		if (abs(p - q) <65536)
		{
			printf("ERROR\n");
			continue;
		}
		//p-1和q-1不能太平滑
		mpz_class maxg = maxgcd((p - 1), (q - 1));
		if (maxg > 20)
		{
			printf("ERROR\n");
			continue;
		}

		if (gcd(e,m)!=1)
		{
			printf("ERROR\n");
			continue;
		}
		//求d
		d = rsad(e, m);
		gmp_printf("%Zd\n", d.get_mpz_t());
	}

}

看了看别人的代码
再看下自己的
wtcl
`

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值