我想使用openSSL库为ECDSA算法生成公钥/私钥对。问题是在生成密钥对之后,在生成的密钥中有一些额外的字符,完全没有意义。例如,这是我的代码的结果:
-----BEGIN EC PARAMETERS-----
MIH3AgEBMCwGByqGSM49AQECIQD/////AAAAAQAAAAAAAAAAAAAAAP//////////
/////zBbBCD/////AAAAAQAAAAAAAAAAAAAAAP///////////////AQgWsY12Ko6
k+ez671VdpiGvGUdBrDMU7D2O848PifSYEsDFQDEnTYIhucEk2pmeOETnSa3gZ9+
kARBBGsX0fLhLEJH+Lzm5WOkQPJ3A32BLeszoPShOUXYmMKWT+NC4v4af5uO5+tK
fA+eFivOM1drMV7Oy7ZAaDe/UfUCIQD/////AAAAAP//////////vOb6racXnoTz
ucrC/GMlUQIBAQ==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MIIBaAIBAQQgRcbK9JeWXghYJe0QHAaHDRrqmgX7Gp9QQrB820lHVDyggfowgfcC
AQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////
MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57Pr
vVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEE
axfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54W
K84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8
YyVRAgEBoUQDQgAEiXKUR9yDAkmUUMj/i6LDxt0+HuMJRZWSbnq2fRZfHXgIkk9w
VFusGaX4keEJf9tOWUokEVNg1RMGfMjG9C8GUQ==
-----END EC PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAA
AAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA////
///////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSd
NgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5
RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA
//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABIlylEfcgwJJlFDI/4uiw8bd
Ph7jCUWVkm56tn0WXx14CJJPcFRbrBml+JHhCX/bTllKJBFTYNUTBnzIxvQvBlE=
-----END PUBLIC KEY-----
我使用BIO作为I / O,这是我的代码:
EC_KEY *ecKey = EC_KEY_new();
if (ecKey == NULL) {
// error happened in initializing new key pair
return ERR_CODE_ECDSA_EC_KEY_NEW_EXCEPTION;
}
EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
if (group == NULL) {
// error happened in generating new group
return ERR_CODE_ECDSA_EC_GROUP_NEW_BY_CURVE_NAME_EXCEPTION;
}
EC_KEY_set_group(ecKey, group);
if (!EC_KEY_generate_key(ecKey)) {
// error happened in generating new key pair
return ERR_CODE_ECDSA_EC_KEY_GENERATE_KEY;
}
BIO* memBioECKey = BIO_new(BIO_s_mem());
if (memBioECKey == NULL) {
return ERR_CODE_BIO_NEW_EXCEPTION;
}
int iRet = PEM_write_bio_ECPKParameters(memBioECKey, group);
if (!iRet) {
return ERR_CODE_BIO_NEW_EXCEPTION;
}
iRet = PEM_write_bio_ECPrivateKey(memBioECKey, ecKey, NULL, NULL, 0, NULL, pchPassPhrase);
if (!iRet) {
return ERR_CODE_BIO_NEW_EXCEPTION;
}
iRet = PEM_write_bio_EC_PUBKEY(memBioECKey, ecKey);
if (!iRet) {
return ERR_CODE_BIO_NEW_EXCEPTION;
}
char* pchPriKey = NULL;
pchPriKey = new char[4096];
int iData = BIO_read(memBioECKey, pchPriKey, 4096);
strPrivateKey.assign(pchPriKey);
iPrivateKeyLen = strPrivateKey.length();
if (pchPriKey != NULL) {
delete []pchPriKey;
pchPriKey = NULL;
}
BIO_free_all(memBioECKey);
EC_KEY_free(ecKey);
ERR_print_errors_fp(stderr);
// Return NO_ERROR to indicate NO ERROR happens
return NO_ERROR;
知道问题出在哪里?我是否错误地使用了BIO API,或者序列不正确?
提前致谢