Search code examples
c#azureazure-keyvaultpublic-keyecdsa

EC Public key from public key bytes extracted from azure keyvault


how to get public key from bytes? for example, I have public key (generated with EC algorithm, curve "secp256r1") and its encoded bytes on java, How can I create public key from these bytes in c#?


Solution

  • There are different methods to fetch the key from the vault. I used the following for the same.

    var secret = keyVaultClient.GetSecretAsync(vaultAddress, "Honeywellpkiofflinetokenprime256v1cert").GetAwaiter().GetResult();
     X509Certificate2Collection exportedCertCollection = new X509Certificate2Collection();
                exportedCertCollection.Import(Convert.FromBase64String(secret.Value));
    
    
                X509Certificate2 certFromSecret = exportedCertCollection.Cast<X509Certificate2>().Single(s => s.HasPrivateKey);
                var publickeybyte = certFromSecret.GetPublicKey();
                var publicekeybyte = certFromSecret.GetPublicKeyString();
                var PublicKeyCNG= certFromSecret.GetECDsaPublicKey();
    
    
                var privateECDsa = LoadPrivateKey(FromHexString(privateKey));
                var publiccECDsa = LoadPublicKey(FromHexString(publicKey));
    

    now you can use the same key to verify your signed information.