受欢迎的博客标签

Office Excel VBA代码加密方案汇总(如何尽可能的保护VBA代码?)

Published

1.VBAProject工程设置密码

该功能是有Excel官方提供,用于对VBA代码设置密码保护

2.vbaProject.bin 破坏性锁定

使用UltraEdit软件打开vbaProject.bin文件,并使用Ctrl+F打开查找对话框,输入id=,定位到需要修改的位置。只要将id后大括号中的位元组随便修改一下即可。保存vbaProject.bin文件;

3.Unviewable+ VBA

通过软件Unviewable+ VBA加密的VBA效果最好。国内使用该软件加密的还不多,但国外很多使用该软件对其的VBA代码进行加密。

作者网址

http://www.spreadsheet1.com/unviewable-vba-project-app-for-excel.html

https://zhuanlan.zhihu.com/p/28203940

4.用VB6把VBA代码写成DLL

一般用VB6把VBA代码写成DLL不过麻烦,使用VB封装为dll进行调用的确可以。

VBA code for Excel can only be written inside Excel using the VBA IDE. VBA projects are stored as part of the Excel file and cannot be loaded into Visual Studio.

具体实现源代码:

https://www.iaspnetcore.com/blog/blogpost/60a0d285b8d10f0221c4b424/encapsulate-vba-operations-into-dll-components-and-invoke-them-in-excel

5.通过VBE外接插件完成VBA代码的动态生成

通过外接插件完成VBA代码的动态生成,动态解密,实时运行,完毕删除这一系列保护代码的动作

detail:http://club.excelhome.net/thread-1368804-1-1.html

6.VBA编辑器隐藏VBA代码

detail:https://github.com/outflanknl/EvilClippy

7.Use Excel Compiler, VBA Compiler 

Excel Compiler, VBA Compiler – software for Excel workbook copy protection

You can compile an Excel spreadsheet into an EXE application with securely protected formulas in binary format and protected VBA code.

https://doneex.com/?option=com_content&task=section&id=10&Itemid=43&gclid=CKSepamRi6cCFQlPgwodbSTAeA

8.VBA Compiler for Excel

How the VBA Compiler works
The DoneEx VbaCompiler transforms source VBA code into C-code and, in the final stage, the C-language code is compiled into a native Windows DLL file.

It's easier and faster than converting your VBA code to C by hand. VBA Compiler website has a simple 10-step guide on how to compile VBA code into a Windows DLL file https://vbacompiler.com/how-to-compile/ and it doesn’t even require any knowledge of C or C++.

During compilation, the transformation of the source VBA code goes through several stages:

Stage 1. VbaCompiler parses and analyses the source VBA code.
Stage 2. The product generates object code based on results of stage 1.
Stage 3. The product generates C-language code from the object code generated in stage 2.
Stage 4. The VbaCompiler runs a C-compiler to compile the C-language code (which was generated in Stage 3) into a Windows DLL.
Stage 5. The final stage of compilation – the VbaCompiler removes compiled VBA code from the file and generates connective VBA code where all compiled DLL functions are called instead of original VBA code.

https://vbacompiler.com/

9.动态增加,删除,修改VBA代码

动态增加,删除,修改VBA代码是这种加密方式的核心价值。

VBA开发者可以提供给用户A数据,代码注册通过之后,动态生成B数据完成核心功能。或者根据程序流程,动态修改运行分支,这样使用者永远也不知道程序的真正运行流程,达到保密效果。

detail:http://club.excelhome.net/thread-1368804-1-1.html

 

建核心代码库(函数名字典+密文代码+代码混淆)+动态解释+动态执行可以隐藏核心代码,处理好api和参数传递就可以了,不过没必要花那个功夫研究,VBA本身就是自用方便,能值得保密的商用程序有几个用vba写.

 

真正的加密只有源代码的加密,不是加个密码打不开就完了。
有的东西没法细说。大概说一下:
青铜:vba自带密码。
黄金:借助网上的工具加密。其实还是设了密码,你能下到的工具,别人也能下到。不说了
钻石:用源代码加密,动态调用。不限制你打开,但人眼不可识别,基本都是乱码,却不影响编译运行。
王者:无招胜有招。无需任何加密,可以打得开,也都看的见源代码(非乱码),但是调用之错综复杂,语句之光怪琉璃,除了本人基本没人能看得懂。

 

Automation Add Ins

An Automation Add In provides several advantages over a standard XLA Add In.

Execution Speed -- An Automation Add In written in VB6 is compiled to native machine code which runs much faster than the interpreted VBA languange.
Security -- Unlike an XLA add in, you never distribute the source code to the end users. If your code has proprietary information or intellectual property value, that remains safely protected on your own computer. It is never distributed to the user. Your code can never be compromised.

 

VB6.0

VB6(Microsoft Visual Basic 6.0)来封装DLL,Microsoft Visual Basic 6.0 的主流技术支持已于 2005 年 3 月 31 日结束.微软已停止对VB6的技术支持,而把VB6升级为VB.NET。

Fortunately VB6 is still supported on Windows 10/Windows Server 2016.

Microsoft Visual Basic 6.0 download url:https://sourceforge.net/projects/vb6extendedruntime/

VB.NET

2002年,微软公司推出全新编程框架,使用Visual Basic在Microsoft .NET Framework上编程,这就是Visual Basic.NET,简称VB.NET。

 Visual Basic in Visual Studio.

The VB6 programming language is not part of .Net.  VB.Net is a completely different language.

 

我们可以在VS里建立com类项目,把NET类库的一些方法重新包装后暴露给VBA使用

 

将EXCEL VBA代码移植到VB.NET

.Net实现VBA,要用VSTO或第三方Office库重写。无法直接转换。

建议将VBA写的函数都贴到 VB6 里面封装做成 DLL,然后在 .net 里面调用。

vba代码一般是在office下操作用的,vb.net不需要转换该代码,需要引用office类型库,再间接调用.

http://blog.sina.com.cn/52uys

 

Office二次开发模式:

1.微软给Excel开发提供了两个接口:一个是常见的Com接口,一个是C API接口

Com接口的执行效率很低。

C API因以下原因而得名:Excel 使用一些标准的 C 数据类型来交换数据;库函数用 C 名称修饰;数据结构为 ANSI C。在具备了适当经验的情况下,使用 C++ 可增强 XLL 项目的易管理性、可读性和稳定性。

因为众所周知,Com接口的执行效率很低。所以,对于一些要求高性能的项目来说,选择C API接口就成了唯一的选择。

微软提供了xll格式的addins,使用C API来操作excel。但是,因为xll只能用C++来开发,使用.net的菜鸟们看着非常羡慕嫉妒恨。所以,很多shim就应运而生,shim意思是给xll提供一个中间垫片,使其能够加载.net写的dll。ExcelDna就是诸多shim中最好的一个。所以,我来回答第一个问题:ExcelDna是确保xll能够加载.net库的一个loader。这么解释不太严谨,但方便各位理解。

1) VBA(visual studio for application),功能有限,语言落后。
2)COM: .net2003中的对象模型(com组件,excel object 11 library,vsto原形)功能强大,需手动操作。
Office PIA: Microsoft.Office.Interop.Excel.dll,还有~access.dll,~word.dll。Microsoft.Office.Core等。PIA是在安装Office的时候选择安装的,而且必须有.NET Framework才能用
3) VSTO: vs.net2005中的vsto(visual studiotool for office,就是对PIA的二次封装)    VSTO方便快捷功能强大。
vsto三类:插件(Add-in),文档级别(如ExcelWorkbook)和模版(Template)。
插件(Add-in):外接程序是应用程序级别,对所有Excel应用都是有效
文档和模板项目:文档级别的程序,只对当前文档和模板有效

VSTO vba 连接数据库的关系(ok)

detail:https://www.cnblogs.com/qq2052702900/p/5559497.html 

 

VBA

VBA 是最早的一个用来扩展Office 应用程序的技术,实现上面提到的这样的需求,如果数据本身就来自于Excel内部的话,VBA很擅长的.

VSTO

VSTO是2005年正式引入的,它的好处是可以基于Microsoft .NET平台进行编程,VSTO的开发语言有VB.NET和C#两种。

最常见的做法创建对应的Ribbon工具栏Add-in,当用户单击了某个按钮后,再根据当前的上下文进行相关的代码处理。

 Important

VSTO relies on the .NET Framework. COM add-ins can also be written with the .NET Framework. Office Add-ins cannot be created with .NET Core and .NET 5+, the latest versions of .NET. This is because .NET Core/.NET 5+ cannot work together with .NET Framework in the same process and may lead to add-in load failures. You can continue to use .NET Framework to write VSTO and COM add-ins for Office. Microsoft will not be updating VSTO or the COM add-in platform to use .NET Core or .NET 5+.

You can take advantage of .NET Core and .NET 5+, including ASP.NET Core, to create the server side of Office Web Add-ins

Web Add-in

Web Add-in是从Office 2013开始支持的新的开发模式,主要在于利用业界标准的Web开发技术来做Add-in开发,具有跨平台和设备的先天优势,集中化部署也降低了运维的复杂性。

Web Add-in包含但不仅限于下面这些主流技术

HTML
CSS
Javacript
Typescript (可选)
NodeJS (可选)

detail:https://www.cnblogs.com/chenxizhang/p/7904401.html

 

Visio solutions

https://docs.microsoft.com/en-us/visualstudio/vsto/visio-solutions?view=vs-2019

VisioComAddinNet5

https://github.com/nbelyh/VisioComAddinNet5

 

 

使用VSTO vs ExcelDna开发插件

VSTO

前提条件:需要.Net Framework框架的支持

我一直是从事Office VBA,VB6研究的,前几年就听说VSTO很强大,将会替代VBA,但是自己没什么C#基础,就一直没有转行。

后来安装了VSTO2012后,先是做几个简单的Windows Form,熟悉了C#语法规则,接着就学着创建Office Addin。

初步的理解,VSTO终究是Tools,是与EXCEL的COM交互的工具,并不是说,有了VSTO,EXCEL也跟着进入.Net时代了.

VSTO是微软提供的一个开发Office插件的很好的方式,可以使用http://VB.Net、C#来开发,开发的插件能够用于Office和WPS,相对ExcelDna开发要稍微简单点。

警告“Microsoft Visual Studio 2010 Tools for Office Runtime (x86 和 x64)”需要项“Microsoft .NET Framework 4.5.2 (x86 和 x64)”,但未包括该项。

ExcelDna

前提条件:需要.Net Framework框架的支持

ExcelDna是用于开发Excel插件的开源项目,可以借助于http://VB.Net、C#和F#进行开发,开发完成后会打包为一个以.xll结尾的文件,发布比较简单,无需部署VSTO环境。但不要以为一.xll结尾的就是使用ExcelDna开发的哦,使用C++等也可以开发哦。

部署容易但只支持Excel

ExcelDna相比VSTO最大的缺点,就是只支持给Excel开发插件,不支持Word或PPT等,也不支持WPS。

 

共同点

二者的共同点就是都需要.Net Framework框架的支持,如果安装过其他插件的小伙伴一定不陌生,WIn8以上的系统都自带.Net 4.5了,所以使用此类系统就很难察觉了,但如果使用WIn7或XP系统,安装后插件可能无法使用,就要检查是否是.Net平台未安装。

 

向工作簿添加新模块。

对于 Excel 5.0 和7.0:从 "插入" 菜单中选择 "宏",然后选择 "模块" 选项。 这将为您提供一个新的模块表 Module1。

对于 Excel 97 和更高版本:按 ALT + F11 以启动 Visual Basic 编辑器。 单击 "插入" 菜单上的 "模块"。