简体   繁体   English

使用 azure keyvault 服务获取 RSA256 私钥和公钥

[英]Get RSA256 private and public key using azure keyvault service

I am trying my hands on Azure Key Vault cloud service.我正在尝试使用 Azure Key Vault 云服务。 I followed a few msdn articles to create a key in azure key vault.我按照几篇 msdn 文章在 azure 密钥库中创建了一个密钥。

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.此代码工作正常,它在 azure 密钥库中创建一个密钥。 I want to get the public and private keys from the key generated using the above code.我想从使用上述代码生成的密钥中获取公钥和私钥。 The KeyBundle response has a JsonWebKey property. KeyBundle 响应具有 JsonWebKey 属性。 I inspected this property and found that on N and E has values.我检查了这个属性,发现在 N 和 E 上有值。

{
  "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.我想使用 RSA 256 私钥来签署 JWT。

There is no way to download private part of the key once it was imported/generated.一旦导入/生成密钥,就无法下载密钥的私有部分。 You can refer this article as an example你可以参考这篇文章作为例子

Secondly, speaking of public keys, only the public key is available to the system.其次,说到公钥,系统只有公钥可用。 The API call to GetKeyAsync doesn't return private key data.对 GetKeyAsync 的 API 调用不返回私钥数据。 This is why the DecryptAsync wrapper method does use the Key Vault API for decryption.这就是 DecryptAsync 包装器方法使用 Key Vault API 进行解密的原因。 In other words, private keys never leave the vault, which is one reason to use Key Vault for decryption instead of bringing private keys into the process.换句话说,私钥永远不会离开保管库,这是使用 Key Vault 进行解密而不是将私钥带入过程的原因之一。

As an option, you can generate key-pair by your own and save a private key作为一个选项,您可以自己生成密钥对并保存私钥

using Azure.Identity;
using Azure.Security.KeyVault.Keys;

var vaultUri = new Uri("https://{kv-name}.vault.azure.net/");
var keyClient = new KeyClient(vaultUri, new DefaultAzureCredential());

// Generate key
var generatedKey = RSA.Create();

// You can save a private key
// var privateKey = generatedKey.ExportRSAPrivateKey();

var webKey = new JsonWebKey(generatedKey, includePrivateParameters: true);
var response = await keyClient.ImportKeyAsync(new ImportKeyOptions("name", webKey));

But if you want to store both public and private keys in Azure I would suggest you to store them as a certificate.但是,如果您想将公钥和私钥都存储在 Azure 中,我建议您将它们存储为证书。 In that case it would be possible to access certificate with public and private keys directly from Azure在这种情况下,可以直接从 Azure 使用公钥和私钥访问证书

using Azure.Identity;
using Azure.Security.KeyVault.Certificates;

var certClient = new CertificateClient(vaultUri, new DefaultAzureCredential());


X509Certificate2 cert = ...
...
var importCertOptions = new ImportCertificateOptions("name", cert.RawData);
var response = await certClient.ImportCertificateAsync(importCertOptions);

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

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