如何用C++实现读取证书文件中的公钥数据? 20

如何用C++实现读取证书文件中的公钥数据?... 如何用C++实现读取证书文件中的公钥数据? 展开
 我来答
kl2805
推荐于2018-04-07 · TA获得超过328个赞
知道小有建树答主
回答量:223
采纳率:0%
帮助的人:144万
展开全部
在C++下,我使用OPENSSL库生成了RSA的公私钥对与DES加密之用的会话密钥,并将这三者及加密结果写入文件以备在Java环境下使用。

在C++程序中,我使用使用公钥来加密了DES的会话密钥,然后在Java下使用私钥来解密会话密钥;在运算结果中,我未做其它方面的码制转换,即按密钥的初始格式DER编码,数学运算结果也是按DER编码来实现。

在Java程序中,我从之前所存储的几个文件中取得密钥与加密结果来做解密。我使用了BC的JCE,即bcprov-jdk14-119.jar,在使用之前,需要先安装此JCE:

假设JDK:jdk1.4\jre\
把BC包放到JRE下的ext:jdk1.4\jre\lib\ext
修改文件jdk1.4\jre\lib\security\java.security:
#
# List of providers and their preference orders (see above):
#
security.provider.1=sun.security.provider.Sun
security.provider.2=com.sun.net.ssl.internal.ssl.Provider
security.provider.3=com.sun.rsajca.Provider
security.provider.4=com.sun.crypto.provider.SunJCE
security.provider.5=sun.security.jgss.SunProvider

security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider

======================================================================

C++程序源码:

#include
#include
#include
//#define _RSA_KEY_PAIR_GENERATE_//密钥是否要生成 只需要在第一次运行时打开此宏

#define _RSA_KEY_PAIR_TOFILE_//密钥对是否要写入文件

#define MAX_RSA_KEY_LENGTH 512 //密钥的最大长度是512字节

#define PUBKEY_ENCRYPT
#define PRIKEY_DECRYPT

#pragma comment(lib, "../lib/libeay32.lib")
static const char * PUBLIC_KEY_FILE = "pubkey.key";
static const char * PRIVATE_KEY_FILE = "prikey.key";

int RsaKeyPairGen(void)
{
RSA *rsa = NULL;

#ifdef_RSA_KEY_PAIR_GENERATE_
//生成RSA密钥对:
rsa = RSA_new();
rsa = RSA_generate_key(1024, 0x10001, NULL, NULL);
#endif

//把密钥对写入文件,以后从文件里读取
#ifdef _RSA_KEY_PAIR_TOFILE_
unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0}, ucPriKey[MAX_RSA_KEY_LENGTH] = {0};
int len = i2d_RSAPublicKey(rsa,NULL);
unsigned char* pt = ucPubKey;
len = i2d_RSAPublicKey(rsa, &pt);

FILE *fpubkey = NULL;
fpubkey = fopen(PUBLIC_KEY_FILE, "wb");
if(fpubkey == NULL)
{
cout << "fopen pubkey.key failed!" << endl;
return 0x01;
}
fwrite(ucPubKey, 1, len, fpubkey);
fclose(fpubkey);

len = i2d_RSAPrivateKey(rsa,NULL);
unsigned char* pt2 = ucPriKey;
len = i2d_RSAPrivateKey(rsa,&pt2);
FILE *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "wb");
if(fprikey == NULL)
{
cout << "fopen prikey.key failed!" << endl;
return 0x02;
}
fwrite(ucPriKey, 1, len, fprikey);
fclose(fprikey);
#endif

if(rsa != NULL)
{
RSA_free(rsa);
rsa = NULL;
}
return 0;
}

//从文件里读取私钥的数据,取得RSA格式的私钥:
int GetPriKey(unsigned char *pucPriKeyData, unsigned long KeyDataLen, RSA* *priRsa)
{
unsigned char *Pt = pucPriKeyData;
*priRsa = d2i_RSAPrivateKey(NULL, &Pt, KeyDataLen);
if(priRsa == NULL)
{
cout << "priRsa == NULL!" << endl;
return 0x22;
}
return 0;
}

//取得RSA格式的公钥:
int GetPubKey(unsigned char *pucPubKeyData,unsigned long KeyDataLen, RSA* *pubRsa)
{
unsigned char *Pt = pucPubKeyData;
*pubRsa = d2i_RSAPublicKey(NULL, &Pt, KeyDataLen);
if(pubRsa == NULL)
{
cout << "pubRsa == NULL!" << endl;
return 0x31;
}
return 0;
}

//公钥加密会话密钥:
int encSessionKeybyRsaPubKey(RSA *rsa, unsigned char *ucKey, unsigned long ulKeyLen,
unsigned char *outData, unsigned long *pulOutLen)
{
return (*pulOutLen = RSA_public_encrypt(ulKeyLen, ucKey, outData, rsa, 1));
}

//私钥解密会话密钥:
int decSessionKeybyRsaPriKey(RSA *rsa, unsigned char *InData, unsigned long ulDataLen,
unsigned char *ucKey, unsigned long *pulKeyLen)
{
return (*pulKeyLen = RSA_private_decrypt(ulDataLen, InData, ucKey, rsa, 1));
}

int main(int argc, char* argv[])
{
unsigned char ucKey[8] = {0x01, 0x03, 0x99, 0x4, \
0x80, 0x65, 0x34, 0x08};
unsigned char ucEncryptedKey[512] = {0}, ucDecryptedKey[512] = {0};
unsigned long encrypted_len = 0, decrypted_len = 0;

#ifdef _RSA_KEY_PAIR_GENERATE_
RsaKeyPairGen();
#endif

//取得公钥:
unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0};

FILE *fpubkey = NULL;
fpubkey = fopen(PUBLIC_KEY_FILE, "rb");
if(fpubkey == NULL)
{
cout << "fopen pubkey.key failed!" << endl;
return 0x03;
}
fseek(fpubkey, 0, SEEK_END);
int len_PK = ftell(fpubkey);
fseek(fpubkey, 0, SEEK_SET);
fread(ucPubKey, 1, len_PK, fpubkey);
fclose(fpubkey);

#ifdef PUBKEY_ENCRYPT
RSA *pRsaPubKey = NULL;
pRsaPubKey = RSA_new();

GetPubKey(ucPubKey, len_PK, &pRsaPubKey);
//公钥加密:
encSessionKeybyRsaPubKey(pRsaPubKey, ucKey, sizeof(ucKey), ucEncryptedKey, &encrypted_len);
//write to file:
FILE *fp = NULL;
fp = fopen("ucKey.data", "wb");
fwrite(ucEncryptedKey, 1, encrypted_len, fp);
fclose(fp);

if(pRsaPubKey != NULL)
{
RSA_free(pRsaPubKey); pRsaPubKey = NULL;
}
#endif

//取得私钥:
unsigned char ucPriKey[MAX_RSA_KEY_LENGTH] = {0};

FILE *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "rb");
if(fprikey == NULL)
{
cout << "fopen prikey.key failed!" << endl;
return 0x02;
}
fseek(fprikey, 0, SEEK_END);
int len_SK = ftell(fprikey);
fseek(fprikey, 0, SEEK_SET);
fread(ucPriKey, 1, len_SK, fprikey);
fclose(fprikey);

#ifdef PRIKEY_DECRYPT
RSA *pRsaPriKey = NULL;
pRsaPriKey = RSA_new();

GetPriKey(ucPriKey, len_SK, &pRsaPriKey);
//私钥解密:
FILE *fp1 = NULL;
fp1 = fopen("ucKey.data", "rb");
int len = ftell(fp1);
fseek(fp1, 0, SEEK_SET);
fread(ucPriKey, 1, len_SK, fp1);
fclose(fp1);
decSessionKeybyRsaPriKey(pRsaPriKey, ucEncryptedKey, encrypted_len, ucDecryptedKey, &decrypted_len);
if(pRsaPriKey != NULL)
{
RSA_free(pRsaPriKey); pRsaPriKey = NULL;
}

//数据对比:
if(0 == memcmp(ucKey, ucDecryptedKey, decrypted_len))
{
cout << "OK!" << endl;
}
else
{
cout << "FAILED!" << endl;
}
#endif

return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式