gpt4 book ai didi

c# - 将 key 导入到 Azure Key Vault (C#)

转载 作者:行者123 更新时间:2023-12-02 23:34:26 25 4
gpt4 key购买 nike

我在使用 SDK 时将 EC key 导入到 Azure Key Vault 时遇到问题。

代码:

var ecKey = Nethereum.Signer.EthECKey.GenerateKey();
var privateKey = ecKey.GetPrivateKeyAsBytes().ToHex();

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).Normalize();
var privateKeyX = ecPoint.Normalize().XCoord.ToBigInteger()
.ToByteArrayUnsigned();
var privateKeyY = ecPoint.Normalize().YCoord.ToBigInteger()
.ToByteArrayUnsigned();

var jwk = new JsonWebKey(new [] { KeyOperation.Sign, KeyOperation.Verify, });
jwk.CurveName = KeyCurveName.P256K;
jwk.KeyType = KeyType.Ec;
jwk.D = privateKeyBytes;

jwk.X = privateKeyX;
jwk.Y = privateKeyY;

await keyClient.ImportKeyAsync(keyName, jwk, cancellationToken);`

来自 Azure 的错误,这对于准确了解问题所在没有太大帮助:

Azure.RequestFailedException: EC key is not valid - cannot instantiate crypto service.Status: 400 (Bad Request)ErrorCode: BadParameter

注意:在此示例中,我生成了一个新 key ,但通常它是我们现有的私钥。

你们知道这里出了什么问题吗?

最佳答案

已通过 Topaco's answer 解决.

我做错的是我对私钥进行编码,然后转换为字节数组。

正确代码:

var ecKey = Nethereum.Signer.EthECKey.GenerateKey();
var privateKey = ecKey.GetPrivateKeyAsBytes();
var parameters = SecNamedCurves.GetByName("secp256k1");
var privateKeyInt =
new Org.BouncyCastle.Math.BigInteger(+1, privateKey);
var ecPoint = parameters.G.Multiply(privateKeyInt).Normalize();
var privateKeyX = ecPoint.Normalize().XCoord.ToBigInteger()
.ToByteArrayUnsigned();
var privateKeyY = ecPoint.Normalize().YCoord.ToBigInteger()
.ToByteArrayUnsigned();

var jwk = new JsonWebKey(new [] { KeyOperation.Sign, KeyOperation.Verify, })
{
CurveName = KeyCurveName.P256K,
KeyType = KeyType.Ec,
D = privateKey,
X = privateKeyX,
Y = privateKeyY,
};

await keyClient.ImportKeyAsync(keyName, jwk, cancellationToken);

关于c# - 将 key 导入到 Azure Key Vault (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75854557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com