Sign a .NET Assembly with a Strong Name Without Recompiling

本文介绍了一种为第三方.NET组件添加强签名的方法,通过反编译原始DLL到中间语言(IL),再重新编译并加入密钥文件,实现不对源项目进行修改的情况下完成签名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Signing a .NET assembly with a strong name is easy in Visual Studio. However, what if this is a 3rd party assembly and you don't have the source?

For me, I have an application that has a requirement that all assemblies are signed with a strong name. One of the assemblies I am using is RestSharp. I like to contribute to RestSharp and I didn't want to modify the project file to sign the assembly as I didn't want that to go back to the repository when I had some changes to contribute.

Not a problem. What I have is a batch file that disassembles the DLL to IL and then reassembles the IL back into a DLL and includes my key file. This way I get to keep the original DLL for projects that I don't need the assembly to have a strong name and a separate one that is signed with the strong name for the projects where I need that.

Here's what I did:
  1. In the Release build folder I created the following:
    • The key file (create using sn.exe -k MyPublicPrivateKeyFile.snk)
    • A subfolder to contain the signed assembly, mine is named "Signed"
    • A batch file (see below)
  2. Create a batch file in the Release folder named "SignAssembly.bat" with the following contents:
del .\Signed\RestSharp.* /F
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe" .\RestSharp.dll /out:.\Signed\RestSharp.il
"C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe" .\Signed\RestSharp.il /dll /key=.\RestSharp.snk /output=.\Signed\RestSharp.dll

pause
The first line deletes any previously signed assembly. Second line uses ILDASM to disassemble the DLL to IL in the "Signed" folder as RestSharp.il. The third line reassembles the IL into an assembly using ILASM and tells it to use the IL file and the key file. That's it.

Now, whenever I do a new release build, I just run the batch file and I have a signed copy to use, and I did it without changing the project file to do it.                   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值