当前位置: 首页 > 知识库问答 >
问题:

如何在Azure密钥库中使用椭圆曲线SECP256K1密钥,当该密钥也被区块链使用时

於和志
2023-03-14

我想知道如何在Azure密钥库中使用SECP256K1密钥,同时该密钥也是在区块链上创建帐户所必需的。

必须实现的目标

    null

我已经在密钥库外生成了SECP256K1密钥对。因此,我有私钥和公钥作为字符串可用。我的想法是在Azure SDK for.NET wia的帮助下将私钥导入密钥库,下面的代码(使用的ECParameters来自Microsoft.Azure.keyvault.webkey.ECParameters命名空间):

  //this part of code is taken from https://www.scottbrady91.com/C-Sharp/JWT-Signing-using-ECDSA-in-dotnet-Core
  var privateKeyBytes = Encoding.UTF8.GetBytes(privateKey);
  var privateKeyInt =
    new Org.BouncyCastle.Math.BigInteger(+1, privateKeyBytes);
  var parameters = SecNamedCurves.GetByName("secp256k1");
  var ecPoint = parameters.G.Multiply(privateKeyInt);
  var privateKeyX = ecPoint.Normalize().XCoord.ToBigInteger()
    .ToByteArrayUnsigned();
  var privateKeyY = ecPoint.Normalize().YCoord.ToBigInteger()
    .ToByteArrayUnsigned();

  //the following part is my code
  var ecParameters = new ECParameters();
  ecParameters.Curve = "P-256K";
  ecParameters.X = privateKeyX;
  ecParameters.Y = privateKeyY;
  ecParameters.D = privateKeyBytes;

  var key = new JsonWebKey(ecParameters);
  JsonWebKeyVerifier.Options options =
    JsonWebKeyVerifier.Options.DenyIncompatibleOperations
    | JsonWebKeyVerifier.Options.DenyExtraneousFields;
  string error = (string) null;

  JsonWebKeyVerifier.VerifyByKeyType(key, options, ref error);

  await client.ImportKeyWithHttpMessagesAsync(vaultBaseUri, name, key);

导入的结果键如下所示(从Visual Studio变量视图复制,可能被缩短了):

  {
    "kty": "EC",
    "crv": "P-256K",
    "x": "vSUHj6deEhPI6QeILgfgf2I7VTgmiDon_5nsss560OA",
    "y": "DK8DnzEOv57arN6f4Wou-vXkty7uje0n2xTHgGAehp8",
    "d": "NUpoaEEzWTFVYXBKNWZuRWZNUkVxZkpKY29LWGdMcHpFUnVNQ2E2Wjd0YkNhY2NpQ3N5"
  }

尽管JsonWebKeyVerifier没有返回错误,对Key.isvalid()的调用也返回true,但由于HTTP 400请求错误,对密钥存储库的调用失败了。我打开了Key Vault实例上的日志,下面的日志出现在那里(id、用户名、IP地址和GUID被故意更改,我不确定什么是机密的;我可以根据需要提供它们):

{
  "time": "2018-11-28T16:28:05.2034585Z",
  "category": "AuditEvent",
  "operationName": "KeyImport",
  "resultType": "Success",
  "resultDescription": "EC key is not valid - cannot instantiate crypto service.",
  "correlationId": "5682a894-0150-484f-a398-6922efed4458",
  "callerIpAddress": "XX.XX.XXX.XXX",
  "identity": {
    "claim": {
      "http://schemas.microsoft.com/identity/claims/objectidentifier": "00000000-0000-0000-0000-000000000000",
      "appid": "00000000-0000-0000-0000-000000000000",
      "http://schemas.microsoft.com/identity/claims/scope": "user_impersonation",
      "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn": "xxx.xxxxxx@xxxxxxxx.com",
      "ipaddr": "XX.XX.XXX.XXX",
      "http://schemas.microsoft.com/claims/authnmethodsreferences": "pwd"
    }
  },
  "properties": {
    "id": "https://xxxxxxx.vault.azure.net/keys/testJK1",
    "clientInfo": "FxVersion/4.6.27019.06 OSName/Windows OSVersion/Microsoft.Windows.10.0.17134. Microsoft.Azure.KeyVault.KeyVaultClient/3.0.2.0",
    "httpStatusCode": 400,
    "requestUri": "https://xxxxxxxx.vault.azure.net/keys/testJK1?api-version=7.0",
    "isAccessPolicyMatch": true,
    "keyProperties": {
      "type": "EC"
    }
  },
  "resourceId": "/SUBSCRIPTIONS/00000000-0000-0000-0000-000000000000/RESOURCEGROUPS/XXXXXX-RG/PROVIDERS/MICROSOFT.KEYVAULT/VAULTS/XXXXXXX",
  "operationVersion": "7.0",
  "resultSignature": "Bad Request",
  "durationMs": "259"
}

编辑11/23/2018

Azure Key Vault上的GetKey方法返回包含X和Y坐标的JsonWebKey。

问题

    null

共有1个答案

顾均
2023-03-14

为了解决类似的场景,我使用Putty key Generator生成了一个openssh pem格式的密钥对,我可以使用以下代码将其导入到KeyVault中。

 var client = new KeyClient(new Uri("https://myKeyVault.vault.azure.net"), new ClientSecretCredential("tenantid", "clientid", "clientSecret"));

 var key = ECDsa.Create();
 key.ImportFromPem(@"-----BEGIN EC PARAMETERS-----
     ....
     -----END EC PARAMETERS-----
     -----BEGIN EC PRIVATE KEY-----
     ...
     -----END EC PRIVATE KEY-----
     ");

 JsonWebKey jwk = new JsonWebKey(key, true);
 await client.ImportKeyAsync("MyKey", jwk);

您需要在密钥存储库中设置应用程序注册并创建访问策略,以便能够进行导入,如下所述https://www.c-sharpconer.com/blogs/fetching-secrets-from-key-vault-in-net-console-app

 类似资料:
  • 我正在处理一些ECC加密问题。 目标是用C或bash编写程序,它将以由128个十六进制字符组成的散列作为输入(例如:8A9A35145C4EA5260DF9972C804FE2D3F9F3D7A2AC01A6BEB21C82BB30957B3952273AC9166B90C120737A925780F84A1D2359E7AA05201C674D29746FCA07),并将从

  • 我在.NET Core2.0中创建了一个RSA加密/解密服务,目前我使用密钥库机密来保存RSA密钥。但据我所知,我可以用Key Vault密钥实现这一点,但目前还不能实现,因为KV Keys不支持2048长RSA密钥的加密/解密...这真让我摸不着头脑。 那么,我是否可以使用带有Azure Key Vault密钥的2048密钥来实现RSA加密/解密呢?

  • 使用AKV管理存储帐户:https://docs.microsoft.com/en-us/Azure/key-vault/key-vault-overview-storage-keys-powershell#manage-storage-account-keys 使用Azure Automation进行密钥循环:https://docs.microsoft.com/en-us/Azure/key-

  • 我们希望利用Azure Key vault将我们的敏感密钥存储在Azure Key vault中,并利用存储的敏感密钥在发送到服务之前加密我们的字符串/纯文本数据。服务将再次与Azure Key vault交互,检索敏感密钥,用于解密客户端发送的加密字符串。任何一个可以提供一些关于如何使用azure密钥库以上的场景。