Forum Discussion

markand911's avatar
markand911
Copper Contributor
Jan 04, 2022

Get RSA256 private and public key using azure keyvault service

I am trying my hands on Azure Key Vault cloud service. I followed a few msdn articles to create a key in azure key vault.

Here is my code

 

public async Task<RSA256Key> GenerateRSAKey(string keyName)
        {
            using (KeyVaultClient client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken))) //GetToken returns access token to connect to Azure key vault
            {
                var newKeyParams = new NewKeyParameters()
                {
                    Kty = "RSA",
                    CurveName= "P-256",
                    KeySize = 2048
                };

                KeyBundle rsa = await client.CreateKeyAsync(_config.VaultUrl, keyName, newKeyParams);
        }
    }

 

 

This code works fine, it creates a key in azure keyvault. I want to get the public and private keys from the key generated using the above code. The KeyBundle response has a JsonWebKey property. I inspected this property and found that on N and E has values.

 

{
  "kid": "https://xxxx.vault.azure.net/keys/yyyy/zzzz",
  "kty": "RSA",
  "n": "wt1SiRuybjkoVwgbUJgHJY9W1WFDMHOzhKx3ewISCINWFgiH5RHOhGDqoIfFVuwGMk0mmnNXdVCFFrATYUPT0EhXqCv_9IDXSh9WW1VvvsZBp0nW1v8e80Mz_nDZ1DVgC2KY8G97JVyfomm6nZRcBVkklimmZEDl_oPpFg68rfnEz4qou-4DNMoF2k9U95xXZfusrFpP5IJnHaMqsCQTozIWu65sWv3I5sW3zRmx93nQWAbf0_FEf70SQ8qgDtP8IVKS7xd05epQkbPsPtI8KwW4tVUsmP7EJYaMxCvT-Y_bpdliwEWxIMTp6cwo3l7AWvb8YyAhPC1Z02Cliweo5Q",
  "e": "AQAB"
}

I want to use the RSA 256 private key to sign a JWT.

Resources