Import Public RSA Key From Certificate RRS feed

  • Question

  • 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 load their public key?

    Monday, September 17, 2012 12:27 PM


  • If you're using the X509Certificate2 class in .NET you can export the certificate this way:
    X509Certificate2 Cert;

    byte[] PublicKey = Cert.GetPublicKey();
    string strPubKey = Convert.ToBase64String(PublicKey);

    In WinRT you import the public key this way:
    AsymmetricKeyAlgorithmProvider RSAProvider = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
    IBuffer iPubKey = CryptographicBuffer.DecodeFromBase64String(strPubKey);
    CryptographicKey RSAKey = RSAProvider.ImportPublicKey(iPubKey, CryptographicPublicKeyBlobType.Pkcs1RsaPublicKey);

    Carlos Lopez - Microsoft Escalation Engineer
    If my reply answers your question, please mark this post as answered.

    Thursday, September 27, 2012 5:45 AM