title: .Net Crypto Service Provider error using nShield HSM
body: <p>I am trying to use nShield from Thales to generate pair of asymmetric keys on it.
  I have found the following <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.cspparameters%28v=vs.110%29.aspx" rel="nofollow">example</a> on msdn:</p>
  
  <pre><code>CspParameters csp = new CspParameters(1, "eToken Base Cryptographic Provider");
  csp.Flags = CspProviderFlags.UseDefaultKeyContainer;
  try
  {
      RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp); 
              key = rsa.ToXmlString(true);
  
  }
  catch(Exception ex )
  {
      string s = ex.Message;
  }
  </code></pre>
  
  <p>I can use KeySafe to succesfully connect and generate key-pairs on the HSM.
  The code above throws the following exception:</p>
  
  <pre><code>System.Security.Cryptography.CryptographicException      
  "Invalid Signature."    System.Security.Cryptography.CryptographicException
  </code></pre>
  
  <p>I have the feeling that I am not setting the correct second parameter in the CspParameters constructor. This is what it says in the example:</p>
  
  <pre><code> // The 1st parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider Types. 
   // The 2nd parameter comes from HKEY_LOCAL_MACHINE\Software\Microsoft\Cryptography\Defaults\Provider.
  </code></pre>
  
  <p>I don't see any nCipher or nShield or Thales or anything like that there.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Working test:</p>
  
  <pre><code>RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
  byte[] data = Encoding.ASCII.GetBytes("string");
  byte[] enc = rsa.Encrypt(data, false);
  String dec = Encoding.ASCII.GetString(rsa.Decrypt(enc, false));
  key = rsa.ToXmlString(true);
  </code></pre>