title: System.Security.Cryptography.CryptographicException: keyset does not exist
body: <p>When I make an x509 certificate to encrypt and decrypt messages, I got some error information and could not able to fix this problem. Could someone ever happen to solve this bug? thanks.</p>
  <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.</p>
  <p>Exception Details:</p>
  <blockquote>
  <p>System.Security.Cryptography.CryptographicException: keyset does not exist。</p>
  <p>Source Error:</p>
  <p>Line 53:             using (RSACryptoServiceProvider
  rsaProviderDecrypt =
  (RSACryptoServiceProvider)cerDecrypt.PublicKey.Key) Line 54:<br />
  { Line 55:                 plainHashBytes =
  rsaProviderDecrypt.Decrypt(encryptedHashBytes, false); Line 56:<br />
  rsaProviderDecrypt.Clear(); Line 57:<br />
  rsaProviderDecrypt.Dispose();</p>
  <p>Source File:
  E:\PayUSite\PayMvcApp\Controllers\HashMessageController.cs    Line: 55</p>
  <p>Stack Trace:</p>
  <p>[CryptographicException: keyset does not exist. ]<br />
  System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32
  hr) +41<br />
  System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle
  pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean
  fOAEP, ObjectHandleOnStack ohRetDecryptedKey) +0<br />
  System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[]
  rgb, Boolean fOAEP) +579</p>
  </blockquote>
  <p><strong>source code:</strong></p>
  <pre><code>string docFile = Server.MapPath(&quot;~/docx/DirectAccess_StepByStep.doc&quot;);
  HashAlgorithm hash = HashAlgorithm.Create(&quot;SHA1&quot;);
  byte[] hashedBytes;
  using (FileStream fs = new FileStream(docFile, FileMode.Open))
  {
      //compute message hash value
      hashedBytes = hash.ComputeHash(fs);
      hash.Dispose();
      fs.Close();
  }
      
  string hashedString = Convert.ToBase64String(hashedBytes);
      
  //encrypt message digest
  string priKeyFile = Server.MapPath(&quot;~/certificate/WosMiddle.pfx&quot;);
  X509Certificate2 certEncrypt = new X509Certificate2(priKeyFile, &quot;123456&quot;);
  byte[] encryptedHashBytes;
  using (RSACryptoServiceProvider rsaProviderEncrypt = (RSACryptoServiceProvider)certEncrypt.PrivateKey)
  {
      encryptedHashBytes = rsaProviderEncrypt.Encrypt(hashedBytes, false);
      rsaProviderEncrypt.Dispose();
  }
      
  //decrypt message digest
  string pubKeyFile = Server.MapPath(&quot;~/certificate/WosMiddle-pubkey.cer&quot;);
  X509Certificate2 cerDecrypt = new X509Certificate2(pubKeyFile);
  byte[] plainHashBytes;
  using (RSACryptoServiceProvider rsaProviderDecrypt = (RSACryptoServiceProvider)cerDecrypt.PublicKey.Key)
  {
      //***will throw error message here...***
      plainHashBytes = rsaProviderDecrypt.Decrypt(encryptedHashBytes, false);
      rsaProviderDecrypt.Dispose();
  }
      
  //verify message whether was modified
  string docFile2 = Server.MapPath(&quot;~/docx/DirectAccess_StepByStep.doc&quot;);
  HashAlgorithm hash2 = HashAlgorithm.Create(&quot;SHA1&quot;);
  byte[] hashedBytes2;
  using (FileStream fs2 = new FileStream(docFile2, FileMode.Open))
  {
      //compute message hash value
      hashedBytes2 = hash.ComputeHash(fs2);
      fs2.Close();
  }
      
  //compare hash value
  bool isEqual = plainHashBytes.SequenceEqual(hashedBytes2);
  </code></pre>