用OPENSSL实现如下:
unsigned int d2i_RSACrefPrivateKey(unsigned char *pKey,unsigned long ulKeyLen,RSAPRIVATEKEYBLOB *pRefPrivateKey)
{
unsigned int rv = 0;
unsigned long ulBnLen;
RSA *rsa = d2i_RSAPrivateKey(NULL, (const unsigned char **)&pKey, ulKeyLen);
if (rsa == NULL) { rv = 1; goto END; }
memset(pRefPrivateKey, 0x00, sizeof(RSAPRIVATEKEYBLOB));
pRefPrivateKey->AlgID = SGD_RSA;
pRefPrivateKey->BitLen = BN_num_bits(rsa->n);
ulBnLen = (BN_num_bits(rsa->e) + 7 ) / 8;
BN_bn2bin(rsa->e, pRefPrivateKey->PublicExponent+4-ulBnLen);
ulBnLen = (BN_num_bits(rsa->n) + 7 ) / 8;
BN_bn2bin(rsa->n, pRefPrivateKey->Modulus + MAX_RSA_MODULUS_LEN - ulBnLen);
ulBnLen = (BN_num_bits(rsa->d) + 7 ) / 8;
BN_bn2bin(rsa->d, pRefPrivateKey->PrivateExponent + MAX_RSA_MODULUS_LEN - ulBnLen);
ulBnLen = (BN_num_bits(rsa->p) + 7 ) / 8;
BN_bn2bin(rsa->p, pRefPrivateKey->Prime1 + RSAref_MAX_PLEN - ulBnLen);
ulBnLen = (BN_num_bits(rsa->q) + 7 ) / 8;
BN_bn2bin(rsa->q, pRefPrivateKey->Prime2+ RSAref_MAX_PLEN - ulBnLen);
ulBnLen = (BN_num_bits(rsa->dmp1) + 7 ) / 8;
BN_bn2bin(rsa->dmp1, pRefPrivateKey->Prime1Exponent + RSAref_MAX_PLEN - ulBnLen);
ulBnLen = (BN_num_bits(rsa->dmq1) + 7 ) / 8;
BN_bn2bin(rsa->dmq1, pRefPrivateKey->Prime2Exponent + RSAref_MAX_PLEN - ulBnLen);
ulBnLen = (BN_num_bits(rsa->iqmp) + 7 ) / 8;
BN_bn2bin(rsa->iqmp, pRefPrivateKey->Coefficient + RSAref_MAX_PLEN - ulBnLen);
END:
if (rsa) RSA_free(rsa);
return rv;
}