⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getneed.h

📁 RSA VC++实现
💻 H
字号:
#include "bn.h"
#include <conio.h>
#include <stdio.h>
#define RSA_N_SIZE 1024//符合标准
#define RSA_P_SIZE (((RSA_N_SIZE) + 1) >> 1)//一半
#define RSA_Q_SIZE ((RSA_N_SIZE) - (RSA_P_SIZE))
#define RSA_EN_SIZE RSA_N_SIZE
#define RSA_D_SIZE RSA_EN_SIZE
#define BYTENUM(a) (((a) & 7)? (((a) >> 3) + 1): ((a) >> 3))//>>3即除以8,((a) & 7)? (((a) >> 3) + 1)确保零头也参与

typedef int S32;
typedef unsigned int U32;
typedef unsigned char U8;
//U8 temp_e[3];


int
random_number(unsigned char *dst, int len, void *dat);
U8 p[BYTENUM(RSA_P_SIZE)+1];
U8 q[BYTENUM(RSA_Q_SIZE)+1];
U8 n[BYTENUM(RSA_N_SIZE)+1];
U8 e[3]={1, 0, 1};//65537
//U8 e[2]={1,1};//其实是257
U8 d[BYTENUM(RSA_D_SIZE)+1];
U8 en[BYTENUM(RSA_EN_SIZE)+1];
void do_need()
{
	S32 err,t;
	mp_int _p, _q, _n, _e, _d, _en;
    if((err = mp_init_multi(&_p, &_q, &_n, &_e, &_d, &_en, NULL)) != MP_OKAY)
	{
		goto LIF;
	}
	//获得素数P的审判次数
    t=mp_prime_rabin_miller_trials(RSA_P_SIZE);
	//生成大素数P

    if((err = mp_prime_random_ex(&_p, t, RSA_P_SIZE, LTM_PRIME_2MSB_ON, random_number, NULL)) != MP_OKAY)
	{
		goto LBL;
	}

	dump_mp_int_to_char (&_p, BYTENUM(RSA_P_SIZE), (char *)p);
	p[BYTENUM(RSA_P_SIZE)]='\0';

	/*printf("p:\n");
	for (i = 0; i < BYTENUM(RSA_P_SIZE); ++i)
	{
		printf("0x%02X, ", p[i]);
		//cout<<p[i]<<" ";
		if (7 == (i & 7))
		{
			printf("\n");
		}
	}*/ 
	
	//获得素数Q的审判次数
	t=mp_prime_rabin_miller_trials(RSA_Q_SIZE);
	//生成大素数P

    if((err = mp_prime_random_ex(&_q, t, RSA_Q_SIZE, LTM_PRIME_2MSB_ON, random_number, NULL)) != MP_OKAY)
	{
		goto LBL;
	}

	dump_mp_int_to_char (&_q, BYTENUM(RSA_P_SIZE), (char *)q);
	q[BYTENUM(RSA_P_SIZE)]='\0';
	/*printf("\nq:\n");
	for (i = 0; i < BYTENUM(RSA_P_SIZE); ++i)
	{
		printf("0x%02X, ", q[i]);
		if (7 == (i & 7))
		{
			printf("\n");
		}
	} */ 


	//计算n=p*q
    if ((err = mp_mul(&_p, &_q, &_n)) != MP_OKAY) 
	{
		goto LBL;
	}
	dump_mp_int_to_char (&_n, BYTENUM(RSA_N_SIZE), (char *)n);
	n[BYTENUM(RSA_N_SIZE)]='\0';

	/*printf("\nn:\n");
	for (i = 0; i < BYTENUM(RSA_N_SIZE); ++i)
	{
		printf("0x%02X, ", n[i]);
		if (7 == (i & 7))
		{
			printf("\n");
		}
	} */ 
	//计算p-1
    if ((err = mp_sub_d (&_p, 1, &_p)) != MP_OKAY) 
	{
		goto LBL;
	}
	//计算q-1
    if ((err = mp_sub_d (&_q, 1, &_q)) != MP_OKAY) 
	{
		goto LBL;
	}
	//计算(p-1)*(q-1)
    if ((err = mp_mul (&_p, &_q, &_en)) != MP_OKAY) 
	{
		goto LBL;
	}
	dump_mp_int_to_char (&_en, BYTENUM(RSA_EN_SIZE), (char *)en);
	en[BYTENUM(RSA_EN_SIZE)]='\0';
/*	printf("\nen:\n");
	for (i = 0; i < BYTENUM(RSA_EN_SIZE); ++i)
	{
		printf("0x%02X, ", en[i]);
		if (7 == (i & 7))
		{
			printf("\n");
		}
	}*/
//62716
    if ((err = mp_read_unsigned_bin(&_e, e, 3)) != MP_OKAY) //这个函数的问题原来是哈哈
	{
		goto LBL;
	}


/*	dump_mp_int_to_char (&_e, 2, (char *)temp_e);
	printf("\ne:\n");
	printf("0x%02X,",temp_e[0]);
	printf("0x%02X,",temp_e[1]);
	printf("\n");*/
	//计算d=e^-1 mod (p-1)*(q-1)
    if ((err = mp_invmod(&_e, &_en, &_d)) != MP_OKAY) 
	{
		goto LBL;
	}
	dump_mp_int_to_char (&_d, BYTENUM(RSA_D_SIZE), (char *)d);
	d[BYTENUM(RSA_D_SIZE)]='\0';
//	printf("\nd:\n");
	/*@$#^$^%^%&^*/
	//temp_num=strlen((char *)d);
//	printf("%d,",temp_num);
//	printf("\n");
/*	for (i = 0; i < BYTENUM(RSA_D_SIZE); ++i)
	{
		printf("0x%02X, ", d[i]);
		if (7 == (i & 7))
		{
			printf("\n");
		}
	}
	//
/*	for (i = 0; i < BYTENUM(RSA_D_SIZE); ++i)
	{
		printf("%c, ", d[i]);
		if (7 == (i & 7))
		{
			printf("\n");
		}
	}*/


LBL:mp_clear_multi(&_p, &_q, &_n, &_e, &_d, &_en, NULL);
LIF:
	{
	}
	//printf("Press any key to continue");
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -