我想知道如何在Azure中使用SECP256K1密钥,同时也需要在块链上创建帐户。
必须实现什么
尝试#1 -生成外部密钥库并导入
我已经在密钥库外生成了SECP256K1密钥对。因此,我将私钥和公钥作为字符串使用。我的想法是在Azure的帮助下,将私钥导入到密钥库( 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变量视图复制,可能是缩短了):
{
"kty": "EC",
"crv": "P-256K",
"x": "vSUHj6deEhPI6QeILgfgf2I7VTgmiDon_5nsss560OA",
"y": "DK8DnzEOv57arN6f4Wou-vXkty7uje0n2xTHgGAehp8",
"d": "NUpoaEEzWTFVYXBKNWZuRWZNUkVxZkpKY29LWGdMcHpFUnVNQ2E2Wjd0YkNhY2NpQ3N5"
}
虽然JsonWebKeyVerifier不返回错误,对key.IsValid()的调用返回为true,但对Key的调用在HTTP400BAD请求中失败。我打开了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"
}
尝试#2 -在密钥库中生成和导出
即使我能够在key Vault中生成SECP256K1 EC密钥,我也没有找到如何导出它的公钥部分。
编辑11/23/2018
Azure上的GetKey方法返回包含X和Y坐标的JsonWebKey。
问题
编辑11/29/2018
提前谢谢。
你好,简
PS:我最初在Azure Key Vault博客上发布了这个问题,但是这里可能有更多的观众。我会把这两个来源联系起来,
发布于 2021-04-17 11:47:02
为了解决类似的情况,我有一个使用生成的openssh格式密钥对,我可以使用下面的代码将其导入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-sharpcorner.com/blogs/fetching-secrets-from-key-vault-in-net-console-app
https://stackoverflow.com/questions/53534454
复制