这个问题很可能是重复的,如果可以,我很抱歉,但是无法找到解决方法。
给出:
RSA* rsa = RSA_generate_key(2048, RSA_3, NULL, NULL);
我想要类似的东西:
const char* pubKey = pubKeyFromRSA(rsa);
const char* privKey = privKeyFromRSA(rsa);
//and then convert it back
RSA* newRSA = RSAFromPrivKey(privKey);
我该怎么做?谢谢
答案 0 :(得分:0)
感谢Michael Dorgan向我指出了正确的方向。我最终拥有了这两个功能:
const char* keyFromRSA(RSA* rsa, bool isPrivate)
{
BIO *bio = BIO_new(BIO_s_mem());
if (isPrivate)
{
PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, NULL);
}
else
{
PEM_write_bio_RSA_PUBKEY(bio, rsa);
}
const int keylen = BIO_pending(bio);
char* key = (char *)calloc(keylen+1, 1);
BIO_read(bio, key, keylen);
BIO_free_all(bio);
return key;
}
RSA* rsaFromPrivateKey(const char* aKey)
{
RSA* rsa = NULL;
BIO *bio = BIO_new_mem_buf(aKey, strlen(aKey));
PEM_read_bio_RSAPrivateKey(bio, &rsa, 0, 0);
BIO_free_all(bio);
return rsa;
}