答案肯定是有办法了!Microsoft在设计.NET时就考虑到混淆了,并暴露出System.Reflection.ObfuscationAttribute和System.Reflection.ObfuscateAssemblyAttribute,并建议(仅仅是建议,没有要求,所以这个不是万能的)混淆工具提供商对它们提供支持。目前大多数的混淆工具提供商都支持了它们(不知道有哪个没有支持,如果你知道,欢迎提出
![](https://www.cnblogs.com/Emoticons/msn/lightbulb.gif)
为了说明必要,先拉出我们的测试code来:
1
public
class
Type1
2![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodA()
{ }
4![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodB()
{ }
5
}
6![](/Images/OutliningIndicators/None.gif)
7
public
class
Type2
8![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
9![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodA()
{ }
10![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodB()
{ }
11
}
编译之后使用IDE自带Dotfuscator做加密,注意这里在Rename配置页里面什么也不做,在Input配置页里选中Honor Obfuscation Attributes,然后就保存并Run,很快看到输出结果,发现所有的类和方法都加密了。
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
4
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
修改Code如下,添上ObfuscationAttribute,代码如下,这里我希望Type1的MethodA方法不混淆,Type2的名字不被混淆,但其他的都自动混淆,到底能不能做到呢?
1
[assembly: ObfuscateAssemblyAttribute(
true
)]
2![](/Images/OutliningIndicators/None.gif)
3
public
class
Type1
4![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
5
[ObfuscationAttribute(Exclude = true)]
6![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodA()
{ }
7![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodB()
{ }
8
}
9![](/Images/OutliningIndicators/None.gif)
10
[ObfuscationAttribute(Exclude
=
true
, ApplyToMembers
=
false
)]
11
public
class
Type2
12![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
13
[ObfuscationAttribute(Exclude = false, Feature = "default",
14
StripAfterObfuscation = false)]
15![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodA()
{ }
16![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void MethodB()
{ }
17
}
这里用到了ObfuscateAssemblyAttribute和ObfuscationAttribute,偶从MSDN摘取了它们的功能说明:
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
5
![](/Images/OutliningIndicators/InBlock.gif)
6
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/None.gif)
12
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
17
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
ObfuscateAssemblyAttribute:指示模糊处理工具对适当的程序集类型使用其标准模糊处理规则。
ObfuscationAttribute:指示模糊处理工具对程序集、类型或成员采取指定的操作。
备注:
ObfuscationAttribute 和 ObfuscateAssemblyAttribute 属性使得程序集作者可以批注二进制文件,以便模糊处理工具能够使用最少的外部配置正确处理这些二进制文件。
重要事项: |
---|
应用此属性不会自动模糊处理该属性应用到的代码实体。应用此属性是为模糊处理工具创建配置文件的替代方法。也就是说, |
当应用于某个程序集时, ObfuscationAttribute 也应用于该程序集内的所有类型。如果 ApplyToMembers 属性 (Property) 未指定或设置为 true,则该属性 (Attribute) 也应用于所有成员。 ObfuscationAttribute 不指定程序集是公共的还是私有的。若要指定程序集是公共的还是私有的,请使用 ObfuscateAssemblyAttribute 属性。
如果 ApplyToMembers 属性未指定或设置为 true,则当应用于类或结构时, ObfuscationAttribute 也应用于该类型的所有成员。当应用于方法、参数、字段和属性 (Property) 时,该属性 (Attribute) 仅影响其应用到的实体。
修改完代码,编译,再次使用Dotfuscator,使用刚才相同的配置混淆,结果如下图,是不是和想象的一样。
![](https://www.cnblogs.com/images/cnblogs_com/winkingzhang/117454/o_obfuscator.jpg)
结论:
通过给特定的类、方法、属性、Assembly可以帮助简化混淆工具的配置脚本。