hust 密码学课设 rsa参数d

hust is 专栏收录该内容
2 篇文章 0 订阅

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 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
gmp_printf("%Zd\n", d.get_mpz_t());
}

}


wtcl
`

• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

09-12
10-15
12-30 1万+
07-03 5162
11-03 1183