我想知道如何在Azure Key Vault中使用SECP256K1密钥,同时在区块链上创建帐户也需要密钥。
需要实现的目标
尝试#1-生成重要的vaul和导入
我已经在Key Vault外部生成了SECP256K1密钥对。结果,我可以将私钥和公钥用作字符串。 我的想法是在以下代码段(借助ECParameter来自Microsoft.Azure.KeyVault.WebKey.ECParameters命名空间)的帮助下,借助Azure SDK for.NET将私钥导入到Key Vault:
//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,但是对Key Vault的调用失败,并显示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"
}
尝试#2-在键值和导出范围内生成
即使我能够在Key Vault中生成SECP256K1 EC密钥,我也找不到如何导出其公共密钥部分。
编辑11/23/2018
Azure Key Vault上的GetKey方法返回包含X和Y坐标的JsonWebKey。
问题
编辑11/29/2018
谢谢。
关于, 扬
PS:我最初在Azure Key Vault blog上发布了此问题,但这里的受众可能更多。回答后,我将链接两个来源,
答案 0 :(得分:0)
为了解决类似的情况,我使用 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);
您需要设置应用注册并在 Key Vault 中创建访问策略,以便能够按照此处所述进行导入 https://www.c-sharpcorner.com/blogs/fetching-secrets-from-key-vault-in-net-console-app