Creating self-signed test certificates with Bouncy Castle

I've been messing about with Bouncy Castle as a means to create self-signed X509 certificates for testing purposes. Essentially I've taken the information from these three blog entries here and here and here, making them into one piece of code. It seems to be a pretty nifty way to make test certificates using C#.

Here are some instructions: first, create a new Console Application in Visual Studio (I'm doing .Net 4) and use NuGet to add Bouncy Castle (I've got v1.7.0) to the project. Then simply use the attached code example. It demonstrates creating a fictional type of security certificate and saving it to disk.

Obviously, I can't take the credit. The clever stuff is all done by Bouncy Castle, and Roger's Blog has demonstrated how to write the C# code. All that I've done is put it together in one place.

The result is a .net X509Certificate2 object which can be used in the normal way.