基本信息
源码名称:使用dnlib实现对.net程序进行混淆加密
源码大小:21.67M
文件格式:.zip
开发语言:C#
更新时间:2025-09-15
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 5 元 
   源码介绍

使用dnlib实现对.net编写的代码进行混淆加密 能够一定程度上防止反编译


public void AddCopyrightInfo(ModuleDef module, string copyrightText, string author = "ZuoTianProtector", string version = "1.0")
        {
            // 创建版权特性类
            var attributeBase = module.CorLibTypes.GetTypeRef("System", "Attribute");
            var copyrightAttrType = new TypeDefUser(
                "ZuoTian.Protector.Attributes",
                "ZuoTianCopyrightAttribute",
                attributeBase
            )
            {
                Attributes = TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public |
                             TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit
            };

            // 添加构造函数
            var ctorSig = MethodSig.CreateInstance(
                module.CorLibTypes.Void,
                new[] { module.CorLibTypes.String, module.CorLibTypes.String, module.CorLibTypes.String }
            );
            var ctor = new MethodDefUser(
                ".ctor",
                ctorSig,
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName
            )
            {
                Body = new CilBody()
            };

            // 获取并验证基类构造函数引用
            var baseCtor = module.Import(typeof(Attribute).GetConstructor(Type.EmptyTypes));
            if (baseCtor == null)
                throw new InvalidOperationException("无法导入 Attribute 基类构造函数,可能是 dnlib 版本不兼容");

            // 构建构造函数指令
            var ctorInstructions = new[] {
                Instruction.Create(OpCodes.Ldarg_0),
                Instruction.Create(OpCodes.Call, baseCtor),
                Instruction.Create(OpCodes.Ret)
            };
            foreach (var instr in ctorInstructions)
            {
                ctor.Body.Instructions.Add(instr);
            }
            copyrightAttrType.Methods.Add(ctor);

            // 添加属性
            AddPropertyToType(module, copyrightAttrType, "CopyrightText", module.CorLibTypes.String);
            AddPropertyToType(module, copyrightAttrType, "Author", module.CorLibTypes.String);
            AddPropertyToType(module, copyrightAttrType, "Version", module.CorLibTypes.String);

            // 添加到模块
            module.Types.Add(copyrightAttrType);

            // 为类添加版权特性
            foreach (var type in module.Types)
            {
                if (type.IsGlobalModuleType || type.IsRuntimeSpecialName || type.IsSpecialName || type.IsInterface)
                    continue;

                var importedCtor = module.Import(ctor);
                var copyrightAttrInstance = new CustomAttribute(importedCtor)
                {
                    ConstructorArguments = {
                        new CAArgument(module.CorLibTypes.String, copyrightText),
                        new CAArgument(module.CorLibTypes.String, author),
                        new CAArgument(module.CorLibTypes.String, version)
                    }
                };

                type.CustomAttributes.Add(copyrightAttrInstance);
            }
        }