我正在尝试使用密码保护的密钥文件(pfx)程序化地签名程序集。然而,当我尝试使用StrongNameKeyPair时,我遇到了以下异常。
无法获取StrongNameKeyPair的公钥。 在System.Reflection.StrongNameKeyPair.ComputePublicKey()中 在System.Reflection.StrongNameKeyPair.get_PublicKey()中
我正在尝试使用密码保护的密钥文件(pfx)程序化地签名程序集。然而,当我尝试使用StrongNameKeyPair时,我遇到了以下异常。
无法获取StrongNameKeyPair的公钥。 在System.Reflection.StrongNameKeyPair.ComputePublicKey()中 在System.Reflection.StrongNameKeyPair.get_PublicKey()中
这里有一段C#代码,可以从一个密码保护的.PFX文件中创建一个StrongNameKeyPair对象:
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));
}
注意:我假定这里的PFX是由.NET Framework工具创建的(例如Visual Studio强名称UI表单),以支持程序集强名称创建。它可能不适用于任何PFX。