1
我想使用openssl加載pkcs12並將其編碼爲base64。我不需要解析它只是加載它,然後將其編碼到base64,所以當我解碼並將內容放入文件時,我將再次具有pkcs12結構。如何使用openssl加載pkcs12並將其轉換爲char *?
char* loadPkcs12(const char* path) {
FILE *fp;
PKCS12 *p12;
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
if (!(fp = fopen(path, "rb"))) {
fprintf(stderr, "Error opening file %s\n", path);
exit(1);
}
p12 = d2i_PKCS12_fp(fp, NULL);
fclose(fp);
if (!p12) {
fprintf(stderr, "Error reading PKCS#12 file\n");
ERR_print_errors_fp(stderr);
exit(1);
}
//here I am trying to encode it
BIO* bio;
bio = BIO_new(BIO_s_mem());
i2d_PKCS12_bio(bio, p12);
BUF_MEM *bufferPtr;
BIO_get_mem_ptr(bio, &bufferPtr);
printf(toBase64(bufferPtr->data));
PKCS12_free(p12);
return 0;
}
char* toBase64(char* str) {
BIO *bio, *b64;
BUF_MEM *bufferPtr;
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line
BIO_write(bio, str, strlen(str));
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bufferPtr);
BIO_set_close(bio, BIO_NOCLOSE);
BIO_free_all(bio);
char* output;
output = (*bufferPtr).data;
return output;
}
我知道對Base64的編碼正在工作,將PKCS12轉換爲char *的問題。我的輸出現在只有大約60個字符長,而且應該更長。有沒有辦法如何將PKCS12轉換爲char *緩衝區? 非常感謝您的建議。
謝謝!它的工作,也感謝NULL終止符和BIO_push提示 – user3737648