Our customer has their public RSA key stored in a certificate.
We need this key hardcoded in our WinRT app, so we can encrypt client-side. However, we're having issues importing the key into an instance of the CryptographicKey class.
We're using the ImportPublicKey on the RSAProvider:
rsaProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
key = rsaProvider.ImportPublicKey(publicKeyBuffer);
We've tried loading several things into the publicKeyBuffer: The certificate, the public key exported from the certificate in several formats.
How do we load their public key?
Two things:
For those banging his head as how you can use a public key stored in a certificate in a WinRT app, let me ease your pain: You can't, at least not directly.
The
AsymmetricKeyAlgorithmProvider.ImportPublicKey
function takes an IBuffer and a CryptographicPublicKeyBlobType, the keyBlob (IBuffer) parameter it's the public key of the certificate, not the full certificate, only its public key.But you can't get the public key of the certificate with out parsing it first, here is where the problem lies, there is no way to parse the certificate on WinRT, given that the most used class for this task, X509Certificate, is not available, nor is its namespace, and the facilities for certificates are only to be used on web services connections.
The only way to workaround this will be by implementing a certificate parser, or porting such functionality from an open source project, like Bouncy Castle. So, if you know one, please leave it in the comments.
By the way, to export the public key from the certificate (in plain .NET) in a format that can be used in a WinRT app, use this:
Then in the WinRT app use this:
Note that i encoded the public key in base 64 first, but you may use raw binary data instead (the CryptographicBuffer class has more methods for this purpose).
I found this article in the MSDN Forum very helpful. Carlos Lopez postet some code to get the Public Key out of a Base64 encoded Certificate.
http://social.msdn.microsoft.com/Forums/en-US/17e1467a-2de7-47d2-8d8c-130518eaac68/how-to-use-a-x509-certificate-not-a-pfx-to-verify-a-signature
Here the code: