?? testgetpubkey.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 + -