0

I have started working on Windows CNG APIs. I am generating RSA keypair using


BCRYPT_ALG_HANDLE alg;
BCryptOpenAlgorithmProvider(out alg, BCRYPT_RSA_ALGORITHM, null, 0);

BCRYPT_KEY_HANDLE key;
BCryptGenerateKeyPair(alg, out key, 2048, 0);

BCryptFinalizeKeyPair(key, 0); //finalize the key so we can use it

DWORD publicKeySize;
BCryptExportKey(key, 0, BCRYPT_RSAPUBLIC_BLOB, null, 0, &publicKeySize, 0);

PUCHAR publicKey= new UCHAR[publicKeySize];
BCryptExportKey(key, 0, BCRYPT_RSAPUBLIC_BLOB, publicKey, publicKeySize, &publicKeySize, 0);

The publicKey buffer receives a BCRYPT_RSAKEY_BLOB structure and I am looking for ways to convert this public key (similarly private key) into PKCS#8 PEM string format like -

-----BEGIN PUBLIC KEY-----
MIIBITANBgkqhkiG9w0BAQEFAAOCAQ4AMIIBCQKCAQBqPgVATKFjTkdJfYBrlBvG
baD/LT8Q8WpHJf/FElNo2gL5rriaq/35suCIZxR7N+gdfvr2jCkwKhIDjhlvaUD/
CisvViDJXUEwzsPCPII6Qh5Q75YT3k+qvro06QOtWAo03Sa43lNY74zMgUQrgoog
C+5X1Cszzef40JRUqvctEBshIr02oazk5Mfgm2KoiVUjWl47OeUltL6Fy7jWIkv2
cFo4UabwtvG9gYOovi6svDYs0Z32pVKIOYM0tJ5rzeJd4zibe3JqzlNGWFVok0xg
FRJlVS1zAA73ugFJqr2lzJIaTIQfrPReSSIYpm7i0p/+FEzf9RzWwHuhGN/RR+El
AgMBAAE=
-----END PUBLIC KEY-----

The reasons I want to convert key pair to string are: -

  1. I want to save this key pair (in encrypted format) on some server for roaming purpose.

  2. I want to use this key-pair on other platforms using their native crypto library like Apple crypto library, WebCrypto API etc.

Please guide me if there is any standard way to do that, or if there is already a standard library on top of CNG that handles this.

0 Answers0