title: "Key not valid for use in specified state" while trying to Sign a Xml
body: <h2>My History:</h2>
  
  <p>I need to sign all my <code>Xml's</code> before uploading to the government agency.
  To sign, I'm using client certificates loaded from the <code>X509Store</code>:</p>
  
  <pre><code>var repo = new X509Store("My", StoreLocation.CurrentUser);
  
  repo.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
  
  foreach (X509Certificate2 certCurrent in repo.Certificates)
  {
      if (!certCurrent.Subject.Equals(subject)) continue;
  
      if (certCurrent.NotAfter &gt; DateTime.Now)
      {
          repo.Close();
          return certCurrent;
      }
  }
  
  repo.Close();
  return null;
  </code></pre>
  
  <p>And <code>SignedXml</code> <code>(XmlDocument xmlDoc, string id, X509Certificate2 certificate)</code>:</p>
  
  <pre><code>var refer = new Reference();
  refer.Uri = "#" + id;
  refer.AddTransform(new XmlDsigEnvelopedSignatureTransform());
  refer.AddTransform(new XmlDsigC14NTransform());
  
  var signedXml = new SignedXml(xmlDoc);
  signedXml.SigningKey = certificate.PrivateKey;
  signedXml.AddReference(refer);
  
  signedXml.ComputeSignature();
  
  var key = new KeyInfo();
  key.AddClause(new KeyInfoX509Data(certificate));
  signedXml.KeyInfo = key;
  
  //Final signature xml block.
  XmlElement signedBlock = signedXml.GetXml();
  </code></pre>
  
  <h2>Problem:</h2>
  
  <p>One of my clients changed his PC password and out of nothing the second block of code throws this error:</p>
  
  <pre><code>Message - 
      Key not valid for use in specified state.
  Type - 
      CryptographicException
  Source - 
      mscorlib
  TargetSite - 
      System.Security.Cryptography.SafeProvHandle CreateProvHandle(System.Security.Cryptography.CspParameters, Boolean)
  StackTrace - 
      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&amp; safeProvHandle, SafeKeyHandle&amp; safeKeyHandle)
      at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
      at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
      at System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
      at Util.SignXml.Sign(XmlDocument xmlDoc, String id, X509Certificate2 certificate, String&amp; error)
  </code></pre>
  
  <p>It says that the <code>getter</code> of the <code>PrivateKey</code> is throwing that exception.
  Later, I'll try to install that certificate again, with the <code>Exportable</code> checked.</p>
  
  <p>PS: <code>Windows Server 2003</code></p>