ailon's DevBlog: Development related stuff in my life

Get RSAKeyValue from Base64 Encoded Public Key

4/27/2010 6:54:55 PM

Note: I don’t know much about encryption and certificates. This is not a tutorial or good explanation of things. It’s just a note to myself for future reference.

I needed to verify data sent to my ASP.NET app signed by 3rd party using their private RSA key. They’ve sent me their public key in base64 encoded form. You’ve probably seen it many times. It looks something like this:

-----BEGIN CERTIFICATE-----
MIIECTCCA3KgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBujELMAkGA1UEBhMCTFQx
EDAOBgNVBAgTB1ZpbG5pdXMxEDAOBgNVBAcTB1ZpbG5pdXMxHjAcBgNVBAoTFVVB

-----END CERTIFICATE-----

Problem is (as far as I understand) to verify data using RSACryptoServiceProvider you either need the key installed on your system or have it in XML Signature format. I’ve searched high and low for a way to convert that encoded key to XML equivalent but couldn’t find a simple way. Most likely I wasn’t searching for the right thing and I admit I’m not an expert in this area. I guess this can be done some other way but after combining some breadcrumbs I found here and there I came up with only 3 lines needed to to get <RSAKeyValue> representation of the key:

   1: byte[] binaryCertData = Convert.FromBase64String(mimeKey);
   2: X509Certificate2 cert = new X509Certificate2(binaryCertData);
   3: string xmlKey = cert.PublicKey.Key.ToXmlString(false);

And that was it. Does the trick.

Update: I’ve created an online tool to convert Base64 encoded public key to XML Signature format.

Tags: ,

blog comments powered by Disqus
Copyright © 2003 - 2018 Alan Mendelevich
Powered by BlogEngine.NET 2.5.0.6