强签名大家并不陌生,如下图所示:
如果代码是机密的,还需要用到 仅延迟签名,
如果只这样做了,代码中没什么逻辑判断,代码还是不够安全的:
1. Strong Name Remover 工具可以很容易的清除你的签名,如下图:
2. 签名替换工具,如下图:
所以,仅仅有签名是不够的,还需要在代码中判断程序集中是否有签名,且签名是自己的。
接下来,需要做的工作如下:
1. public key token
在 Visual Studio Tools 中找到命令提示,启动并执行 ”sn -Tp 你的程序集“
2. 代码
const string _sLicenseErr = "FCL.Core 被篡改..."; [DllImport("mscoree.dll", CharSet = CharSet.Unicode)] public static extern bool StrongNameSignatureVerificationEx(string wszFilePath, bool fForceVerification, ref bool pfWasVerified);
string wszFilePath = Assembly.GetExecutingAssembly().Location; Debug.WriteLine(wszFilePath); bool fForceVerification = true; bool pfWasVerified = false; bool bFlg = StrongNameSignatureVerificationEx(wszFilePath, fForceVerification, ref pfWasVerified); if (!bFlg) { throw new Exception(_sLicenseErr + " 0x0001"); } byte[] data = Assembly.GetExecutingAssembly().GetName().GetPublicKeyToken(); if (data.Length == 0) { throw new Exception(_sLicenseErr + " 0x0002"); } StringBuilder sb = new StringBuilder(); foreach (byte b in data) { sb.AppendFormat("{0:x2}", b); } string sPK = sb.ToString(); if (sPK != "???f8f1c????ffdf") { throw new Exception(_sLicenseErr + " 0x0003"); }