RSA加/解密算法--miracl大数库实现

6 篇文章 0 订阅
1 篇文章 0 订阅

发一个自己简单的小code C语言实现(0929 更新一下,之前理解不对的地方修改)
在这里插入图片描述


如果有任何错误和疑问,请邮箱联系leapoo@126.com


int main()
{  

	big p,q,n,fn,e,d,minus_1,gcd,one;
	big msg,enc_msg,dec_msg;
	miracl *mip;
	long seed;
	int width,p_width,q_width;
	int p_top,q_top;
	int p_bot,q_bot;
	seed = srand();

	mip = mirsys(2048, 2);

	
	p  = mirvar(0);
	q  = mirvar(0);
	n  = mirvar(0);
	fn = mirvar(0);
	e = mirvar(65537);
	d = mirvar(0);
	minus_1 = mirvar(-1);
	gcd = mirvar(0);
	one = mirvar(1);
	msg = mirvar(0);
	enc_msg = mirvar(0);
	dec_msg = mirvar(0);

	printf("Please enter the RSA width ");
	scanf_s("%d", &width);

	do {
		printf("*********************************\n");
		seed = brand();
		bigbits(width / 2, p);
		printf("The randomize generated number p is ");
		otnum(p, stdout);
		printf("Find the Next Prime number after p\n");
		nxprime(p, p);
		printf("The Next Prime number after p is ");
		otnum(p, stdout);
		p_width = numdig(p);
		if (p_width != (width / 2)) {
			printf("The p Width %d != Required Width %d \n",p_width,width/2 );
			continue;
		}
		else
		{
			printf("The p Width %d == Required Width %d \n", p_width, width/2);
			
		}
			
		p_top = getdig(p, width / 2);
		p_bot = getdig(p, 0);
		if ((p_top == 1) && (p_bot == 1))
		{
			printf("VALID PRIME p!! The p_width is %d, The p_top is %d, the p_bot is %d \n", p_width, p_top, p_bot);
		}
		else {
			printf("INVALID PRIME p!! The p_width is %d, The p_top is %d, the p_bot is %d \n", p_width, p_top, p_bot);
			continue;
		}

		printf("*********************************\n");
		break;
	} while (1);


	do {
		seed = brand();
		bigbits(width / 2, q);
		printf("The randomize generated number q is  ");
		otnum(q, stdout);
		printf("Find the Next Prime number after q\n");
		nxprime(q, q);
		printf("The Next Prime number after q is ");
		otnum(q, stdout);
		if (p == q)
		{
			printf("Invalid Q Prime beacause p:%d==q:%d \n",p,q);
			continue;
		}

		q_width = numdig(q);
		if (q_width != (width / 2)) {
			printf("The q Width %d != Required Width %d \n", q_width, width / 2);
			continue;
		}
		else
		{
			printf("The q Width %d == Required Width %d \n", q_width, width / 2);

		}

		q_top = getdig(q, width / 2);
		q_bot = getdig(q, 0);
		if ((q_top == 1) && (q_bot == 1))
		{
			printf("VALID PRIME q!! The q_width is %d, The q_top is %d, the q_bot is %d \n", q_width, q_top, q_bot);
		}
		else {
			printf("INVALID PRIME q!! The q_width is %d, The q_top is %d, the q_bot is %d \n", q_width, q_top, q_bot);
			continue;
		}
		printf("*********************************\n");
		break;
	} while (1);

	printf("*********************************\n");

	printf("Select p =");
	otnum(p, stdout);
	printf("Select q =");
	otnum(q, stdout);
	printf("*********************************\n\n");
	printf("*********************************\n");
	multiply(p,q,n);
	add(p, minus_1, p);
	add(q, minus_1, q);
	multiply(p, q, fn);
	printf("n =");
	otnum(n, stdout);
	printf("fn =");
	otnum(fn, stdout);
	printf("*********************************\n\n");

	printf("*********************************\n");
	printf("Find a public key e which smaller than fn and co-prime with fn\n");

	/*
	do {
		bigrand(fn, e);
	
		//egcd(e, fn, 1);
		//printf("gcd: ");
		//otnum(gcd, stdout);		
		if(egcd(e, fn, one) ==1)
		{
			printf("FIND!!!The co-prime with fn is e: ");
			otnum(e, stdout);
			break;
		}
		printf("FIND next\n ");
		otnum(e, stdout);
	} while (1);
	*/
	printf("*********************************\n");
	printf("Find a private key d\n");

	xgcd(e, fn,d,d,d);
	printf("The private key is d: ");
	otnum(d, stdout);
	printf("*********************************\n\n");
	printf("The public  key is e: ");
	otnum(e, stdout);
	printf("The private key is d: ");
	otnum(d, stdout);
	printf("*********************************\n");

	printf("Random Generated the message smaller than N\n");
	bigrand(fn,msg);
	printf("The message is : ");
	otnum(msg, stdout);
	printf("Now encrpted with public key\n");
	powmod(msg, e, n, enc_msg);
	printf("The encrpted message is : ");
	otnum(enc_msg, stdout);

	printf("*********************************\n\n");
	printf("Decrpt the encrpted message \n");
	powmod(enc_msg, d, n, dec_msg);
	printf("The de-encrpted message is : ");
	otnum(dec_msg, stdout);

	if (mr_compare(dec_msg,msg)==0)
	{
		printf("RSA Algorithm successful!\n");

	}


    return 0;
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值