通过OpenSSL(c ++)以XML(w3c)格式保存RSA公钥和私钥

时间:2012-04-24 07:46:54

标签: c++ xml openssl rsa

我想使用RSA公钥和私钥的XML文件格式。 现在我找到了如何以PEM和二进制(DER)格式保存这些密钥(例如,PEM_write_RSAPrivateKey())

我有一个带有xml格式RSA密钥的字符串,我需要将它们加载到EVP_PKEY或RSA OpenSSL结构中。

XML格式是这样的:

<RSAKeyPair>
  <Modulus>...</Modulus>
  <Exponent>...</Exponent>
  <P>...</P>
  <Q>...</Q>
  <DP>...</DP>
  <DQ>...</DQ>
  <InverseQ>
    ...
  </InverseQ>
  <D>...</D>
</RSAKeyPair>

谢谢!

1 个答案:

答案 0 :(得分:2)

//just a code for demo,not for actually use  
int len;  
RSA *rsa;  
BIO *bio;
unsigned char *data; 
bio = BIO_new(BIO_s_meme()); 
BIO *b64;  
b64 = BIO_new(BIO_f_base64());  
BIO_write(bio, "<RSAKeyPair>\n",strlen("<RSAKeyPair>\n"));  
//write Modulus
len=BN_num_bytes(rsa->n);  
data=(unsigned char *)OPENSSL_malloc(len);  
if(data) {  
  BIO_write(bio,"  <Modulus>",strlen("  <Modulus>"));
  BN_bn2bin(rsa->n,data);  
  bio = BIO_push(b64, bio);  
  BIO_write(bio, data, len);  
  (void)BIO_flush(bio);  
  BIO_pop(bio);  
  BIO_reset(b64);  
  BIO_write(bio,"</Modulus>",strlen("</Modulus>"));  
}  
//write Exp  
...  
//write the bignum in rsa structure you want  
BIO_write(bio, "</RSAKeyPair>\n",strlen("</RSAKeyPair>"));