发一个自己简单的小code C语言实现
RSA加/解密算法--miracl大数库实现

#include <stdio.h>
#ifdef MR_STATIC
#include <string.h>
#endif

#include "miracl.h"


#pragma comment (linker,"/NODEFAULTLIB:libc.lib")

#define mr_min(a,b) ((a) < (b)? (a) : (b))
#define NP 2


int main()
{  

	big p,q,n,fn,e,d,minus_1,gcd,one;
	big msg,enc_msg,dec_msg;
	miracl *mip;
	long seed;
	int p_width,q_width;

	seed = srand();

	mip = mirsys(100, 10);

	
	p  = mirvar(0);
	q  = mirvar(0);
	n  = mirvar(0);
	fn = mirvar(0);
	e = mirvar(0);
	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 first prime number width ");
	scanf_s("%d", &p_width);

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

	irand(seed);
	bigbits(p_width,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);
	printf("*********************************\n\n");

	printf("Please enter the second prime number width ");
	scanf_s("%d", &q_width);
	printf("*********************************\n");

	seed = srand();
	bigbits(q_width, q);

	printf("The randomize generated number q is  ");
	otnum(q, stdout);
	printf("Find the Next Prime number after q\n");
	nxprime(q, q);
	if(p==q)
	{
		nxprime(q, q);
	}
	printf("The Next Prime number after q is ");
	otnum(q, stdout);
	printf("*********************************\n");
	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;
}




如果有任何错误和疑问,请邮箱联系[email protected]



相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-01
  • 2022-01-13
猜你喜欢
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
  • 2021-04-09
相关资源
相似解决方案