简体   繁体   English

如何将RSA公钥和私钥读入单个RSA结构?

[英]How to read RSA public and private keys into single RSA struct?

What I'm trying to do is generate random RSA keys and then store them before my program terminates. 我正在尝试做的是生成随机RSA密钥,然后在我的程序终止之前存储它们。 This part is working just fine using RSA_generate_key, PEM_write_bio_RSAPrivateKey and PEM_write_bio_RSA_PUBKEY. 这部分使用RSA_generate_key,PEM_write_bio_RSAPrivateKey和PEM_write_bio_RSA_PUBKEY正常工作。 I can also encrypt/decrypt just find using the RSA structure returned by RSA_generate_key. 我也可以使用RSA_generate_key返回的RSA结构加密/解密。

However, my problem comes when my program restarts and I want to read back in the keys that I stored previously. 但是,当我的程序重新启动并且我想要读回我之前存储的密钥时,我的问题出现了。 I can use PEM_read_bio_RSAPrivateKey and PEM_read_bio_RSA_PUBKEY to pull the keys in, but I need to get them into the same RSA structure, similar to how they are stored by RSA_generate_key. 我可以使用PEM_read_bio_RSAPrivateKey和PEM_read_bio_RSA_PUBKEY来输入密钥,但是我需要将它们放入相同的RSA结构中,类似于RSA_generate_key的存储方式。

My code is shown below. 我的代码如下所示。 I have the keys stored in memory along with a small header that tell me how large the keys are. 我将密钥存储在内存中,并带有一个小标题,告诉我密钥的大小。 The private key start right after the header and the public key is stored right after the private key. 私钥在标题之后立即开始,公钥紧接在私钥之后。

privateKey = (uint8_t *) ( buffer + rsaStruct->hdrSize );
publicKey = (uint8_t *) ( privateKey + rsaStruct->privateKeyLength );

bioPrivate = BIO_new_mem_buf( (void *) privateKey, rsaStruct->privateKeyLength );
bioPublic = BIO_new_mem_buf( (void *) publicKey, rsaStruct->publicKeyLength );

bioPrivate = BIO_new_mem_buf( (void *) privateKey, rsaStruct->privateKeyLength + rsaStruct->publicKeyLength );
if( bioPrivate == NULL || bioPublic == NULL ) {
    fprintf( stderr, "%s: BIO_new_mem_buf failed!\n", __FUNCTION__ );
    return ECE_RSA_ERROR_BIO_CREATION_FAILED;
}

PEM_read_bio_RSAPrivateKey( bioPrivate, &keyPair, NULL, NULL );
PEM_read_bio_RSA_PUBKEY( bioPublic, &keyPair, NULL, NULL );

BIO_free( bioPrivate );
BIO_free( bioPublic );

If I try to just send in the same RSA structure, it doesn't seem to work. 如果我尝试发送相同的RSA结构,它似乎不起作用。 I'm able to encrypt just fine, but my decryption fails. 我能够加密很好,但我的解密失败了。 This could likely be due to the fact that the public key is the last key retrieve and the one used for encryption. 这可能是由于公钥是最后一个密钥检索和用于加密的密钥。 If the second call over-writes the address of my RSA struct, I would end up with an RSA structure that has nothing but the public key. 如果第二个调用覆盖了我的RSA结构的地址,我最终会得到一个只有公钥的RSA结构。

Anyway, if anyone could tell me how to get both the public and private key into the same RSA structure, that would be great! 无论如何,如果有人能告诉我如何将公钥和私钥都放入相同的RSA结构中,那就太棒了!

Comparing to RSA private key, public key additionaly contains only the public exponent. 与RSA私钥相比,公钥另外仅包含公共指数。 So just copy it from public key to private key structure, and everything should work. 所以只需将其从公钥复制到私钥结构,一切都应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM