How to use a PRIVATE key with RSACryptoServiceProvider class? RRS feed

  • Question

  • Hi,

    I have a .pvk file that I want to use.  I created it using makecert.

    I can load the .cer (public) part when I want to use it (see code), but I have yet to find or work out a way of loading the pvk.

    there are a LOT of web pages that say 'JUST create the 'blah' object from a file'.  well none of them work!

    I have this example code for loading CER files (from a genius called Ji Zhou) - I need similar to be able to load PVK files - the problem being that amongst all the 'documentation' for security and encryption a lot of it doesn;t quite work when tested (Ji Jhou's stuff being the exception.)

    Anyhow - a PVK equivalent of the CER example below is what I am desparate for.

    CspParameters cspp = new CspParameters(PROVIDER_RSA_FULL);
    cspp.KeyContainerName = OstroPUBLICKeyStoreName;
    cspp.Flags = CspProviderFlags.UseNonExportableKey | CspProviderFlags.UseMachineKeyStore;
    // Creates/Accesses a key pair in the key container identified by the cspp.
    rsa = new RSACryptoServiceProvider(cspp);
    string fname = @"C:\\MASTER.cer";
    X509Certificate2 duhcert = new X509Certificate2(fname);
    //Get an instance of RSAParameters from ExportParameters function.
    RSAParameters RSAKeyInfo = rsa.ExportParameters(false);
    //Set RSAKeyInfo to the public key values. 
    RSAKeyInfo.Modulus = duhcert.GetPublicKey();
    //Import key parameters into RSA.


    Thursday, August 23, 2012 10:43 AM


All replies