In MATLAB If your code is very important, need to encrypt, at present I have found two methods: First, generate P file; two, generate an. exe file
The method of generating P file in MATLAB is very simple, it is used in command Wondow
Pcode YOURFILE.M
The command can convert the M file to the corresponding Pcode file yourfile.p, which is said to be not decompile.
First, generate P file
The P file is a pre-resolved version of the corresponding m file (preparsed versions). Because when you first execute the M file, Matlab needs to parse it (parse) once (after the first execution of the parsed content will be put into memory for the second execution, that is, the second execution without further parsing), which virtually increased the execution time. So we have to explain beforehand, then when we use the M file, we will directly execute the corresponding resolved version, that is, the P file. But also because the parse speed of MATLAB is very fast, generally do not have to do their own pre-resolution. Pre-resolution is only done when some programs are called to a very high number of M files, such as GUI applications, to increase the speed of subsequent calls.
such as the current directory of MATLAB test.m file, which a=1,b=2, for the pre-resolution, and TEST.P files. Because the P file's invocation priority is higher than the M file, when you call test, the TEST.P is invoked as a preference choice.
The code that can modify TEST.M is:
% ----------------------
% TEST.M
A = 3;
b = 4;
% ----------------------
And then call Test in command window:
>> Test
Warning:p-file C:\Program FILES\MATLAB71\WORK\TEST.P is older than M-file C:\Program.
C:\Program FILES\MATLAB71\WORK\TEST.P May is obsolete and may need to be regenerated.
Type ' help Pcode ' for information about generating p-files from M-files.
A warning will appear warning you that the P file you are calling is older than the M file with the same name, meaning that the M file has been modified. Still, the old P file is invoked, which results in a = 1 and b = 2.
P files can be used for confidential code, if you give someone a M file, others can open to see all your code and algorithms. If your code doesn't want to be seen, you can give him a P file.
The Pcode function can also be applied to the M function file.
P files are encrypted files that can only be run and cannot be restored to M files.
Ii. building. exe file encryption
His disadvantage is not able to run away from the MATLAB stand-alone, must also be called in command window, portability is not strong. This problem can be resolved if the. exe file is generated.
The way to build an. exe file is slightly more complex:
1, first of all to ensure that your program is functional, not the script form. It doesn't matter if it's a scripted form, just add a function []=name () to the first line of the script file.
2, then, in command window with the mcc-m-B sgl file.m command to generate an. exe file, the period will let you choose the compiler (my is VC6.0), will find that a heap of files generated.
3, the next step generated by the pile of files to be copied to the machine to be run. At this time still need to matlab necessary dynamic connection library. Copy the <matlab path >/extern/lib/win32/mglinstallar.exe to the machine to be run.
4. Run Mglinstallar.exe on the machine to be run, then select Extract Directory, will extract bin and toolbox two subdirectories in the specified directory, where bin\ Win32 Directory is the math library and graphics library from the operation of the MATLAB all the necessary dynamic connection library, a total of n. You can take these. dll into System32, or you can put it directly in the application directory (I put it in the application directory). The Toolbox directory must be the same directory as the application.
5. Finished, double-click the. exe to run.
Summary: Both methods can achieve the purpose of protecting source code, but each has its own advantages and disadvantages. The advantage of generating P files is that the method is simple, the size of the file is only about 3 times times the source code, the disadvantage is not to run away from the MATLAB environment. The advantage of generating an. exe file is that it can be run independently of MATLAB, with the disadvantage that the resulting file size is thousands of times times the original (5555555555, of course, contains those. dll files, the most bulky part). Take my program to make a comparison, the source code is 8.34K, generate p file 22.6K, generate executable files (including those. dll) a total of more than 30 trillion.
Resources:
1, MATLAB P-code how to open
2, for Matlab m file how to encrypt