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()
Here is a piece of C# code that creates a StrongNameKeyPair object from a password-protected .PFX file:
public static StrongNameKeyPair GetStrongNameKeyPairFromPfx(string pfxFile, string password)
{
X509Certificate2Collection certs = new X509Certificate2Collection();
certs.Import(pfxFile, password, X509KeyStorageFlags.Exportable);
if (certs.Count == 0)
throw new ArgumentException(null, "pfxFile");
RSACryptoServiceProvider provider = certs[0].PrivateKey as RSACryptoServiceProvider;
if (provider == null) // not a good pfx file
throw new ArgumentException(null, "pfxFile");
return new StrongNameKeyPair(provider.ExportCspBlob(false));
}
NOTE: I assume the PFX here has been created by the .NET Framework tools (for example the Visual Studio Strong Name UI form) to support an assembly strong name creation. It may not be ok with any PFX.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With