title: How can I create a self-signed certificate using C#?
body: <p>I need to create a self-signed certificate (for local encryption - it's not used to secure communications), using C#. </p>
  
  <p>I've seen some implementations that use <a href="http://www.daveamenta.com/2008-05/what-is-pinvoke-pinvoke/" rel="noreferrer">P/Invoke</a> with <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa379884%28v=vs.85%29.aspx" rel="noreferrer">Crypt32.dll</a>, but they are complicated and it's hard to update the parameters - and I would also like to avoid P/Invoke if at all possible.</p>
  
  <p>I don't need something that is cross platform - running only on Windows is good enough for me.</p>
  
  <p>Ideally, the result would be an X509Certificate2 object that I can use to insert into the Windows certificate store or export to a <a href="https://en.wikipedia.org/wiki/X.509#Certificate_filename_extensions" rel="noreferrer">PFX</a> file.</p>