2

I've been working on signing and verifying files with ECDSA, and have got all my in-house tests working fine. However, I can't complete testing with my client, because they cannot import my public key.

I'm using CngKey.Export(CngKeyBlobFormat.EccPublicBlob), but it appears the format of the resulting byte[] is proprietary (and therefore useless).

How might I export this in a more-common format (such as PEM or DER), that is readable by non-.NET users, without over-reliance on third-partly libraries (e.g. I don't want to have to rewrite everything to use BouncyCastle)?

For example, how do I get from:

const string keyName = "My Key";

var provider = CngProvider.MicrosoftSoftwareKeyStorageProvider;
var cngOptions = CngKeyOpenOptions.UserKey;

if (CngKey.Exists(keyName, provider, cngOptions))
{
    using (var key = CngKey.Open(keyName, provider, cngOptions))
    {
        byte[] keyData = key.Export(CngKeyBlobFormat.EccPublicBlob);            
    }
}

...to:

-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAElkRqN7wIGwvHP0HCMuBZPS/L5ZDp4mBG
A5qssu1FumNtCEHQbjbUa46AvpXRL290Mr2iabTv9Z/SwDciihKZI6BrzSb1x060
WDgXslbTnzzh+lxne2AZtPPPzorZOiI7
-----END PUBLIC KEY-----

...?

jimbobmcgee
  • 1,561
  • 11
  • 34
  • Try using [Convert.ToBase64String](https://msdn.microsoft.com/en-us/library/dhx0d524%28v=vs.110%29.aspx) – Icemanind May 29 '15 at 16:47
  • @Icemanind - No, I know how to make Base64, but the Base64 representation of a proprietary `byte[]` is still proprietary. The problem is that the `byte[]` generated by `CngKey.Export` is not actually the public key bytes... – jimbobmcgee May 29 '15 at 16:51
  • Ah I misunderstood. Look at [this SO question](http://stackoverflow.com/questions/24251336/import-a-public-key-from-somewhere-else-to-cngkey) instead. – Icemanind May 29 '15 at 16:54

0 Answers0