繁体   English   中英

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

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

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

暂无
暂无

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

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