|
security
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
PFX Import - Can Import but can't read back PrivateKeyI am using .net 2.0 on windows xp (sp2). I wrote following code to import the certificate into my store name in local machine location: X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); myStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite); X509Certificate2 certificate = new X509Certificate2(); certificate.Import(fileName, mypassword, X509KeyStorageFlags.MachineKeySet); myStore.Add(certificate); myStore.Close(); The issuer certificate (for the above certificate) is already in the local machine store. After wards I write following code to dump the contents of the certificate (in total different process): X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); myStore.Open(OpenFlags.ReadOnly); foreach (X509Certificate2 cert in myStore.Certificates) { Console.WriteLine("Issuer: {0}; Subject={1}", cert.Issuer, cert.Subject); if (!cert.HasPrivateKey) { Console.WriteLine("Certificate has no private key."); } RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider; if (rsa != null) { ... } } The above code throws exception on the line that contains cert.PrivateKey property. It is not cast exception ("Keyset does not exist") since I am using as operator. If I delete the certificate and import it through windows Certificates mmc, my read code executes fine. In both cases, I can check that the imported certificate has the private key. Is there some thing wrong in my import code??? Thanks, Raghu/.. Did you check the details of both certificates and compare (i.e. the one you
imported programaticaly versus the one you manually imported by MMC) ? Not sure about this, but the cert.PrivateKey might require that the private key be exportable. Check and compare the Exportable flag that is default for the X509KeyStorageFlags. Might not be able to instantiate RSACryptoSeviceProvider that way if key is not exportable .. just a guess. - Mitch Gallant Show quoteHide quote "Raghu" <RaghuNoSpam> wrote in message news:edApVrsMGHA.2416@TK2MSFTNGP15.phx.gbl... > Hi, > > I am using .net 2.0 on windows xp (sp2). I wrote following code to import the certificate into my > store name in local machine location: > > X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); > myStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite); > > X509Certificate2 certificate = new X509Certificate2(); > certificate.Import(fileName, mypassword, X509KeyStorageFlags.MachineKeySet); > myStore.Add(certificate); > myStore.Close(); > > The issuer certificate (for the above certificate) is already in the local machine store. > > After wards I write following code to dump the contents of the certificate (in total different > process): > > X509Store myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); > myStore.Open(OpenFlags.ReadOnly); > > > foreach (X509Certificate2 cert in myStore.Certificates) > { > Console.WriteLine("Issuer: {0}; Subject={1}", cert.Issuer, cert.Subject); > if (!cert.HasPrivateKey) > { > Console.WriteLine("Certificate has no private key."); > } > > RSACryptoServiceProvider rsa = cert.PrivateKey as RSACryptoServiceProvider; > if (rsa != null) > { > ... > } > } > > The above code throws exception on the line that contains cert.PrivateKey property. It is not cast > exception ("Keyset does not exist") since I am using as operator. > > If I delete the certificate and import it through windows Certificates mmc, my read code executes > fine. In both cases, I can check that the imported certificate has the private key. > > Is there some thing wrong in my import code??? > > Thanks, > Raghu/.. > > I don't see any differences. When I imported the pfx file through mmc, I did
not check the exportable check box. So that is not the issues. Any other ideas? Thanks. Show quoteHide quote "Mitch Gallant" <jensigner@community.nospam> wrote in message news:%2377qNhvMGHA.2916@tk2msftngp13.phx.gbl... > Did you check the details of both certificates and compare (i.e. the one > you > imported programaticaly versus the one you manually imported by MMC) ? > > Not sure about this, but the cert.PrivateKey might require that the > private key > be exportable. Check and compare the Exportable flag that is default for > the X509KeyStorageFlags. Might not be able to instantiate > RSACryptoSeviceProvider > that way if key is not exportable .. just a guess. > > - Mitch Gallant > > "Raghu" <RaghuNoSpam> wrote in message > news:edApVrsMGHA.2416@TK2MSFTNGP15.phx.gbl... >> Hi, >> >> I am using .net 2.0 on windows xp (sp2). I wrote following code to import >> the certificate into my store name in local machine location: >> >> X509Store myStore = new X509Store(StoreName.My, >> StoreLocation.LocalMachine); >> myStore.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite); >> >> X509Certificate2 certificate = new X509Certificate2(); >> certificate.Import(fileName, mypassword, >> X509KeyStorageFlags.MachineKeySet); >> myStore.Add(certificate); >> myStore.Close(); >> >> The issuer certificate (for the above certificate) is already in the >> local machine store. >> >> After wards I write following code to dump the contents of the >> certificate (in total different process): >> >> X509Store myStore = new X509Store(StoreName.My, >> StoreLocation.LocalMachine); >> myStore.Open(OpenFlags.ReadOnly); >> >> >> foreach (X509Certificate2 cert in myStore.Certificates) >> { >> Console.WriteLine("Issuer: {0}; Subject={1}", cert.Issuer, cert.Subject); >> if (!cert.HasPrivateKey) >> { >> Console.WriteLine("Certificate has no private key."); >> } >> >> RSACryptoServiceProvider rsa = cert.PrivateKey as >> RSACryptoServiceProvider; >> if (rsa != null) >> { >> ... >> } >> } >> >> The above code throws exception on the line that contains cert.PrivateKey >> property. It is not cast exception ("Keyset does not exist") since I am >> using as operator. >> >> If I delete the certificate and import it through windows Certificates >> mmc, my read code executes fine. In both cases, I can check that the >> imported certificate has the private key. >> >> Is there some thing wrong in my import code??? >> >> Thanks, >> Raghu/.. >> >> > >
fxcop and link demands
How to encrypt/decrypt a file System.Security.SecurityException was unhandled Problem with RSA.ImportParameters() under ASP .NET after changed the permisssion set to "nothing", I can't set the .net configuration anymore? Request for Permission failed ReflectionPermission weird behavior? moving .net containers CryptoAPI impersonation in vb.net |
|||||||||||||||||||||||