如何将以下公钥转换为der编码的公钥? 谢谢,我可能看到了 i2d_PUBKEY,但我不知道该怎么做。
#include <openssl/evp.h>
#include <openssl/x509.h>
uint8_t *pubkey = NULL;
size_t len = 0;
get_pubkey_function(&pubkey, &len);
printf(pubkey);
/* this step will print out
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE+GmnZ1UZqKfa81Yh0CG6huQPhYON
Q0W7AE4whuX+oUOg+BewYSOLUwj5zfwRB3i7T07HoltPfAOYeHI57Oe7jA==
-----END PUBLIC KEY-----
*/
EVP_PKEY *public_key = NULL;
BIO *bio = NULL;
bio = BIO_new_mem_buf((void *)pubkey, -1);
PEM_read_bio_PUBKEY(bio, &public_key, NULL, NULL);
EXPECT_TRUE(public_key);
我说得对吗?
很晚了,但以防万一有人需要答案:
EVP_PKEY *key = EVP_PKEY_new();
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
EVP_PKEY_keygen_init(ctx); // initialize context
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 4096); //making 4096 bits makes it more secure
EVP_PKEY_keygen(ctx, &key); //add context to the key
现在假设
char[64] path = "/path/to/your/file.der"
FILE* key_file = fopen(path, 'w');
if(!i2d_PUBKEY_fp(key_file, key)){ //Writes the key to the file stream.
fprintf(stderr, "BLOCKCHAIN ERROR! Failed to write public key for account %s\n", username);
fclose(key_file);
EVP_PKEY_CTX_free(ctx);
return -1;
}
fclose(key_file);
EVP_PKEY_CTX_free(ctx);