Search code examples
.netcode-signingstrongnamepkcs#12assembly-signing

How to use StrongNameKeyPair with a password protected keyfile (pfx)?


I am trying to programatically sign an assembly using a password protected keyfile (pfx). However when I try to use the StrongNameKeyPair I get the following exception.

Unable to obtain public key for StrongNameKeyPair. at System.Reflection.StrongNameKeyPair.ComputePublicKey() at System.Reflection.StrongNameKeyPair.get_PublicKey()


Solution

  • It does not look like Microsoft updated StrongNameKeyPair to support anything other than the snk file format - as there's, sadly, no constructor accepting an RSA instance.

    In fact my understanding is that the PKCS#12 support for strongnaming assemblies is an indirect one, i.e. provided by the tools (msbuild or VS.NET) and not directly by the framework.

    However you can still programatically sign an assembly using any RSA instance by re-using the (MIT.X11 licensed) code from Mono.Security StrongName class, available in github. It's a bit more work - but it will work on any framework version and any (future) format that gets promoted :-)