Search code examples
c#.netcryptographystrongnamesnk

How to convert a strongname public key (snk) to <RSAKeyValue>?


I have a file testpublic.snk which holds a public key. I have created it with sn -p c:\test.snk c:\testpublic.snk. Now, how can I convert testpublic.snk to a string like

<RSAKeyValue><Modulus>z140GwiEJvuGOVqMQUxnojILR8rn2SFOViigoloku59H5eqzqca3Hdyl/jc+Acdb5ktzhBOOyGFElE0/Btlvw9cXVVW8zcT0MBOCaq25D1rSVYLGGM6nXzBrl1XsrBEadZbCgkcF5rw8GaYcYakijaltP1/hvxhbMOARM9VCQ50=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

Thanks for your help.


Solution

  • Simply re-use the (MIT.X11 licensed) code from Mono.Security StrongName class, available in github, then call ToXmlString on it's RSA property. Something like:

    Console.WriteLine (new StrongName (data).RSA.ToXmlString (false));
    

    Where data is a byte[] containing the content of your snk file. Also you can use true if you want the private key in your XML (it will work if it was available fom your snk file).