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

📄 testgetpubkey.cpp

📁 UDP对QQ来说太重要了。 UDP和TCP是同一个层上的协议
💻 CPP
字号:
// testGetPubKey.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include "..\..\hmdbapi\hmdbgetuserpublickey.h"
#include "..\..\hmdbapi\hmdbsetuserpublickey.h"

int cmpkey(char* sUID,char* sPath)
{
	
	FILE* fp = NULL;
	int nLen = 0;
	unsigned char KeyBuf[4096] = {0};
	unsigned char* p;
	p = KeyBuf;

	RSA* rsa;
	rsa = RSA_new();
	if (rsa == NULL)
	{
		printf("\nOoph, Error on prepare alloc memory.\n");
		return -1;
	}

	printf("I'm going to open datafile %s ...",sPath);

	fp = fopen(sPath ,"rb");
	if( fp == NULL )
	{
		RSA_free(rsa);
		printf("Can not open file %s\n",sPath);
		return -1;
	}

	puts("ok.");
	printf("reading key from file %s ...",sPath);

	if (PEM_read_RSA_PUBKEY(fp, &rsa, NULL, NULL)==NULL)
	{
		printf("Can not read public key from file %s \n",sPath);
		RSA_free(rsa);
		return -1;
	}

	puts("ok.");

	nLen = i2d_RSAPublicKey(rsa, &p);
	RSA_free(rsa);
	fclose(fp);

	printf("Registing key to database ...");
	if(HMDBSetUserPublicKey(sUID, KeyBuf, nLen, KeyBuf, nLen ) != HMASP_OK)
	{
		//return HMASP_ERROR_DBSETUSERPUBLICKEY;
		printf("Error on register user %s's public key.\n",sUID);
		return -1;

	}
	printf("ok.\nThe user %s's public key registed successfully.\n",sUID);
	char buf[2048];
	int nlen=2048;
	char buf2[2048];
	int n=2048;

	if (HMDBGetUserPublicKey(sUID,buf,&nlen,buf2,&n)!=HMASP_OK)
	{
		puts("Read Key fail.");
		return -1;
	}
	puts("Read Key ok.");

	if (memcmp(KeyBuf,buf,nlen)==0)
		puts("Compare Key....OK.");
	else
		puts("Compare Key....FAIL.");

	return 0;
}
int main(int argc, char* argv[])
{
	printf("start test...\n");
	cmpkey("testkey512","testkey512pub.pem");
	cmpkey("testkey1024","testkey1024pub.pem");
	cmpkey("testkey2048","testkey2048pub.pem");
	cmpkey("testkey4096","testkey4096pub.pem");
	cmpkey("testkey8192","testkey8192pub.pem");

	return 0;
}

⌨️ 快捷键说明

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