Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DSA asymmetric key in .NET XML format to PEM format

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.

like image 717
Paya Avatar asked Jul 26 '10 19:07

Paya


1 Answers

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);
}
like image 60
President James K. Polk Avatar answered Oct 23 '22 13:10

President James K. Polk