views:

172

answers:

1

I have a DSA private key exported using the DSACryptoServiceProvider.ToXmlString, and I need to convert it to PEM format ("file.pem"), so I can open it in PHP using openssl_pkey_get_private function.

How do I accomplish this?

The solution can use DSACryptoServiceProvider.ExportCspBlob method if it's of any help, I just need to convert the key.

+2  A: 

Using the bouncycastle C# library class DotNetUtilities, it is fairly easy.

DSACryptoServiceProvider dsa = new DSACryptoServiceProvider(1024);
AsymmetricCipherKeyPair dsaKey = DotNetUtilities.GetDsaKeyPair(dsa);
using (StreamWriter sw = new StreamWriter("dsa.pem"))
{
    PemWriter pw = new PemWriter(sw);
    pw.WriteObject(dsaKey);
}
GregS
+1, I'm just surprised there is no direct way how to do that using pure .NET framework only (no additional external libraries).
Paja
Well, the PEM format is not a real standard, more a de-facto one.
GregS