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

Save RSA public and private keys in XML (w3c) format via OpenSSL (c++)

本文关键字:保存 RSA 公钥 私钥 格式 XML OpenSSL c++ 通过 w3c      更新时间:2023-10-16

我想使用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>

谢谢!

//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>"));