title: The requested operation is not supported in CngKey.Create
body: <p>I'm trying to generate a self-signed certificate on the fly (programmatically) in a C# assembly (targeting <code>.NET 4.0</code>), to serve as a root CA to generate other certificates. The certificate doesn't need to be persisted in the Windows certificate store, I'll export it as a file.</p>
  
  <p>Reading through <a href="https://stackoverflow.com/q/13806299/3136474">this question</a> (and in particular, <a href="https://stackoverflow.com/a/13830861/3136474">@dthorpe's answer</a>), I decided to give a try to <a href="http://clrsecurity.codeplex.com/" rel="nofollow noreferrer">CLR Security</a>.</p>
  
  <p>The <code>CLR Security</code> library put an extension method on <a href="https://msdn.microsoft.com/en-us/library/system.security.cryptography.cngkey(v=vs.110).aspx" rel="nofollow noreferrer">CngKey class</a> to generate a self-signed certificate, but I couldn't succeed in creating an instance of <code>CngKey</code> with:</p>
  
  <pre><code>var key = CngKey.Create(CngAlgorithm.Sha1); //same with Sha256, Sha512 and MD5
  //or
  var key = CngKey.Create(CngAlgorithm.Sha1, null, new CngKeyCreationParameters()
  {
      ExportPolicy = CngExportPolicies.AllowExport,
      KeyUsage = CngKeyUsages.AllUsages,
      KeyCreationOptions = CngKeyCreationOptions.MachineKey,
  });
  </code></pre>
  
  <p>Any of these lines raises the exception:</p>
  
  <blockquote>
    <p>System.Security.Cryptography.CryptographicException was unhandled<br>
    HResult=-2146893783<br>
    Message=The requested operation is not supported.</p>
  </blockquote>
  
  <pre><code>Source=System.Core  
  StackTrace:  
    at System.Security.Cryptography.NCryptNative.CreatePersistedKey(SafeNCryptProviderHandle provider, String algorithm, String name, CngKeyCreationOptions options)  
    at System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm,  String keyName, CngKeyCreationParameters creationParameters)  
    at System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm)  
    at Tests.Program.Main(String[] args) at Program.cs:line 51
  </code></pre>
  
  <p>Searching through SO and the internet, I've checked the following:</p>
  
  <ul>
  <li>I'm running a Windows 7 box (so it supports RPC as per <a href="https://msdn.microsoft.com/en-us/library/ms838991.aspx" rel="nofollow noreferrer">MSDN</a>)</li>
  <li>Tried on a Windows Server 2012 box, same error</li>
  <li>The process is running as admin (so it have access to all cert storages, anyway)</li>
  <li>The services <code>CNG Key Isolation</code> and <code>Remote Procedure Call (RPC)</code> are running</li>
  </ul>
  
  <p>Any help would be appreciated.</p>