尝试解密通过 getkeyasync 检索到的密钥时,我收到错误。我错过了什么?

When attempting to decrypt a key retrieved through getkeyasync I recieve errors. What am I missing?

我在 KeyVault 中存储了一个 RSA 密钥(不是秘密,是密钥)。使用 .net KeyVaultClient 我可以检索密钥。

但是,当我尝试解密密钥以便它可以在我们的一个应用程序中使用时,我收到一个错误消息(我尝试了几种解密密钥的方法,每种方法都在下面突出显示,并且每种方法都会生成不同的错误消息/异常)。

我试了好几种方法。我将概述每一个,希望不会泄露我们的任何敏感信息。

KeyBundle keyBundle = await keyClient.GetKeyAsync(fullUri);

哪里

(i) keyClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken))); (ii) fullUri 是 "https://[mycompany].vault.azure.net/keys/[keyidentifier]"

我收到一个 KeyBundle 对象,在该 KeyBundle 对象中我有 JsonWebKey,其中包含作为字节数组的模数和指数。

获得这些值后,我尝试使用以下方法解密密钥。

  1. 使用keyClient.DecryptAsync(keyBundle.KeyIdentifier.ToString(), JsonWebEncryptionAlgorithm.RSA15, keyBundel.Key.N).GetAwaiter().GetResult();方法。

我尝试更改上述调用中的算法并收到相同的错误 (KeyVaultErrorException):BadRequest,

此外,我尝试用 keyBundle.Key.Kid 替换 keyBundle.KeyIdentifier.ToString() (这似乎具有相同的值)。

  1. 使用 KeyVaultClientExtensions.DecryptAsync(keyClient, keyBundle.Key.Kid, "RSAOAEP", keyBundle.Key.N);

我还收到 KeyVaultErrorException - BadRequest。

  1. 使用 HttpClient PostAsync 事件(下面的代码)我尝试直接调用 Rest APi 并收到以下异常:

{"error":{"code":"BadParameter","message":"The parameter is incorrect.\r\n"}}

其余代码Api调用:

keyClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken));

string fullUri = $"{baseUrl}keys/{keyIdentifier}";

KeyBundle keyBundle = await keyClient.GetKeyAsync(fullUri);

JsonWebKey key = keyBundle.Key;

string decryptUri = $"{keyBundle.Key.Kid}/decrypt?api-version=7.0";

                HttpResponseMessage response = null;
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    try
                    {
                        var uri = new Uri(decryptUri, UriKind.Absolute);
                        string encryptedText = Convert.ToBase64String(key.N); //Encoding.UTF8.GetString(key.N, 0, key.N.Length);
                        DecryptionRequestInformation requestInformation = new DecryptionRequestInformation();
                        requestInformation.alg = "RSA1_5";
                        requestInformation.value = encryptedText;

                        var stringContent = new StringContent(JsonConvert.SerializeObject(requestInformation), Encoding.UTF8, "application/json");

                        response = await client.PostAsync(uri, stringContent);
                    }
                    catch (Exception ex)
                    {

                        throw ex;
                    }
                }

                string encryptionResult = string.Empty;
                if (response != null)
                {
                    encryptionResult = response.Content.ReadAsStringAsync().Result;
                }

AccessToken 是在 KeyVaultClient 回调中调用 GetAccessToken 的结果

我希望能够在我们的一个应用程序中使用解密的密钥,但我无法解密它。

有人可以解释一下我遗漏了什么吗?我有一种感觉,从 GetKeyAsync 调用返回的字节数组不是我想的那样(RSA 模数值 (key.N) 是加密密钥吗?)或者我没有对字节进行编码(key.N) 正确传递给 DecryptAsync 方法。

不幸的是,许多在线示例都引用了 KeyVault 机密而不是密钥,我找不到任何在处理 Keyvault 密钥时似乎有效的示例。

请问我想念她什么?

提前致谢

朱利安

根据您的问题描述和评论,我认为您可能以错误的方式使用了 Azure Key Vault。

您只能获取密钥的public部分,可用于生成public密钥。

私钥将存储在 Key Vault 中,无法检索。

因此,您可以使用 KeyVaultClient 来:

  1. 用私钥签署摘要

  2. 使用 public 密钥验证签名

  3. 使用 public 密钥加密数据

  4. 用私钥解密数据

只能使用 KeyVaultClient 执行签名和解密操作。验证和加密操作可以在本地执行,因为您可以使用密钥的 public 部分的模数和指数生成 public 密钥。

如果你确实想获取私钥并在你的应用程序中使用它,我认为你必须使用secret。 Secret 正是您想要的东西:您可以将它安全地存储在 Azure 上,并在需要时检索它。