使用LibTomCrypt实现RSA加密解密算法,公钥、私钥分离

//使用本代码需要先下载LibTomMath和LibTomCrypt库
//编译LibTomMath、LibTomCrypt后生成lib文件
//在本工程的包含目录中添加LibTomCrypt的头文件目录
//链接器中添加LibTomCrypt、LibTomMath的Lib文件

#include 
#include 
#define LTM_DESC
#include 
#include 

#define PTSIZE 87
#define CPSIZE 1024
#define BUFFERSIZE 1024

//按照16进制方式打印字符串
void strprint(const char * name,unsigned char *msg,int len);
//对比两个字符数组如果完全匹配返回0,否则返回负数
int msgcmp(unsigned char *msg1,unsigned long l1,unsigned char *msg2,unsigned long l2);

int main(void)
{	
	char c;
	int prng_idx,hash_idx,err;
	unsigned char pt[PTSIZE],pt1[CPSIZE],cp[CPSIZE];
	unsigned char out[BUFFERSIZE];
	unsigned long l1,l2;
	rsa_key key;
	rsa_key pubkey,prikey;
	unsigned char pubkeybuffer[BUFFERSIZE], prikeybuffer[BUFFERSIZE];
	unsigned long pubkeybuffersize, prikeybuffersize;
	int keysize = 0;
	int stat;
	int n;
	LARGE_INTEGER t1, t2, tc;

	QueryPerformanceFrequency(&tc);

	/*********解析算法准备**********/
	//绑定数学库
	ltc_mp = ltm_desc;
	//注册随机数生成器
	register_prng(&sprng_desc);
	prng_idx = find_prng("sprng");
	//注册hash运算库
	register_hash(&sha1_desc);
	hash_idx = find_hash("sha1");	
	
	/*********生成key**********/
	//生成一个新的key
	err = rsa_make_key(NULL, prng_idx, 1024 / 8, 257, &key);
	if (err != CRYPT_OK)
	{
		printf("rsa_make_key err:%s\n",error_to_string(err));
		goto MAIN_FUNC_RET;
	}
	//打印Key的长度
	keysize = rsa_get_size(&key);
	if (keysize < 0)
	{
		printf("rsa_get_size err:%s\n", error_to_string(err));
		goto MAIN_FUNC_RET;
	}
	printf("keysize = %d\n",keysize);
	//将key导出成为私密秘钥形势的字符串,做单独保存
	err = rsa_export(prikeybuffer, &prikeybuffersize, PK_PRIVATE, &key);
	if (err != CRYPT_OK)
	{
		printf("rsa_export err:%s\n", error_to_string(err));
		goto MAIN_FUNC_RET;
	}
	strprint("prikey", prikeybuffer, prikeybuffersize);
	//将key导出成为公共秘钥形势的字符串,公开加密使用
	err = rsa_export(pubkeybuffer, &pubkeybuffersize, PK_PUBLIC, &key);
	if (err != CRYPT_OK)
	{
		printf("rsa_export err:%s\n", error_to_string(err));
		goto MAIN_FUNC_RET;
	}
	strprint("pubkey", pubkeybuffer, pubkeybuffersize);
	//从公共秘钥字符串中导入成rsa_key加密使用
	err = rsa_import(pubkeybuffer, pubkeybuffersize, &pubkey);
	if (err != CRYPT_OK)
	{
		printf("rsa_import err:%s\n", error_to_string(err));
		goto MAIN_FUNC_RET;
	}
	n = rsa_get_size(&pubkey);
	printf("pubkeysize = %d\n",n);
	//从私密秘钥字符串中导入成为rsa_key解密使用
	err = rsa_import(prikeybuffer, prikeybuffersize, &prikey);
	if (err != CRYPT_OK)
	{
		printf("rsa_import err:%s\n", error_to_string(err));
		goto MAIN_FUNC_RET;
	}
	n = rsa_get_size(&prikey);
	if (n < 0)
	{
		printf("rsa_get_size err:%s\n", error_to_string(err));
		goto MAIN_FUNC_RET;
	}
	printf("prikeysize = %d\n",n);
	system("pause");

	/*****************加密和解密验证******************/
	memset(pt, 0, PTSIZE);
	srand(time(0));
	//随机生成一个字符数组作为待加密原文
	for (int i = 0; i < PTSIZE; i++)
	{
		pt[i] = rand() % 255;
	}
	strprint("pt", pt, PTSIZE);

	//多次尝试加密解密,验证结果,计算运算时间
	for (int i = 0; i < 100; i++)
	{
		memset(cp,0,CPSIZE);
		QueryPerformanceCounter(&t1);
		rsa_encrypt_key_ex(pt, PTSIZE, cp, &l1, (const unsigned char*)"abcde", 5, NULL, prng_idx, hash_idx, LTC_PKCS_1_V1_5, &pubkey);
		QueryPerformanceCounter(&t2);
		strprint("encrypt text",cp,l1);
		printf("加密运算时间:%f\n", (t2.QuadPart - t1.QuadPart)*1000.0 / tc.QuadPart);
		//尝试使用公开秘钥进行解密失败
		QueryPerformanceCounter(&t1);
		//rsa_decrypt_key_ex(cp, l1, pt1, &l2, (const unsigned char*)"abcde", 5, hash_idx, LTC_PKCS_1_V1_5,&stat, &pubkey);
		rsa_decrypt_key_ex(cp, l1, pt1, &l2, (const unsigned char*)"abcde", 5, hash_idx, LTC_PKCS_1_V1_5, &stat, &prikey);
		QueryPerformanceCounter(&t2);
		printf("stat = %d\n",stat);
		strprint("decrypt text",pt,l2);
		printf("解密运算时间:%f\n", (t2.QuadPart - t1.QuadPart)*1.0 / tc.QuadPart);
		printf("解密后对比结果:%s\n",msgcmp(pt,PTSIZE,pt1,l2) == 0 ? "正确" : "错误");
		system("pause");
	}

MAIN_FUNC_RET:
	rsa_free(&key);
	rsa_free(&prikey);
	rsa_free(&pubkey);
	system("pause");
	return 0;
}

void strprint(const char * name, unsigned char *msg, int len)
{
	printf("name = %s,len = %d\n", name, len);
	for (int i = 0; i < len; i++)
	{
		if (i != 0 && i % 32 == 0)
		{
			printf("\n");
		}
		printf("0x%02x,", msg[i]);
	}
	printf("\n");
}

int msgcmp(unsigned char *msg1, unsigned long l1, unsigned char *msg2, unsigned long l2)
{
	if (l1 != l2)
		return -1;
	for (int i = 0; i < l1; i++)
	{
		if (msg1[i] != msg2[i])
		{
			return -2;
		}
	}
	return 0;
}

https://download.csdn.net/download/haozi596/10308358

你可能感兴趣的:(加密)