Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unable to read Private Key as XML String

Unable to read Private Key as XML String

Scheduled Pinned Locked Moved C#
helpcsharpsysadmindata-structuressecurity
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jadhav Ajay
    wrote on last edited by
    #1

    My C# web application is trying to read the Private Key of the our own digital certificate installed on Windows 2008 Server, when it needs to decrypt the data received from the web request. Following is the code:

    X509Certificate2 cert = GetCert(thumbPrintOfPrivKey, StoreName.My, StoreLocation.CurrentUser);

    try
    {

    string privateKey = cert.PrivateKey.ToXmlString(true);
    log.Info("Retrieved Private Key....");

    rsacsp.FromXmlString(privateKey);

    }
    catch(Exception e)
    {
    string msg = "Unable to Read the Private Key: ";
    log.Error(msg + e.Message);

    throw new Exception(msg, e);
    }

    And following line of above-mentioned code throws an error.

    string privateKey = cert.PrivateKey.ToXmlString(true);

    The error which I get is: - "The system cannot find the file specified." And following is the stack Trace:

    at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
    at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
    at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
    at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
    at System.Security.Cryptography.RSACryptoServiceProvider..ctor(CspParameters parameters)
    at System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
    at SSO.SSOUtility.ProcessData(String decodedSig, String decodedToken, String combined) in

    When I checked my Application Event Logs to see what exactly is happening, the event log showed that the "CertificateServicesClient" has stopped. And I noticed that it gets started automatically when I logon to the server. Can anybody please help me in finding the reason as to why this service(?) stops and starts automatically, and how can we restart it immediately the moment it stops? Thanks in advance.

    ----- Ajay Jadhav http://www.divinet.co.in

    K 1 Reply Last reply
    0
    • J Jadhav Ajay

      My C# web application is trying to read the Private Key of the our own digital certificate installed on Windows 2008 Server, when it needs to decrypt the data received from the web request. Following is the code:

      X509Certificate2 cert = GetCert(thumbPrintOfPrivKey, StoreName.My, StoreLocation.CurrentUser);

      try
      {

      string privateKey = cert.PrivateKey.ToXmlString(true);
      log.Info("Retrieved Private Key....");

      rsacsp.FromXmlString(privateKey);

      }
      catch(Exception e)
      {
      string msg = "Unable to Read the Private Key: ";
      log.Error(msg + e.Message);

      throw new Exception(msg, e);
      }

      And following line of above-mentioned code throws an error.

      string privateKey = cert.PrivateKey.ToXmlString(true);

      The error which I get is: - "The system cannot find the file specified." And following is the stack Trace:

      at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
      at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
      at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
      at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
      at System.Security.Cryptography.RSACryptoServiceProvider..ctor(CspParameters parameters)
      at System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
      at SSO.SSOUtility.ProcessData(String decodedSig, String decodedToken, String combined) in

      When I checked my Application Event Logs to see what exactly is happening, the event log showed that the "CertificateServicesClient" has stopped. And I noticed that it gets started automatically when I logon to the server. Can anybody please help me in finding the reason as to why this service(?) stops and starts automatically, and how can we restart it immediately the moment it stops? Thanks in advance.

      ----- Ajay Jadhav http://www.divinet.co.in

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Isn't this a repost?

      J 1 Reply Last reply
      0
      • K Keith Barrow

        Isn't this a repost?

        J Offline
        J Offline
        Jadhav Ajay
        wrote on last edited by
        #3

        Yes, it is.

        ----- Ajay Jadhav http://www.divinet.co.in

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups